Please use the below trigger and Apex handler for your scenario:
Here you have to on custom lookup in Account to user name it as User (Api Name : User__c)
Apex Trigger:
trigger UserTrigger on User (after insert,after update) {
if ((trigger.isInsert || trigger.isUpdate) && trigger.isAfter){
Set<Id> userIds = new Set<Id>();
for (User u : trigger.new) {
// Add the user id to the set of ids userIds.add(u.Id); } if (!System.isFuture() && !System.isBatch()) { //upsert Account records with the changes in user records UpsertUserAccount.execute(userIds); }
} }
Apex Handler Class:
public with sharing class UpsertUserAccount { @Future public static void execute(Set<Id> userIds) {
List<Account> AccountsToUpsert = new List<Account>(); // Create a list of Account to upsert
List<User> users = [SELECT Id, Email, FirstName, LastName, Department, ManagerId, Fax, Phone, MobilePhone, Title, Street, City, State, PostalCode, Country FROM User WHERE Id IN : userIds AND IsActive = true];
List<Account> Accounts = [SELECT Id, User__c FROM Account WHERE User__c IN: userIds];
Map <Id,Id> userAccountMap = new Map <Id,Id>(); // Create a user Account map of userId, AccountId
for (User u : users){ // Loop through each upserted user
Account c = new Account( // Create a Account record in memory User__c = u.Id, // Populate the user lookup Email__c = u.Email, // Populate the email facebook_name__c = u.FirstName, // Populate the first name Name = u.LastName, // Populate the last name Fax = u.Fax, // Populate the fax number Phone = u.Phone, // Populate the phone number OwnerId = u.Id); // Populate the contact owner
if (userAccountMap.get(u.Id) != null) { c.Id = userAccountMap.get(u.Id); // specify the Account to be updated }
AccountsToUpsert.add(c); // Add the Account to the bulk upsert list
}
if(AccountsToUpsert.size() > 0){ upsert AccountsToUpsert; // Upsert all Account in single DML statement } }
}
Can you please Let me know if it works or not!!!
If it helps don't forget to mark this as a best answer!!!
Please use the below trigger and Apex handler for your scenario:
Here you have to on custom lookup in Account to user name it as User (Api Name : User__c)
Apex Trigger:
trigger UserTrigger on User (after insert,after update) {
if ((trigger.isInsert || trigger.isUpdate) && trigger.isAfter){
Set<Id> userIds = new Set<Id>();
for (User u : trigger.new) {
// Add the user id to the set of ids
userIds.add(u.Id);
}
if (!System.isFuture() && !System.isBatch()) {
//upsert Account records with the changes in user records
UpsertUserAccount.execute(userIds);
}
}
}
Apex Handler Class:
public with sharing class UpsertUserAccount {
@Future
public static void execute(Set<Id> userIds) {
List<Account> AccountsToUpsert = new List<Account>(); // Create a list of Account to upsert
List<User> users =
[SELECT Id, Email, FirstName, LastName, Department, ManagerId, Fax, Phone, MobilePhone, Title, Street, City, State, PostalCode, Country
FROM User
WHERE Id IN : userIds AND IsActive = true];
List<Account> Accounts =
[SELECT Id, User__c
FROM Account
WHERE User__c IN: userIds];
Map <Id,Id> userAccountMap = new Map <Id,Id>(); // Create a user Account map of userId, AccountId
for(Account userAccount: Accounts){
userAccountMap.put(userAccount.User__c,userAccount.Id);
}
for (User u : users){ // Loop through each upserted user
Account c = new Account( // Create a Account record in memory
User__c = u.Id, // Populate the user lookup
Email__c = u.Email, // Populate the email
facebook_name__c = u.FirstName, // Populate the first name
Name = u.LastName, // Populate the last name
Fax = u.Fax, // Populate the fax number
Phone = u.Phone, // Populate the phone number
OwnerId = u.Id); // Populate the contact owner
if (userAccountMap.get(u.Id) != null) {
c.Id = userAccountMap.get(u.Id); // specify the Account to be updated
}
AccountsToUpsert.add(c); // Add the Account to the bulk upsert list
}
if(AccountsToUpsert.size() > 0){
upsert AccountsToUpsert; // Upsert all Account in single DML statement
}
}
}
Can you please Let me know if it works or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
How can I create a question in this forum i m new ?
Please follow the screenshots to post your question in this forum.
Thanks,
Raj