1

I am trying change the case ownerid to Queue but getting System.FinalException: Record is read-only error in after insert.

Below is the my code:

if(Trigger.isInsert && Trigger.isAfter)
        {    
            Map<String, Id> mapQueues = new Map<String, Id>();
            for(Group grp: [select Id, name, Type from Group where Type = 'Queue']){
                mapQueues.put(grp.name, grp.id);
            }

            for(Case caseObject : Trigger.New)
            {
                if(caseObject.Complaint_type__c == 'Complaint About Services Through Centers'){
                    caseObject.OwnerId = mapQueues.get('Licensing Department');
                }
                else if(caseObject.Complaint_type__c=='Consumer Protection Complaint'){
                    caseObject.OwnerId = mapQueues.get('Monitoring and Consumer Protection');
                }
                else if(caseObject.Complaint_type__c=='Online Services'){
                    caseObject.OwnerId = mapQueues.get('IT Department');
                }
                else {
                    caseObject.OwnerId = mapQueues.get('Support Team');
                }
            }
}
1
1

Record is read only in after trigger context. Refer Context Variable Considerations

So, you need to separately query the case records to update. Here is the approach.

if(Trigger.isInsert && Trigger.isAfter)
{    
    Map<String, Id> mapQueues = new Map<String, Id>();
    for(Group grp: [select Id, name, Type from Group where Type = 'Queue']){
        mapQueues.put(grp.name, grp.id);
    }

    List<Case> lstCase = [SELECT id, Complaint_type__c, OwnerId FROM Case WHER Id IN:Trigger.NewMap.keyset()];

    for(Case caseObject : lstCase)
    {
        if(caseObject.Complaint_type__c == 'Complaint About Services Through Centers'){
            caseObject.OwnerId = mapQueues.get('Licensing Department');
        }
        else if(caseObject.Complaint_type__c=='Consumer Protection Complaint'){
            caseObject.OwnerId = mapQueues.get('Monitoring and Consumer Protection');
        }
        else if(caseObject.Complaint_type__c=='Online Services'){
            caseObject.OwnerId = mapQueues.get('IT Department');
        }
        else {
            caseObject.OwnerId = mapQueues.get('Support Team');
        }
    }
    update lstCase;
}
0

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.