+ Start a Discussion
shakila Gshakila G 

My Batch class not executing properly

Hi All,

I created a new field in user with the data type number am trying to update the field called Total License from user license object .

to acomplish this i wrote a bacth class.But its not updating this field.

Kindly let mw know ,Where i do changes
Batch Class:
global class totallicensecount implements Database.Batchable<SObject>{
Map<ID,String> License= New Map<ID,String>();
Map<string,Integer> License1= New Map<string,Integer>();
list<user> query1=new list<user>();
set<string> setuserlicense=new set<string>();
list<user> listofuser=new list<user>();

        
        global Database.QueryLocator start(Database.BatchableContext bc){
                string query='select Id,name,TotalLicenses from Userlicense'; 
                query+='limit 1';       
                return Database.getQueryLocator(query);
        
        }
        

        global void execute(Database.BatchableContext bc,list<Userlicense> scope){
        
                for(Userlicense us:scope){
                License1.put(Us.Name,us.TotalLicenses);
                setuserlicense.add(us.name);
                }
            
                    query1=[select Id,name,profile.Userlicense.name,Profile.UserLicense.TotalLicenses,Type__C,Total_license_count__c from user where profile.Userlicense.name in :setuserlicense];
                    for(user us1:query1){
                    us1.Total_license_count__c=License1.get(us1.profile.Userlicense.name);
                    listofuser.add(us1);                   
                   
                }
            
        IF(listofuser.size()>0){
        
        update listofuser;
        
        }
}



global void finish (Database.BatchableContext bc){
}



}


 
Rajesh_KumarRajesh_Kumar
Hi Shakila G,
Remove query+='limit 1';  in your start method. Instead, run your batch like Database.executeBatch(new totallicensecount(),1);
 
shakila Gshakila G
After removing  that line also its not updating the field
DheerajKumar@SalesforceDheerajKumar@Salesforce
Hi Shakila,

In your code remove the query+='limit 1';  -> query+=' limit 1';. There is a space required before limit.

This below code might be helpful for you. In start method fetch user record so it will process on user if there are large number of user around more then 50K then below logic will work perfectly. So use below updated batch class.
 
global class totallicensecount implements Database.Batchable<SObject>{
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'select Id,name,profile.Userlicense.name,Profile.UserLicense.TotalLicenses,Total_license_count__c from user';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc,list<User> scope){
        Map<string,Integer> licenseNameToLicenseCountMap = New Map<string,Integer>();
        
        for(UserLicense userLicense : [select Id,name,TotalLicenses from Userlicense]) {
            licenseNameToLicenseCountMap.put(userLicense.Name, userLicense.TotalLicenses);
        }
        
        List<User> usersToUpdate = new List<User>();
        
        for(User user : scope) {
            if(licenseNameToLicenseCountMap.containsKey(user.profile.Userlicense.name)) {
                user.Total_license_count__c = licenseNameToLicenseCountMap.get(user.profile.Userlicense.name);
                usersToUpdate.add(user);
            }
        }
        if(usersToUpdate.size() > 0) {
            update usersToUpdate;
        }
    }
    
    global void finish (Database.BatchableContext bc){}
}
Hit Kudos if this solve you problem and if this is what you are looking for then please mark it as a solution for other benefits.

Thanks
Dheeraj Kumar
 
shakila Gshakila G
I replaced the code u shared even though its not working.

Am calling this batch class and method as a invokable method from Apex class using process builder

public class Batch_Alert_totallicensecount {
@InvocableMethod (label='Update User' )

      public static void execute(){
        
       /totallicensecount TC= New totallicensecount();
        ID IDbatch= database.executeBatch(tc);     
   
        }
        
}

But still getting fasiled