Please try the below code. Hope this helps you writing your apex class.
If you want for only active users then you fetch the active users using (IsActive=true)
public class LeadUpdateOwner { public static void updateLeadRegion() { List<User> UserList = new List<User>(); UserList = [SELECT Id,Location__c FROM User];
List<Lead> LeadList = new List<Lead>(); LeadList = [SELECT OwnerId,Name,Region__c FROM Lead];
Mark as best answer if it helps you.
Thanks,
Ghanshyam Choudhari
Please try the below code. Hope this helps you writing your apex class.
If you want for only active users then you fetch the active users using (IsActive=true)
public class LeadUpdateOwner {
public static void updateLeadRegion()
{
List<User> UserList = new List<User>();
UserList = [SELECT Id,Location__c FROM User];
List<Lead> LeadList = new List<Lead>();
LeadList = [SELECT OwnerId,Name,Region__c FROM Lead];
List<Lead> updateLeadList = new List<Lead>();
for(User u: UserList)
{
for(Lead led: LeadList)
{
if(u.Id == led.OwnerId)
{
led.Region__c = u.Location__c;
updateLeadList.add(led);
}
}
}
if(updateLeadList.size()>0)
{
update updateLeadList;
}
}
}
Please mark it as best answer if you find helpful.
Thank You
Ajay Dubedi
it seems like you need batch apex To run batch Apex, go to User Menu --> Developer Console.
In the Apex Code section,