You need to sign in to do that
Don't have an account?

Batch Class to update a custom field on User object(i.e: count the number of accounts at account owner level)
It is urgent requiremnet,
count the number of accounts at account owner level.
I mean the accounts they are the ownerof. the requiremnet is only count of accouts not a name.
for example: account, contain 100 records on same account owner. then this count of 100 sholud be display on that Users custom field.
Here is the code,
global class NumberOfBPsClass implements Database.Batchable<sObject> {
List<Account> acc= new List<Account>();
//String query;
Set<Id> userIds =new Set<Id>();
User currentUser=new User();
//currentUserId = UserInfo.getUserId();
//List<User> currentUser=[SELECT Number_of_Business_Partners__c,Number_of_BPs_in_Team__c FROM User WHERE Id=:currentUserId];
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Number_of_Business_Partners__c,Number_of_BPs_in_Team__c FROM User]);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
List<Account> accns = new List<Account>();
List<user> lstcon= new List<user>();
for(sObject s : scope)
{
Account a = (Account)s;
user c = new user();
c.Number_of_Business_Partners__c=accns.size();
lstcon.add(c);
}
insert lstcon;
}
global void finish(Database.BatchableContext BC) {
}
}
thnaks in advance
count the number of accounts at account owner level.
I mean the accounts they are the ownerof. the requiremnet is only count of accouts not a name.
for example: account, contain 100 records on same account owner. then this count of 100 sholud be display on that Users custom field.
Here is the code,
global class NumberOfBPsClass implements Database.Batchable<sObject> {
List<Account> acc= new List<Account>();
//String query;
Set<Id> userIds =new Set<Id>();
User currentUser=new User();
//currentUserId = UserInfo.getUserId();
//List<User> currentUser=[SELECT Number_of_Business_Partners__c,Number_of_BPs_in_Team__c FROM User WHERE Id=:currentUserId];
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Number_of_Business_Partners__c,Number_of_BPs_in_Team__c FROM User]);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
List<Account> accns = new List<Account>();
List<user> lstcon= new List<user>();
for(sObject s : scope)
{
Account a = (Account)s;
user c = new user();
c.Number_of_Business_Partners__c=accns.size();
lstcon.add(c);
}
insert lstcon;
}
global void finish(Database.BatchableContext BC) {
}
}
thnaks in advance
If need all the accounts owned by you or each user, you may simply go to Accounts tab and select List View named My Accounts.
Yet if you need to have the count on each user record using Batch Apex you may use below code (Though I'm not aware why you want to use Batch here instead of Trigger)
HTH,
Suraj
All Answers
If need all the accounts owned by you or each user, you may simply go to Accounts tab and select List View named My Accounts.
Yet if you need to have the count on each user record using Batch Apex you may use below code (Though I'm not aware why you want to use Batch here instead of Trigger)
HTH,
Suraj
I have one more field on User, i.e: Number_of_Items__c, this is represents that current users role, so many users contain same role right.,
then my requirement is, I have one role 'XYZ', this is same for 5 users, then each user have 10 account records, then finally 5*10=50 is the result, ok,
this final result should be my output. and this is need on user field i.e: Number_of_Items__c
I hope you can understand.
thanks
I see you need count of total accounts/partners role wise, on user record. If I'm not wrong, then in your above example, all five users with "XYZ" role will have value 50 in their "Number_of_Items__c" fields. For this you may refer below script:
Hope this helps!!