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 

Trigger development - Please Help

I am trying to get a Status field to update when two other fields equal. I am not getting any errors with my code, however it does not seem to be working properly. Please take a look and let me know your thoughts!
Thank you,
Trigger

trigger Read on Web_Links__c (before update){
    StatusRead.Read(Trigger.new);
}


Class

public class StatusRead {
    public static void Read(List<Web_Links__c> WI){
        Web_Links__c WL = [SELECT status__c FROM Web_Links__c];
            if(WL.Owner_ID__c == WL.Last_user_to_view_record__c){
                WL.status__c = 'Read';
                update WL;
           }
    }
}

 
Best Answer chosen by Ryan Greene
Akhil AnilAkhil Anil
Hi Ryan,

All that you need is this. Since it's a pretty straightforward one, you don't need to build the logic in a separate class. Also, when trying to update in before update mode, Trigger.New is writeable which means you can directly make the changes in the List and need not perform a separate DML operation. Kindly mark it as an answer if this is what you were looking for !
 
trigger Read on Web_Links__c (before update){
    
	for(Web_Links__c w:Trigger.New) {
	    
		if(w.Owner_ID__c == w.Last_user_to_view_record__c)
		   w.Status__c = 'Read';
	}

}

 

All Answers

Akhil AnilAkhil Anil
Hi Ryan,

All that you need is this. Since it's a pretty straightforward one, you don't need to build the logic in a separate class. Also, when trying to update in before update mode, Trigger.New is writeable which means you can directly make the changes in the List and need not perform a separate DML operation. Kindly mark it as an answer if this is what you were looking for !
 
trigger Read on Web_Links__c (before update){
    
	for(Web_Links__c w:Trigger.New) {
	    
		if(w.Owner_ID__c == w.Last_user_to_view_record__c)
		   w.Status__c = 'Read';
	}

}

 
This was selected as the best answer
Ryan GreeneRyan Greene
Trigger works, thank you. 

However, what I am trying to accomplish is when a user enters a record they own I need the status to change. For that, the trigger is not working unless the user updates something in the record. How can I get this trigger to work with a user not doing anything to the record? I modified the trigger slightly to reflect.
 
trigger Read on EFI_Web_Links__c (before update){
    for(EFI_Web_Links__c w:Trigger.New) {
        if(w.Owner_ID__c == String.valueOf(UserInfo.getUserId()).substring(0, 15))
           w.Status__c = 'Read';
    }
}

 
Akhil AnilAkhil Anil
Hi Ryan,

Triggers are invoked only when a record is modified. So no matter what, unless a record is changed, the trigger cannot be invoked.