function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ss kumarss kumar 

CaseDemoRecordType: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.CaseDemoRecordType: line 17, column 1

trigger CaseDemoRecordType on Case (After insert , After update)
{
    if((trigger.isAfter) && (trigger.isinsert || trigger.isupdate))
    {
    List<RecordType> recordTyp = [Select id , Name , Sobjecttype from RecordType];
    List<Case> LstCs = [select id , CaseNumber , RecordTypeId from case ]; 
    
    For(Case cs : Trigger.new)
    {
        For(RecordType RecTyp : recordTyp )
        {
            
          If(cs.RecordTypeID == RecTyp.id)
        {
            if(RecTyp.Name == 'School zone')
            {
                cs.Description = 'This is SCHOOL ZONE';
                system.debug('This is SCHOOL zONE^^^^^'+RecTyp.Name);
            }
            else
                cs.Description = 'This is COLLEGE ZONE';
                system.debug('This is COLLEGE zONE......'+RecTyp.Name);
            
            system.debug('The REcord Type Ids..............'+cs.RecordTypeId);

        }
        }
                            update cs;

    }

    }
}
SwethaSwetha (Salesforce Developers) 
HI Kumar,
I can see that you are trying to update the Case record within a loop, which is not allowed. Salesforce does not allow updating records within a loop in an After trigger context.

To resolve the issue, you need to move the update statement outside of the loop.
trigger CaseDemoRecordType on Case (After insert, After update) {
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
        List<RecordType> recordTypes = [SELECT Id, Name, SObjectType FROM RecordType];
        List<Case> casesToUpdate = new List<Case>();

        for (Case cs : Trigger.new) {
            for (RecordType recType : recordTypes) {
                if (cs.RecordTypeId == recType.Id) {
                    if (recType.Name == 'School zone') {
                        cs.Description = 'This is SCHOOL ZONE';
                        System.debug('This is SCHOOL ZONE: ' + recType.Name);
                    } else {
                        cs.Description = 'This is COLLEGE ZONE';
                        System.debug('This is COLLEGE ZONE: ' + recType.Name);
                    }
                    System.debug('The Record Type Id: ' + cs.RecordTypeId);
                }
            }
            casesToUpdate.add(cs);
        }

        if (!casesToUpdate.isEmpty()) {
            update casesToUpdate;
        }
    }
}

If this information helps, please mark the answer as best. Thank you
Abdul KhatriAbdul Khatri
Hi Kumar,

I see too many weaknesses in your code, here are some
  • If you are updating the same SObject in the trigger, always try to use Before vs Insert
  • To Retrieve the Recordtype, always try to use the standard feature instead of SOQL as it doesn't count against your SOQL Limit.
  • In case you use SOQL, always try to use the WHERE Clause and make sure you protect your code using static variables by reoccurrence.
  • Always try to minimize the use of for loop and if checks in your code

Here is the code, I have commented the lines not needed for your reference. I hope this will reduce your code to half.
trigger CaseDemoRecordType on Case (before insert, before update) {
    
    if((trigger.isBefore) && (trigger.isInsert || trigger.isUpdate))
    {
        Id recTypeIdCaseSZ = Schema.SObjectType.Case.getRecordTypeInfosByName().get('School zone').getRecordTypeId();       
        //List<RecordType> recordTyp = [Select id , Name , Sobjecttype from RecordType];
        
        //List<Case> LstCs = [select id , CaseNumber , RecordTypeId from case ]; 
        
        For(Case cs : Trigger.new)
        {
            //For(RecordType RecTyp : recordTyp )
            //{
            
            //If(cs.RecordTypeID == RecTyp.id)
            if(cs.RecordTypeId == recTypeIdCaseSZ)
            {
                //if(RecTyp.Name == 'School zone')
                //{
                cs.Description = 'This is SCHOOL ZONE';
                //system.debug('This is SCHOOL zONE^^^^^'+RecTyp.Name);
            }
            else
            {
                cs.Description = 'This is COLLEGE ZONE';
                //system.debug('This is COLLEGE zONE......'+RecTyp.Name);
                
                system.debug('The REcord Type Ids..............'+cs.RecordTypeId);
                
            }
            
            //update cs;
            
        }
        
    }    
}
I am surprised how people with good scores provide directions here

I hope my explanation and suggestion help you in the future. 

Regards,
Abdul Aziz Khatri