You need to sign in to do that
Don't have an account?

Record is Read Only error on trigger
I have had a few instances where i recieve the following error:
UpdateServiceContract: execution of AfterUpdate
caused by: System.FinalException: Record is read-only
Trigger.UpdateServiceContract: line 8, column 1
Is there a way of setting the trigger to check if the field is read only and if it is make it writeable before applying the update?
The trigger is :
trigger UpdateServiceContract on Case (before insert,before update,after update) {
Id caseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id ;
for (Case c : trigger.new) {
if (c.RecordTypeID == caseRecordTypeId )
if (c.service_contract__c == NULL){
try{
//c.Service_Contract__c = [select Id from ServiceContract where AccountId ='810D0000000Cfza' and Primary_Service_Contract__c = True].id;
c.Service_Contract__c = [select Id from ServiceContract where AccountId = :c.AccountId and Primary_Service_Contract__c = True limit 1].id;
}catch(QueryException e) {
//No records found. Maybe you should set it to Null
}
}
}
}
UpdateServiceContract: execution of AfterUpdate
caused by: System.FinalException: Record is read-only
Trigger.UpdateServiceContract: line 8, column 1
Is there a way of setting the trigger to check if the field is read only and if it is make it writeable before applying the update?
The trigger is :
trigger UpdateServiceContract on Case (before insert,before update,after update) {
Id caseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id ;
for (Case c : trigger.new) {
if (c.RecordTypeID == caseRecordTypeId )
if (c.service_contract__c == NULL){
try{
//c.Service_Contract__c = [select Id from ServiceContract where AccountId ='810D0000000Cfza' and Primary_Service_Contract__c = True].id;
c.Service_Contract__c = [select Id from ServiceContract where AccountId = :c.AccountId and Primary_Service_Contract__c = True limit 1].id;
}catch(QueryException e) {
//No records found. Maybe you should set it to Null
}
}
}
}
All Answers
Please try the following code:
Please let me know how it works out for you. Mark it Solve if this answers your query so that others can view it as a proper solution.
Thanks,
Apoorv