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
SimrinSimrin 

Functions in trigger

Hello,
Can i use fucntions in trigger. I have a piece of code which is executed many times. 

 

 

Best Answer chosen by Simrin
ManojjenaManojjena
Hi Simrin ,

You can write method inside  trigger .Basically if you  write private static method inside trigger will be good as we can not call the  trigger method from out side  the trigger .

You can test below example :
 
trigger AccountTrigger on Account(before Insert ){

  
   private static string getCurrentUserProfileName(){
        return [SELECT Name FROM Profile where id = :Userinfo.getProfileId()].Name;
    }
}

You can use thid method to check the profile name .

Let me know if it helps .

Thanks 
Manoj





 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
If you want to re-use function in trigger and other classes as well then please create one Trigger , Ond Handler class and one Trigger Action class
Please see below code for example :-
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please let us know if this will help you

Thanks
Amit Chaudhary
ManojjenaManojjena
Hi Simrin ,

You can write method inside  trigger .Basically if you  write private static method inside trigger will be good as we can not call the  trigger method from out side  the trigger .

You can test below example :
 
trigger AccountTrigger on Account(before Insert ){

  
   private static string getCurrentUserProfileName(){
        return [SELECT Name FROM Profile where id = :Userinfo.getProfileId()].Name;
    }
}

You can use thid method to check the profile name .

Let me know if it helps .

Thanks 
Manoj





 
This was selected as the best answer