1

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"

enter image description here

  • 3
    So many hard-coded ids...that test is just painful to look at. – Adrian Larson Aug 2 '16 at 19:27
  • 2
    You need to call CreateCasefromContract.createCase from your test to emulate what the button click does. – Keith C Aug 2 '16 at 19:28
1

You never called the method createCase in your test. Try doing something like this:

@isTest
public class Test_CaseCreateWebService {

    static testMethod void test_method_one() {
        // Implement test code

        Account acct = new Account();
        acct.RecordTypeId = '012d0000000kS4q';  // Charity Record Type
        acct.Name = 'Test Account';
        acct.SP_ID__c = '296352';
        insert acct;


        Contract cont = new Contract();
        cont.RecordTypeId = '012630000004MBT';  // Financial Record Type
        cont.AccountId = acct.Id;
        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';
        cont.State__c = 'a0h630000005AWB';
        //cont.Entity_SPID__c = acct.SP_ID__c;
        insert cont;

        // Test for case
        if(cont.RecordTypeId == '012630000004MBT')
        {
            CreateCasefromContract.createCase(cont.Id);
        }
    }
}

where cont is the contract you created in your test.


Other considerations:

  1. Also I recommend you avoid hardcoding ids. Create a dummy user, and use the account you created in the test (the account is created but never used) instead of hardcoding account id. In general: create records if its necessary to avoid hardcoding ids.
  2. Add assert sentences at the end of your test. Don't only care about coverage also care about correctness of your code, every test should test that the logic is working as expected.
  3. If you change the code to insert the user, you may face the issue MIXED_DML_OPERATION, that can be resolved whit System.runAs.

Here is some documentation that could be usefull: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods.

  • Also I recommend you avoid hardcoding ids. Create a dummy user, and use the account you created in the test (the account is created but never used) instead of hardcoding account id. – D. Cruz Aug 2 '16 at 19:36
  • 1
    You should edit this into your answer. – battery.cord Aug 2 '16 at 19:39
  • I got it, but where would you add that? – Templar Aug 2 '16 at 21:33
  • Instead of manual case creation, you must invoke that method. I will update my answer and let me know if it was your solution. – D. Cruz Aug 2 '16 at 22:01
  • D. Cruz I've tried what you posted above and now getting error "System.QueryException: List has no rows for assignment to SObject". – Templar Aug 5 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.