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
Brookesy--Brookesy-- 

Trigger writing out current UserId and a Date

Im trying to create a simple trigger that writes out the UserId and Date (of the user triggering the trigger) to 2 custom fields.

 

Firstly is it possible to write out the user of the person triggering the trigger?

 

Secondly would something like this work? Trying to get it to trigger when the SS gets changed to 2 - Contact Made. I apologize for my shoddy coding, still learning! :)

 

trigger userForSS2 on Account (after update) { 

 

if(Trigger.isUpdate & myAccount.Sales_Stage__c == '2 - Contact Made'){ myAccount.SQL_User__c = UserInfo.getUserId(); myAccount.SQL_Date__c = date.Today();

}

}

 

 

 

Any help would be great!

Michael 

 

Best Answer chosen by Admin (Salesforce Developers) 
AkiTAkiT

Absolutely possible.

 

could do the trigger before insert & update with a code something like

 

...for(Account myAccount : Trigger.new){ if(myAccount.Sales_Stage__c == '2 - Contact Made'){ myAccount.SQL_User__c = UserInfo.getUserId(); myAccount.SQL_Date__c = system.today(); }}...

 

 

 

All Answers

sforce2009sforce2009
You can do. The Best practice is do a bulk handling
AkiTAkiT

Absolutely possible.

 

could do the trigger before insert & update with a code something like

 

...for(Account myAccount : Trigger.new){ if(myAccount.Sales_Stage__c == '2 - Contact Made'){ myAccount.SQL_User__c = UserInfo.getUserId(); myAccount.SQL_Date__c = system.today(); }}...

 

 

 

This was selected as the best answer
Brookesy--Brookesy--

Thanks for the swift responses guys!!

 

I managed to write some code quite similar to what you pasted below AT. But i was getting this error:

 

Error:Apex trigger userForSS2 caused an unexpected exception, contact your administrator: userForSS2: execution of AfterUpdate caused by: System.Exception: Record is read-only: Trigger.userForSS2: line 6, column 1 

 

Reading the error obviously gave me the answer :) After Update wasnt a good idea!!

 

Thanks again for your help!

 

Michael