function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
riShI9999riShI9999 

trigger on user to create ,update account

hi ....
trigger that automatically create a account  when user created ;
if any update  of user data  reflect on account 
help me
Maharajan CMaharajan C
Hi Rishi,

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
git Hubgit Hub

How can I create a question in this forum i m new ?
Maharajan CMaharajan C
Hi,

Please follow the screenshots to post your question in this forum.

User-added image


User-added image

User-added image

Thanks,
Raj
git Hubgit Hub
Thanks you Raj  :)