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
nishant shrivastava 40nishant shrivastava 40 

Batch Class throwing errors for picklist field values


#Sales Cloud#Batch Apex Class

I have a batch class and it throws an error for different picklist value assignment. Invalid integer whereas those fields are picklist and should be dealt as text field.
Please check the class...
Error 1 : invalid integer for 'noch k.A' & '6-25' and so on.
Error 2 : it is also not updating the field partially. no partial success.
Error 3. It's not triggering an email from Finish method. i tried then in execute method. it does not trigger there as well.

I shall be grateful to you for your kind cooperation. Thanks in advance.

global class PicklistCleaning implements Database.Batchable<sObject>, database.stateful {
    Exception[] errors = new Exception[0];
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
        string Query = 'select NoOfEmployees__c from Account where NoOfEmployees__c !=\'noch k. A.\' AND NoOfEmployees__c !=\'1 - 5\' AND NoOfEmployees__c !=\'6 - 25\'AND NoOfEmployees__c !=\'26 - 50\' AND NoOfEmployees__c !=\'51 - 250\' AND NoOfEmployees__c !=\'251 - 500\' AND NoOfEmployees__c !=\'501 - 1.000\' AND NoOfEmployees__c !=\'1001 - 9999\' AND NoOfEmployees__c !=\'> 10.000\'';
        system.debug(Query);
            return database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Account> records){
        // process each batch of records
        List<Account> Acclist = New List<Account>();
        for(Account Acc : records){
            if(acc.NoOfEmployees__c == null || acc.NoOfEmployees__c == ''){ 
                acc.NoOfEmployees__c = 'noch k. A.';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=5 || integer.valueof(acc.NoOfEmployees__c) == 1){
                acc.NoOfEmployees__c = '1 - 5';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=25 || integer.valueof(acc.NoOfEmployees__c) >= 6){
                acc.NoOfEmployees__c = '6 - 25';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=50 || integer.valueof(acc.NoOfEmployees__c) >= 26){
                acc.NoOfEmployees__c = '26 - 50';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=250 || integer.valueof(acc.NoOfEmployees__c) >= 51){
                acc.NoOfEmployees__c = '51 - 250';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=500 || integer.valueof(acc.NoOfEmployees__c) >= 251){
                acc.NoOfEmployees__c = '251 - 500';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=1000 || integer.valueof(acc.NoOfEmployees__c) >= 501){
                acc.NoOfEmployees__c = '501 - 1.000';
            }
            if(integer.valueof(acc.NoOfEmployees__c) <=9999 || integer.valueof(acc.NoOfEmployees__c) >= 1001){
                acc.NoOfEmployees__c = '1001 - 9999';
            }
            if(integer.valueof(acc.NoOfEmployees__c) >=10000){
                acc.NoOfEmployees__c = '> 10.000';
            }
            Acclist.add(acc);
        }
        try{
           Database.upsert(Acclist, false);
           Database.SaveResult[] sr = Database.update(Acclist, false);
            for(Database.SaveResult srr :  sr){
                if(srr.isSuccess()){
                    system.debug(srr.getId());
                }
                else{
                    for(Database.Error err : srr.getErrors()){
                        system.debug(err.getFields());
                        system.debug(err.message);
                        system.debug(err.getMessage());
                    }
                }
            }
        }
        catch(system.dmlexception e){
            errors.add(e);
            if(!errors.isEmpty()){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           string[] adds = new string[]{'////@test.com///'};
            mail.setSubject('Errors occurred during batch process.');
            mail.setPlainTextBody(''+errors);
            mail.setToaddresses(adds);
             List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
             mails.add(mail);
            if(mails.size() > 0){
                Messaging.sendEmail(mails);
            }
       }
        }
    }    
    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
         if(!errors.isEmpty()){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           string[] adds = new string[]{'///////@test.com///'};
            mail.setSubject('Errors occurred during batch process.');
            mail.setPlainTextBody(''+errors);
            mail.setToaddresses(adds);
             List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
             mails.add(mail);
            if(mails.size() > 0){
                Messaging.sendEmail(mails);
            }
       }
    }    
}
Danish HodaDanish Hoda
Hi Nishant,
For the first error - 
I can see 'NoOfEmployees__c' field on Account is a Number field and you are querying for the value as a text field.
nishant shrivastava 40nishant shrivastava 40
no that's a picklist field. Danish :)