0

I have custom object example - Agreement and I would like to change the record type if my field type value is changed, below is my code ( I am able to save the code but while testing I am geeting error) can I please get some help here.

public static void beforeUpdateAgreement(List<AGR_Agreement__c> NewAgreement){
    Map<String, Id> typeMap = New Map<String, Id>();
    for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'AGR_Agreement__c']) {
        typeMap.put(rt.DeveloperName, rt.Id);
    }

    for(AGR_Agreement__c agmt : NewAgreement) {
        recordtype agmtRT = [select id, developername from recordtype where id = :agmt.recordtypeid];
        recordtype SART = [select id, developername from recordtype where developername ='CORE_Agreement'];
        if(agmtRT.id == SART.id){
            if(agmt.Payment_terms_type__c == 'Invoice Date') {
                id recid = typeMap.get('Non_standard_agreement');
                recordtype rectype = [select id, developername from recordtype where id = :recid];
                agmt.RecordTypeid = rectype.id; 
            }
        }
    }
}

Note - I am calling this method beforeUpdateAgreement in my handler on before update.

Below is the error Message

AGR_AgreementTriger: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject

4
  • Please edit your question to add the exact error text.
    – Moonpie
    Commented Jul 28, 2022 at 13:43
  • When you get an error, you should always include the error message (and stack trace) verbatim. Edit your question to include that (and include the line with the error in a separate code block). In this situation though, with three queries inside of a loop I'd be surprised if it isn't you running into the 101 SOQL query limit.
    – Derek F
    Commented Jul 28, 2022 at 13:44
  • Hi Derek, I have updated my question with error message can you please suggest or help with the resolution to rectify this code
    – khushi
    Commented Jul 28, 2022 at 13:53
  • Hi @Moonpie , added error message now.
    – khushi
    Commented Jul 28, 2022 at 14:03

1 Answer 1

2

Records can have a null RecordTypeId, so your code will crash if it does. Note that you should not query inside a loop. We can pull all of those queries out to the top level. Further, you can use describe calls to get the information you need without queries. You also didn't need some of the queries/describes at all. Your code can be distilled down to just:

public static void beforeUpdateAgreement(List<AGR_Agreement__c> NewAgreement){
    Map<String, RecordTypeInfo> agrAgreementRecordTypeInfos = sObjectType.AGR_Agreement__c.getRecordTypeInfosByDeveloperName();
    Id SARTId = agrAgreementRecordTypeInfos.get('CORE_Agreement').getRecordTypeId();
    Id nonStandardAgreementId = agrAgreementRecordTypeInfos.get('Non_standard_agreement').getRecordTypeId();
    for(AGR_Agreement__c agmt : NewAgreement) {
        if(agmtRT.RecordTypeId == SARTid && agmt.Payment_terms_type__c == 'Invoice Date') {
            agmt.RecordTypeid = nonStandardAgreementId.id; 
        }
    }
}

You must log in to answer this question.

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