I have criteria in the workflow which need to convert to apex code. Here is the WF rule and criteria to convert.

Rule Criteria: Opportunity: Status EQUALS Service

Evaluation Criteria: Evaluate the rule when a record is created, and any time it's edited to subsequently meet criteria

In apex class, I written logic like below:

   if(opp.status = 'Service'){
        opp.count__c = 0;

but when updating the record though criteria are already met, this condition is always executing and updating the record. How to make this not execute if already satisfied and updated. Please help me.

1 Answer 1


You just need to check the old value first:

for(Integer index = 0; index < Trigger.size; index++) {
  if(Trigger.new[index].Status__c == 'Service' && 
      Trigger.new[index].Status__c != Trigger.old[index].Status__c) {
    // Logic here
  • what is Trigger.size here ?
    – RKCY
    Commented Jun 26, 2022 at 21:21
  • @RKCY It's the number of records in the current transaction context. It is the same as Trigger.new.size()/Trigger.old.size(), etc.
    – sfdcfox
    Commented Jun 26, 2022 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .