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
Ryan GreeneRyan Greene 

Error: Field is not writeable (but I'm not trying to write to it)

I am creating a simple trigger to update the Status field if the field 'Last user to view record' equals the Owner ID of the record and the Status currently equals 'New'. The 'Last user to view record' field is a formula which pulls the current Users ID. (So if the User ID matches the Owner ID the status will change to 'Read') What seems to be wrong?
Thanks for your help!
Ryan
trigger Read on Web_Links__c (before update) 
{
OwnerId From Web_Links__c];
    for(Web_Links__c WI : Trigger.new ){
        if(WI.Status__c = 'New'){
            if(WI.Last_user_to_view_record__c = WI.OwnerId){
                WI.Status__c = 'Read';
            }
        }
    }
}

 
Best Answer chosen by Ryan Greene
Peter FribergPeter Friberg
Hi Ryan

I think the problem is not the Srarus__c field, it is the if-statement where you are trying to set the field Status__c to 'New'. Comparisons for equality in Apex must use double equal signs. Try this instead:
for (Web_Links__c WI : Trigger.new ) {
    if (WI.Status__c == 'New') {
        if (WI.Last_user_to_view_record__c == WI.OwnerId) {
            WI.Status__c = 'Read';
        }
    }
}
Also make sure the field Status__c is a text field or a picklist field which is assignable (e.g. read/write possible on field level). If Status__c is a formula field then setting a value of Status__c is not possible.

Good luck!
 

All Answers

Peter FribergPeter Friberg
Hi Ryan

I think the problem is not the Srarus__c field, it is the if-statement where you are trying to set the field Status__c to 'New'. Comparisons for equality in Apex must use double equal signs. Try this instead:
for (Web_Links__c WI : Trigger.new ) {
    if (WI.Status__c == 'New') {
        if (WI.Last_user_to_view_record__c == WI.OwnerId) {
            WI.Status__c = 'Read';
        }
    }
}
Also make sure the field Status__c is a text field or a picklist field which is assignable (e.g. read/write possible on field level). If Status__c is a formula field then setting a value of Status__c is not possible.

Good luck!
 
This was selected as the best answer
Ryan GreeneRyan Greene
I might answer my own question!! instead of using a single '=' in the conditional I need to use a double '=='.