0

I want to auto-close all the opportunities 60 days after Action_By_date__c and Stage is Imported. Opportunity must be closed by system where Stage = System Closed and Closed reason = Expired. When I try to push code it says Expression cannot be assigned due to "opp.Action_By_Date__c = 'opp.Action_By_Date__c + 60'". I am not sure how could I check for opportunities 60 days after Action_By_date__c.

public class CloseOpportunityBatch implements Database.Batchable<SObject> {
   public Database.QueryLocator start(Database.BatchableContext bc) {
      return Database.getQueryLocator(
         'SELECT Id, StageName, Action_Date__c, Closed__c' +
         'FROM Opportunity' +
         'WHERE StageName =\'Imported\' '
    );
}

public void execute(Database.BatchableContext bc, List<Opportunity> opportunity) {
    for( Opportunity opp : opportunity ) {
        if (opp.StageName == 'Imported' && opp.Action_Date__c = 'opp.Action_Date__c + 60' ) {
            opp.StageName = 'System Closed';
            opp.Closed__c = 'Expired';
        }
        update opp;
    }
}

public void finish(Database.BatchableContext bc) {
}

}

1
  • this could of course be done by Record-triggered flows w/ Scheduled Paths
    – cropredy
    Commented Dec 6, 2021 at 23:39

3 Answers 3

1

You are abusing batch architecture by post filtering in your execute block. You should put all of your criteria in your start method wherever possible. Move your filtering to SOQL. Notice that you actually check for StageName = 'Imported' in the query, so post-filtering that in your execute block is already redundant.

Also, I highly recommend sticking to static queries wherever possible. Only use dynamic SOQL as a last resort.

public Database.queryLocator start(Database.BatchableContext context)
{
    return Database.getQueryLocator([
        SELECT Id FROM Opportunity
        WHERE StageName = 'Imported'
        AND Action_By_Date__c < LAST_N_DAYS:60
    ]);
}
public void execute(Database.BatchableContext context, List<Opportunity> records)
{
    for (Opportunity record : records)
    {
        // no further filtering required
    }
}
1
  • Thanks @Adrian Larson! Actually I am new to Batch Apex and learning as it is my first task but Really Appreciate your effort for correcting me and will try and implement this. Thanks!
    – sfdx007
    Commented Dec 6, 2021 at 17:01
2

This condition:

opp.Action_By_Date__c = 'opp.Action_By_Date__c + 60' 

has two flaws.

  1. It's written as an assignment statement (=) rather than a comparison (==). That's the immediate source of your error.
  2. The right-hand side of the comparison is a string literal, not an expression. It sounds like what you want is to compare the stored date to today's date to determine if it is more than 60 days old.

What you need is probably something like

opp.Action_By_Date__c.addDays(60) < Date.today()
2
  • Thanks @David Reed! It worked. Thanks a lot.
    – sfdx007
    Commented Dec 6, 2021 at 16:55
  • It kind of misses the big picture though.
    – Adrian Larson
    Commented Dec 6, 2021 at 16:55
0

use following code

Action_By_Date__c >= Action_By_Date__c .addDays(60);
1
  • if your batch deleting opportunity on daily basis Commented Dec 6, 2021 at 16:48

You must log in to answer this question.

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