Skip to main content
added 1549 characters in body
Source Link
Tushar Sharma
  • 29.7k
  • 7
  • 38
  • 60

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

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

Source Link
Russel baker 1
  • 377
  • 1
  • 8
  • 31

Workflow is not trigger when criteria is meeting any other approch to achive my requirments

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.