-2

I have a process builder in case which set NOW() value for "DateTimeA" field if the status is closed. Also I have a code in before update trigger , which checks the difference between current datetime and DateTimeA field. If the difference is less than 5 days , then DateTimeA field should be set null. But this is not happening.

for(Case cs : casDetailsList) {
  if (cs.DateTimeA__c != null && (cs.now().getTime() - cs.DateTimeA__c.getTime() < 5L * 24 * 60 * 60 * 1000)) {
          System.Debug('Inside Loop');
          cs.DateTimeA__c = null;
          System.Debug('Value once updated' + cs.DateTimeA__c);
  }
}  

I'm getting caseDetailsList from trigger.New in update method of case trigger.Also I checked in my debug log. It is coming inside the loop and even the value is printed as null. But I dont know why it is not reflecting in the record. Without reflecting , it is setting the value for that DateTimeA field to current datetime.

  • Can you share where the casDetailsList is coming from? – manjit5190 Feb 21 '18 at 13:26
  • please find my edited question @manjit – ram135868 Feb 21 '18 at 13:29
  • Possible that after the trigger update, the process builder fire again (depend on the setup in the process). It will be much easier if you will combine those logic. Either put both in the process builder or both in the trigger. – Liron C Feb 21 '18 at 13:33
  • Actually the weird thing is , though I deactivated the process builder , the value I'm going to modify is reflected in the record instead of null – ram135868 Feb 21 '18 at 13:43
4

you are using complicated (probably wrong )way to calculate number of days between two dates. Use Datetime.date() and Date class to calculate it

for(Case cs : casDetailsList) {
    if (cs.DateTimeA__c != null && cs.DateTimeA__c.date().daysBetween(Date.today()) < 5) {
          System.Debug('Inside Loop');
          cs.DateTimeA__c = null;
          System.Debug('Value once updated' + cs.DateTimeA__c);
    }
} 
  • Thanks Alexander. But the problem here is the null value is not being set when using this code , not the calculation – ram135868 Feb 21 '18 at 13:38
0

In your Process Builder, in the diamond condition, double check that you are doing one of the two things:

  1. Your condition specifically checks for the PRIORVALUE of the Status field, such that the Process only fires when there is a change from any other value to 'Closed'; or

    AND(TEXT(PRIORVALUE([Case].Status)) != 'Closed', TEXT([Case].Status) = 'Closed')

  2. In this diamond definition, expand Advanced on the bottom and make sure to check 'Yes' to 'Do you want to execute the actions only when specified changes are made to the record?', and keep the formula condition as simple as

    TEXT([Case].Status) = 'Closed'

I suspect that your Process is always running on all Cases whose Status is 'Closed'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.