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
Sfdc SivaSfdc Siva 

Help to creating BatchApex

Hi All,
Can any one help for creating batch apex using below requiremet.
We have three fields in Accounts.Those are 
     1. "Type" equals "Fd Gov't","LL Gov't","ST Gov't" 
      2. " Entity" Flag equals False 
       3. "Structure" equals " Agency ".
    
 If Type = "Fd Gov't","LL Gov't","ST Gov't"  And  "Structure" equals "Agency"  then  "Entity" Flag Should be "False".
    
Please help me to create a batch apex for above senario.

Thaks in Advance,
 
Nagendra ChinchinadaNagendra Chinchinada
Hi Sive,

Here is the batch,
global class BatchEntityUpdate implements Schedulable,Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){        
        String query='SELECT Id,Name,Tpe__c,Structure__c,Entity__c FROM Account WHERE Tpe__c IN (\'Fd Gov\\'t\',\'LL Gov\\'t\',\'ST Gov\\'t\') AND  Structure__c = 'Agency' AND Entity__c <> false ';     
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope){ 
       
        for(Account acc : scope){
		acc.Entity__c = false; // Update the field
		}		
		Update scope;
    }       
    
    global void finish(Database.BatchableContext BC){        
       
    }
}

Please let me know if it helps.
Sfdc SivaSfdc Siva
Hi Nagendra,

Thanks for Help.But am facing one error like " line breaks not allowed in string literals at line 3 column -1".The problem is in SOQL .Can you please help me on that.

Thanks
Nagendra ChinchinadaNagendra Chinchinada
Siav,
Here is the corrected code,
global class BatchEntityUpdate implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){        
        String query='SELECT Id,Name,Type,Structure__c,Entity__c FROM Account WHERE Type IN (\'Fd Gov\\\'t\',\'LL Gov\\\'t\',\'ST Gov\\\'t\') AND  Structure__c = \'Agency\' AND Entity__c <> false ';     
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope){ 
       
        for(Account acc : scope){
		acc.Entity__c = false; // Update the field
		}		
		Update scope;
    }       
    
    global void finish(Database.BatchableContext BC){        
       
    }
}

 
Sfdc SivaSfdc Siva
Hi Nagendra Prasad,

With out error program was successfully saved.Before running the Batch i want to check the recodrs in workbench when i am trying same SOQL"
SELECT Id,Tpe__c,Entity__c,Structure__c FROM Account WHERE Tpe__c =  (\'LL Gov\\\'t\',\'ST Gov\\\'t\',\'Fd Gov\\\'t\') AND Structure__c = 'Agency' AND Entity__c <> false'  "  Facing error like
"MALFORMED_QUERY: Structure__c FROM Account WHERE Tpe__c = (\'LL Gov\\\'t\',\'ST ^ 
ERROR at Row:1:Column:108  Bind variables only allowed in Apex code".


Can you please help me on this Issue.

Thanks for All your help



 
Nagendra ChinchinadaNagendra Chinchinada
The query mentioned in the batch class is formed as string which will not run in workbench. Modify the query as below.

SELECT Id,Name,Type,Structure__c,Entity__c FROM Account WHERE Type IN ('Fd Gov\'t','LL Gov\'t','ST Gov\'t') AND  Structure__c = 'Agency' AND Entity__c <> false

And also for the fields mentioned in query, uneed to replace the API names of the fields. I just appended __c to names u gave. For example,for Structure field, get the API name from Account fields list and replce it(in both above query and SOQL query in the batch).

Please select the above code as the best answer if it is the solution of ur issue, so thet it will help others also who r searching for the same issue.