I've created a custom button on Contract Object (it is a web service button), if you button clicked it creates Case and copy view values from contract and account to new created case. The button is working fine. I'm trying to write a test unit class and it passes but shows 0%. Any idea why because I do not see any errors. custom button apex web service class:
global with sharing class CreateCasefromContract {
WebService static String createCase(ID ContractId)
{
Contract record = [select Id, AccountId, State__c, State_Name__c, First_Financial_Period__c,
Filing_Due_Date__c, ContractNumber, Standard_Financial_Period__c,
RecordTypeId, Account.Non_Solicitation_Agreement__c, Status,
Account.Inactive_Flag__c, Name, Account.Call_Center__c,
Entity_SPID__c, Account.GP_ID__c, Account.Charity_Code__c,
Account.Advertising_Participant__c, Account.Advertising_Rate__c,
Account.RecordType.Name, RecordType.Name
from Contract
where Id =:ContractId
AND Account.Non_Solicitation_Agreement__c=false
];
String ContractNumber = record.Id;
String StateNumber = record.State__c;
String FilingDueDate = String.ValueOf(record.Filing_Due_Date__c);
String EntityRecordType = String.ValueOf(record.Account.RecordType.Name);
State_Rule__c listofStates = [select Id, Annual_Financial_Reporting_Required__c,
Annual_Reporting_Due_Date__c, Charity_Signature_Required__c,
Donation_Detail_Required__c, Final_Financial_Reporting_Required__c,
Final_Reporting_Due_Date__c, Financial_Reporting_Lead_Time__c
from State_Rule__c
where Id =:StateNumber];
if( record.RecordType.Name == 'Financial')
{
case objCas = new case();
objCas.AccountId = record.AccountId;
objCas.RecordTypeId = [select Id from RecordType where Name = 'Financial' and SobjectType='Case' Limit 1].Id;
objCas.Status= 'Waiting Data';
objCas.Origin = 'web';
//Case Charity Information Section
objCas.Contract_Name__c = record.Name; //Although in Charity Section is populated from Contract
objCas.SPID__c = record.Entity_SPID__c; //Although in Charity Section is populated from Contract
objCas.Call_Center__c = record.Account.Call_Center__c;
objCas.GPID__c = record.Account.GP_ID__c;
objCas.Char_Code__c = record.Account.Charity_Code__c;
objCas.Advertising_Participant__c = record.Account.Advertising_Participant__c;
objCas.Advertising_Rate__c = record.Account.Advertising_Rate__c;
objCas.Entity_Record_Type__c = EntityRecordType;
//Case State Filing Information Section
objCas.Contract_Number__c = record.ContractNumber;
objCas.Filing_Due_Date__c = FilingDueDate;
objCas.First_Financial_Period__c = record.First_Financial_Period__c;
objCas.Standard_Financial_Period__c = record.Standard_Financial_Period__c;
objCas.State__c = record.State_Name__c;
objCas.Contract_Number_lookup__c = record.Id;
//Case State Regulation Information
objCas.Annual_Financial_Reporting_Required__c = listofStates.Annual_Financial_Reporting_Required__c;
objCas.Annual_Reporting_Due_Date__c = listofStates.Annual_Reporting_Due_Date__c;
objCas.Charity_Signature_Required__c = listofStates.Charity_Signature_Required__c;
objCas.Donation_Detail_Required__c = listofStates.Donation_Detail_Required__c;
objCas.Final_Financial_Reporting_Required__c = listofStates.Final_Financial_Reporting_Required__c;
objCas.Final_Reporting_Due_Date__c = listofStates.Final_Reporting_Due_Date__c;
objCas.Financial_Reporting_Lead_Time__c = listofStates.Financial_Reporting_Lead_Time__c;
insert objCas;
}
else if( record.RecordType.Name == 'NOI')
{
// Do similar to the above Case creation scenario
}
return ContractNumber;
}
}
The Test Unit Class is below:
@isTest
public class Test_CaseCreateWebService {
static testMethod void test_method_one() {
// Implement test code
Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Account;
Map<String, Schema.RecordTypeInfo> AccountRecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
//Now get the RecordTypeId we will have to use mentod getRecordTypeId
Id rtId = AccountRecordTypeInfo.get('Charity').getRecordTypeId();
//Now Use insert Account Record typ Like:
Account acct = new Account(RecordTypeId=AccountRecordTypeInfo.get('Charity').getRecordTypeId(),
Name ='Test Account',
SP_ID__c = '123456',
Non_Solicitation_Agreement__c=false);
insert acct;
State_Rule__c sta = new State_Rule__c();
sta.Name = 'Connecticut';
sta.Annual_Financial_Reporting_Required__c = true;
insert sta;
RecordType contRecTyp = [select id, name
from RecordType
where sobjectType = 'Contract'
AND name = 'Financial'
limit 1];
Contract cont = new Contract();
cont.RecordTypeId = contRecTyp.Id;
//cont.RecordType.Name = 'Financial';
cont.AccountId = acct.Id; //'00163000009nTtt';
cont.Status = 'Draft';
cont.StartDate = Date.Today();
cont.Filing_Due_Date__c = Date.Today().addDays(30);
cont.ContractTerm = 12;
cont.Reason_for_Filing__c = 'State of Domicile';
insert cont;
if(contRecTyp.Name == 'Financial')
{
CreateCasefromContract.createCase(cont.Id);
}
}
}
I've modify the above Unit Test class but now i'm getting salesforce unit test create case error "system.queryException: List has no rows for assignment to SObject"

CreateCasefromContract.createCasefrom your test to emulate what the button click does.