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
Peter CowenPeter Cowen 

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
}
}
}
}
Best Answer chosen by Peter Cowen
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- you can not update the same record in After event. So please change your code and remove After update event
trigger UpdateServiceContract on Case (before insert,before 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 = :c.AccountId and Primary_Service_Contract__c = True limit 1].id;
			}catch(QueryException e) {
			}
		}
	}
}
Let us know if this will help you
 

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Peter,

Please try the following code:
 
trigger UpdateServiceContract on Case (before insert,before 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 = :c.AccountId and Primary_Service_Contract__c = True limit 1].id;
				}catch(QueryException e) {
				//No records found. Maybe you should set it to Null
				}
			}
		}
	}
}

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
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- you can not update the same record in After event. So please change your code and remove After update event
trigger UpdateServiceContract on Case (before insert,before 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 = :c.AccountId and Primary_Service_Contract__c = True limit 1].id;
			}catch(QueryException e) {
			}
		}
	}
}
Let us know if this will help you
 
This was selected as the best answer