0

I have checked workflow and it is not working as I expected. According to you workflow Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria and rule criteria are stage equal to prospecting, Open task = o and last activity age greater than 12 and opportunities are meeting with criteria. My batch file is updating one custom dummy field at night so it should work because record is updating.

However let me tell you my requirement. We set some Stage timeline limit for opportunity. It is 14 days for prospecting stage and if opportunity still in prospecting stage email should go to opportunity owner.

1 Email reminder: - I want to send email reminders to opportunity owner on 13th day when opportunity is still in Prospecting stage and no open task and last activity age greater than or equal to 12.

2 Email reminder: - On 1 week after 14 days completed of opportunity create date, if opportunity is still in prospecting stage and open task equal to 0 and last activity age greater than or equal to 12.

  1. Email reminder: - On 2 week after 14 days completed of opportunity create date, if opportunity is still in prospecting stage and open task equal to 0 and last activity age greater than or equal to 12.

4 Email reminder: - On 3 week after 14 days completed of opportunity create date, if opportunity is still in prospecting stage and open task equal to 0 and last activity age greater than or equal to 12.

Can you help me to achieve this requirement through workflow because the current workflow rule is not able to catch opportunities if they are under rule criteria.

Batch code :

global class updateoppforworkflow1 implements Database.Batchable<SObject>, Database.Stateful{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' and Open_Tasks__c=0 and Last_Activity_Age__c >=12 ';
         // added below line because Last_Activity_Age__c should be >=12 which we cnt do inside test class
        if(test.isRunningTest())
        {

            Query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' ';
        }
        system.debug(query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<SObject> scope){
        for(Opportunity obj : (Opportunity[]) scope){
            if(obj.Stage_change_reminder_count__c != Null){
               obj.Stage_change_reminder_count__c = 0;
              } else 
               obj.Stage_change_reminder_count__c = 1;
            }
        system.debug('list to be updated size  :: ' + scope.size());
        if(!scope.isEmpty())
        {
              update scope;
         }
     }

    global void finish(Database.BatchableContext BC){
    }
}

enter image description here

1
  • 1
    Can you please share code of your batch and screenshot of workflow rule
    – Peeyush
    Commented Jan 21, 2016 at 7:13

1 Answer 1

0

Batch code :

global class updateoppforworkflow1 implements Database.Batchable<SObject>, Database.Stateful{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' and Open_Tasks__c=0 and Last_Activity_Age__c >=12 ';
         // added below line because Last_Activity_Age__c should be >=12 which we cnt do inside test class
        if(test.isRunningTest())
        {

            Query = 'Select Id, Name, Stage_change_reminder_count__c,StageName From Opportunity WHERE StageName = \'Prospecting\' ';
        }
        system.debug(query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<SObject> scope){
        for(Opportunity obj : (Opportunity[]) scope){
            if(obj.Stage_change_reminder_count__c != Null){
               obj.Stage_change_reminder_count__c = 0;
              } else 
               obj.Stage_change_reminder_count__c = 1;
            }
        system.debug('list to be updated size  :: ' + scope.size());
        if(!scope.isEmpty())
        {
              update scope;
         }
     }

    global void finish(Database.BatchableContext BC){
    }
}

enter image description here

@PeeyushSachdeva

5
  • as far as I can see, obj.Stage_change_reminder_count__c = 1; is always being executed, that mean that workflow criteria was true before and after the batch update, so Workflow is not fired
    – kurunve
    Commented Jan 21, 2016 at 8:26
  • So what is the resolution. What are the changes required in workflow. @kurunve Commented Jan 21, 2016 at 10:15
  • Idea would be to create 1 workflow with 4 time trigger actions, that do next thing - send an email every 7 days (like now, now+7 days, now + 14 days etc) accordingly to action. In criteria, there should be logic condition Stage_change_reminder_count__c = 1. In batch job there should be next logic: List<Opp> op_to_update = new List<Opp>(); for(Opp o : scope){ if (o.Stage_change_reminder_count__c == 0){ o.Stage_change_reminder_count__c = 1; op_to_update.add(o); } } update op_to_update; Main idea - as soon as we set flag to 1 - WF would send email as long as criteria is true.
    – kurunve
    Commented Jan 21, 2016 at 10:34
  • Hmm make sense, Thanks. I'll Do and check and if it's work, will mark as best answer. Commented Jan 21, 2016 at 10:53
  • this is not an answer and looks like an edit to the OP. Comments from @kurunve look like the answer
    – cropredy
    Commented Jan 21, 2016 at 15:37

You must log in to answer this question.

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