+ Start a Discussion

how to set rule criteria through trigger



I want to share the records through trigger based on workflow rule.  I mean if the rule criteria is 'when the record is create' then i use the 'after insert' event.


If the rule criterial in work flow is 'When a record is created, or when a record is edited and did not previously meet the rule criteria'. How can i set the rule criteria in trigger... Any one please help me...





Totaly unclear requirement .Be more specific possibly with an example .


Make sure you are not mixing up  Time-based workflow trigger and  APEX trigger  .




My requirement is,


I need to automate the work flow rules through triggers. in that work flow rule i am sending a email for user with sharing instructions of a record. I need to set that sharing criteria of a record through trigger.  I am doing everything fine... But i got a doubt that is, how to set the rule criteria in trigger if the work flow having ' When a record is created, or when a record is edited and did not previously meet the rule criteria'. 






Madhan Raja MMadhan Raja M

Hi Laskhmi,


Use 'before insert', 'before update' events and create a checkbox called "previously met?". This checkbox helps you to track, whether the condition is satisfied in the previous condition.



Assume, previously_met__c=FALSE;




// your logic





Madhan Raja M


You can use

Trigger.isInsert - (is Created )


Trigger.isUpdate && (sObject.Sharing_flag__c <> trigger.oldMap.get(Objectid).Sharing_flag__c)

(is edited ) and - Compare flag value(Create a custom field Sharing flag, set if Sharing Rules are set,

Note: don't add it to page layout)








Did you mean in Sharing_flag__c, shall i keep the sharing rule value like read or write somthing once edited.......





You can use the content of the variable however you want to use it, all depends on requirements.


If you are going to change the sharing rules based on some criteria in a field(like status) you can use the same field instead of additional custom field  like Sharing_flag__c.


Note: Sharing_flag__c is just a example I used, you can use the same fields and values that trigger the workflow