0

I have a custom setting name "StateAbbreviationsInfo" which has fields of Statecode__c and Region__C. The requirement is to create a batch class and check for the lead statecode and find the appropriate Region from the custom setting and update it on the Owner_Division_Region__c on lead.

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

    global Database.QueryLocator start(Database.BatchableContext BC) {

     Set<Id> lset = new Set<Id>();
     lset.add(Schema.SObjectType.Lead.getRecordTypeInfosByName().get('ABC').getRecordTypeId());
     string query ='Select id,name,Owner_Division_Region__c from Lead where RecordTypeId IN :lset';
     return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<SObject> Scope){
   Map<String,StateAbbreviations__c> stateabb = StateAbbreviations__c.getAll();
   List<Lead> listToUpdate = new List<Lead>();
   for(Lead ld : (Lead[]) Scope){
   if(ld.id!=null){

   if(StateAbbreviations__c.containsKey(Lead.Statecode))
   {
   ld.Owner_Division_Region__c = stateabb.get(Statecode).Region__c;
   listToUpdate.add(Id);

   }

  }

  }
    if(listToUpdate.size() > 0){
    Database.Update(listToUpdate);
    }
   }
global void finish(Database.BatchableContext BC){

    }

    global void execute(SchedulableContext ctx) {
        database.executeBatch(new UpdateRegion());
   }

}
  • What have you tried so far? Are you stuck somewhere? – Pranay Jaiswal Mar 11 at 19:17
  • @PranayJaiswal please see the above code. I am positive that my assignment is not right – snesalesforce Mar 11 at 19:35
  • Is this the correct code, does this compile? I see listToUpdate.add(<i in UPPER CASE>d);, is that a typo and that it should be listToUpdate.add(<L in LOWER CASE>d); instead? – Jayant Das Mar 11 at 19:45
  • @PranayJaiswal im seeing the following compile error : Error: Compile Error: Class UpdateRegion must implement the method: System.Iterable<Lead> Database.Batchable<Lead>.start(Database.BatchableContext) at line 1 column 14 – snesalesforce Mar 11 at 19:55
  • It should be <sObject?> – Pranay Jaiswal Mar 11 at 20:00
1

So the problem is in line

if(StateAbbreviations__c.containsKey(Lead.Statecode))

You already made a map of String vs StateAbbreviations__c above as Map<String,StateAbbreviations__c> stateabb . You have to use contains in it.

Secondly,Lead.Statecode is a field token, not the actual value of lead's Statecode. To get actual value of lead's statecode you have to use ld.Statecode

As you are using statecode field , you have to include that in your SOQL as well.

global class UpdateRegion implements Database.Batchable<sObject>  {

    global Database.QueryLocator start(Database.BatchableContext BC) {

     Set<Id> lset = new Set<Id>();
     lset.add(Schema.SObjectType.Lead.getRecordTypeInfosByName().get('ABC').getRecordTypeId());
     string query ='Select id,name,Owner_Division_Region__c , Statecode from Lead where RecordTypeId IN :lset';
     return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Lead> leadList){
        Map<String,StateAbbreviations__c> stateabb = StateAbbreviations__c.getAll();
        List<Lead> listToUpdate = new List<Lead>();
        for(Lead ld : leadList){

               if(stateabb.containsKey(ld.Statecode)){
                ld.Owner_Division_Region__c = stateabb.get(ld.Statecode).Region__c;
                listToUpdate.add(ld);
            }

        }
        if(listToUpdate.size() > 0){
            Database.Update(listToUpdate);
        }
   }
    global void finish(Database.BatchableContext BC){

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.