You need to sign in to do that
Don't have an account?
shakila 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){
}
}
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){
}
}
Remove query+='limit 1'; in your start method. Instead, run your batch like Database.executeBatch(new totallicensecount(),1);
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.
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
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