0

I've created a custom button on Contract Object (it is a web service button), if you click the button a Case gets created and copies values from contract, account and a custom object state filing guide line to the newly created case. The button is working fine. I'm trying to write a test unit class and it is not passing beyond %22 and it gives error "System.QueryException: List has no rows for assignment to SObject". 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];

        // the if statement check for record type Financial and Non-Soliciate is False
        if( record.RecordType.Name == 'Financial' && record.Account.Non_Solicitation_Agreement__c == false)
        {
            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;
    }
}

Unit Test Class:

    @isTest
public class Test_CaseCreateWebService {

    static testMethod void test_method_one() {

        RecordType acctRecTyp = [select id, name 
                                 from RecordType
                                 where sobjectType = 'Account' 
                                 AND name = 'Charity'
                                 limit 1];

        Account acct = new Account(RecordTypeId = acctRecTyp.Id,
                                   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.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';
        insert cont;

        if(cont.Id !=null)
        {
                CreateCasefromContract.createCase(cont.Id);
        }

    }
}

screen shot of the error: enter image description here

0

2 Answers 2

0

You forgot to specify a state, so the query returns no values. Add this somewhere in your unit test.

cont.State__c = 'Connecticut';

For future reference, in your live code, you should probably use try-catch blocks to abort early, or consider querying for a list and then see if it's empty. If there's an invalid state, either nothing will happen (if you don't handle the error), or the user will get a confusing message about "no rows for assignment..."

1

The issue would appear to be here:

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];

StateNumber comes from Contract.State__c that you never mock a value for in the testmethod.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .