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
Miller 95Miller 95 

Deactivating Campaigns Using Batch Scheduler

Hi Everyone,

Not sure if I am doing this right. I would like to get your opinion. I am looking to use the batch schedule to go out and look at all my active campaigns that have a end date of today and deactivate them. I have tried to use a workflow for this with no success, so this is what I have.

global class DeactivateCampaignBatch implements Database.Batchable<sObject>{

   //Start Method
   global Database.QueryLocator start(Database.BatchableContext BC){ 
       String Query = 'SELECT EndDate FROM Campaign WHERE Campaign.EndDate=Today()';
       return Database.getQueryLocator(query);
   }

   //Execute Logic
   global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Campaign> acamps;
        
        acamps = [SELECT Id
                 FROM Campaign 
                 WHERE IsActive = true
                 limit 100];

   for(sObject s : scope){Campaign c = (Campaign)s;
        if(c.IsActive = true){
            c.IsActive=FALSE;
            acamps.add(c);
            }
        }

    update acamps;
    
   }

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

I get an error on line 6 saying incorrect token: '('.

Please Help!!!
Best Answer chosen by Miller 95
Boris BachovskiBoris Bachovski
You don't need to use the brackets (), that's the apex function. Use TODAY instead. Here you can find all SOQL date literals http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

All Answers

Boris BachovskiBoris Bachovski
You don't need to use the brackets (), that's the apex function. Use TODAY instead. Here you can find all SOQL date literals http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
This was selected as the best answer
Miller 95Miller 95
Brilliant Boris, that worked. I had to modify the batch file a little because it was creating to list, but here is the working code:

global class DeactivateCampaignBatch implements Database.Batchable<sObject>{
String Query = 'SELECT EndDate FROM Campaign WHERE Campaign.EndDate=Today';
    
   //Start Method
   global Database.QueryLocator start(Database.BatchableContext BC){ 
       return Database.getQueryLocator(query);
   }

   //Execute Logic
   global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Campaign> acamps = new List<Campaign>();

   for(sObject s : scope){Campaign c = (Campaign)s;
        if(c.IsActive = true){
            c.IsActive=FALSE;
            acamps.add(c);
            }
        }

    update acamps;
    
   }

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

Thanks again for your help!