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
Isaac SeanIsaac Sean 

Trigger to update account number using user object

    for (User use : {
        String accid = use.AccountId;
        List<Account> account = [SELECT AccountNumber FROM Account WHERE Primary_Id__c = :accid];
        if(account != null && account.size()>0)
             use.Primary_id__c = account[0].AccountNumber;

Try this Isaac Sean

Set<String> accountIdSet = new Set<String>();
Map<String, Account> accountMap = new Map< String, Account>();

for(User use : Trigger.New){
for(Account acc : [Select id, AccountNumber From Account where Id IN :accountIdSet]){
	accountMap.put(acc.Id, acc);
for(User use : Trigger.New){
	use.Primary_Id__c = accountMap.get(use.AccountId).AccountNumber;
Sankaranarayanan VenkatachalamSankaranarayanan Venkatachalam
Hi Isaac,

If the 'AccountId' field in User is a lookup field, then you can change the 'Primary Id' to a Formula field, which gets the 'Account Number' value from the Account lookup. This will prevent the need to update all the old User records which may have blank 'Primary Id' field.

Best Regards.