You need to sign in to do that
Don't have an account?
ss 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;
}
}
}
{
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;
}
}
}
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.
If this information helps, please mark the answer as best. Thank you
I see too many weaknesses in your code, here are some
Here is the code, I have commented the lines not needed for your reference. I hope this will reduce your code to half. 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