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
Vianney Bancillon 8Vianney Bancillon 8 

workflow and apex class

Hello everybody !
I recently set up a workflow that works in the following way :
  •  ‘Opportunity’ object that has a field ‘‘Number of days before finalisation’’. This is a formula field that calculates automatically based on the date field ‘‘Commitment to order finalisation’’. This is the formula: Number of days before finalisation= Commitment to order finalisation – TODAY()
  • When Number of days before finalisation = -1, the workflow sends an email to the opportunity owner
 
My tests are OK ; however, a colleague has told me that this is the case because I changed the ‘‘Commitment to order finalisation’’ field.
In my mind , the following will happen: Number of days before finalisation is updated automatically everyday by the formula , which will at some point return ‘‘-1’’. In this case: FIRE! The WF is set off and sends an email.
In my colleague’s opinion, the modification of the Number of days before finalisation field is not interpreted by SalesForce as an object update, and therefore won’t set off the WF.
It seems that an Apex class that updates my record is necessary.
Could you explain me why? I can't understand the logic.
Thanks a lot.

Have a good day.

Vianney BANCILLON
 
Best Answer chosen by Vianney Bancillon 8
RD@SFRD@SF
Hi Vianney,

Your colleague is right. I have faced a similar confusion.

1. The formula values are not saved, infact they are calculated every time they are accessed.
2. The workflow which you have setup is perfectly fine. But the workflow is set to run when the evaluation criteria is satisfied.
     i.e. When a record is created, edited or subsequently edited to meet the criteria.

Note: even if the formula value is going to be "-1" the workflow wont run to check the condition as the record is not getting updated.Basically no event is happening.

In cases like these time triggers comes in handy, update the same workflow with a time trigger to "Commitment to order finalisation" assuming it is a date field. Setup the email under this time trigger. What would happen here is that when a new record is created a time event would be scheduled to send a mail on the "Commitment to order finalisation" date.

Please follow the link (https://help.salesforce.com/articleView?id=000005245&type=1)to learn more about time triggers.

Hope it helps
RD

All Answers

RD@SFRD@SF
Hi Vianney,

Your colleague is right. I have faced a similar confusion.

1. The formula values are not saved, infact they are calculated every time they are accessed.
2. The workflow which you have setup is perfectly fine. But the workflow is set to run when the evaluation criteria is satisfied.
     i.e. When a record is created, edited or subsequently edited to meet the criteria.

Note: even if the formula value is going to be "-1" the workflow wont run to check the condition as the record is not getting updated.Basically no event is happening.

In cases like these time triggers comes in handy, update the same workflow with a time trigger to "Commitment to order finalisation" assuming it is a date field. Setup the email under this time trigger. What would happen here is that when a new record is created a time event would be scheduled to send a mail on the "Commitment to order finalisation" date.

Please follow the link (https://help.salesforce.com/articleView?id=000005245&type=1)to learn more about time triggers.

Hope it helps
RD
This was selected as the best answer
Vianney Bancillon 8Vianney Bancillon 8
Hi RD!
Thanks a lot for you answer. I think the light is coming!
You said in your previous post:
"1. The formula values are not saved, infact they are calculated every time they are accessed."

Does it mean that the formula works like a simple "display"?
Thanks



 
RD@SFRD@SF
Hi Vianney,

You could say that yes in a way, as the formula field are only visible in detail pages and not in edit or new record create pages

Regards,
Deepak
Vianney Bancillon 8Vianney Bancillon 8
Thanks RD.
"In cases like these time triggers comes in handy, update the same workflow with a time trigger to "Commitment to order finalisation" assuming it is a date field. Setup the email under this time trigger. What would happen here is that when a new record is created a time event would be scheduled to send a mail on the "Commitment to order finalisation" date."

I tried to set up a time trigger.
Maybe I'm wrong but if I want the Workflow to send a mail daily, I have to add a time trigger for each day...
- 1 day after Commitment to order finalisation > send a mail
- 2 days after...> send a mail

In theory, my workflow should send the mail each day without limits... so I should add a time trigger indefinitely...

Thanks for your help!

 
RD@SFRD@SF
Happy to help.

Please mark as best answer if it solved the issue.

Let me know if you hit anything else regarding this.

RD
Vianney Bancillon 8Vianney Bancillon 8
Ok thanks Deepak.
My previous post is not so clear.
I just want to know if there is another way to trigger my workflow daily without adding a time trigger for each day?
Maybe I have to create another topic. However it is about the same issue.
thanks for your help.
Vianney
 
RD@SFRD@SF
Hi Vianney,

There is a way of doing it, it involves apex. And we would have to schedule the apex code, to run daily.

Hope it helps
RD
Vianney Bancillon 8Vianney Bancillon 8
Ok RD I took your advice. I tried to write an Apex Class to update my opportunities, and a Class that implements Schedulable interface. My tests in Sandbox seem good.
I have created several Opportunities in production in order to control it runs right. But I didn't receive the e-mail form the workflow this night...

Here is the code of the batch:
 
global class batchOpportunitiesUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query='Select StageName from Opportunity';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<Opportunity> scope){  
        List<Opportunity> oppToUpdate = new List<Opportunity>();
        for(Opportunity o : scope){
            if(o.StageName=='Au planning de livraison'){
              oppToUpdate.add(o); 
            }
            else if(o.StageName=='Révision en cours'){
                oppToUpdate.add(o);
            }
            else if(o.StageName=='Finalisation de commande'){
                oppToUpdate.add(o);
            }
        }
        update oppToUpdate;
    }
    
    global void finish(Database.BatchableContext BC){}
}

Here is the code of the "Schedulable Class":
 
global class scheduledOpportunityBatch implements Schedulable {
    global void execute(SchedulableContext sc){
        batchOpportunitiesUpdate b = new batchOpportunitiesUpdate();
        database.executeBatch(b);
    }
}


And the screenshot of the scheduled task:


Scheduled task


What is wrong in my development?

Thanks a lot for your help!

Vianney
 
Vianney Bancillon 8Vianney Bancillon 8
Please, someone to help me!
;-)
Have a good day.
Vianney BANCILLON