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
pdvaporpdvapor 

Setting Account Lookup with Trigger

I am trying to set an Account Lookup field on all Accounts owned by Contacts who are Portal Users under an Account.

 

Essentially, Portal User A is under Account A.  When I update Account A, I want to update all of Portal User A's Accounts with the Account A Account Id.  Can't seem to get this to work.  Any advice?

 

trigger UpdateClientAccount on Account (after update) {
    
    Set<Id> acctId = new Set<Id>();
    for (Account acct : Trigger.new)
    acctId.add(acct.id);
    
    Map<Id, Contact> contacts = new Map<Id, Contact>(
    [select id from contact
    where id in :acctId]);

    Map<Id, User> users = new Map<Id, User>(
    [select id from user
    where id in :Trigger.oldMap.keySet()]);
        
    for (Account accounts : [select Owner_s_Client_Account__c from Account
    where OwnerId in :Trigger.oldMap.keySet()])
    accounts.Owner_s_Client_Account__c = acctId;

}

 

Shashikant SharmaShashikant Sharma

One change in your trigger see this in below code, // filter according to contactid

 

trigger UpdateClientAccount on Account (after update) {
    
    Set<Id> acctId = new Set<Id>();
    for (Account acct : Trigger.new)
    acctId.add(acct.id);
    
    Map<Id, Contact> contacts = new Map<Id, Contact>(
    [select id from contact
    where id in :acctId]);
    // filter according to contactid
    Map<Id, User> users = new Map<Id, User>(
    [select id from user
    where contactid in : contacts.keySet()]);
        
    for (Account accounts : [select Owner_s_Client_Account__c from Account
    where OwnerId in :Trigger.oldMap.keySet()])
    accounts.Owner_s_Client_Account__c = acctId;

}

 Could you please elaborate more what do you want to update in this trigger, could not get from your question.

pdvaporpdvapor

This is a tough one.  Essentially, I want to update the Account Owned by Portal Users by the Portal User Contact Account.

pdvaporpdvapor

And I want to update it to the Account ID that is the record I am currently updating.

Shashikant SharmaShashikant Sharma

Still can not understand let me ask via an example

 

Account : Acc1

 

has 3 Contacts

 

Con1 , Con2 , Con3

 

Con1 and Con2 has portal users enabled U1 and U2 respectively

 

What would be the value of 

 

Owner_s_Client_Account__c field on Account after update trigger executed.

 


pdvaporpdvapor


Account : Acc1

 

has 3 Contacts

 

Con1 , Con2 , Con3

 

Con1 and Con2 has portal users enabled U1 and U2 respectively

 

U1 is the Owner of Account 2 and Account 3

U2 is the Owner of Account 4 and Account 5

 

When the trigger is executed, Owner_s_Client_Account__c field on Account 2, Account 3, Account 4, and Account 5 is the Id of Acc1.

 

Thanks again for your help!

pdvaporpdvapor

Also to help, here is what I am intending to do in the Trigger.

trigger UpdateClientAccount on Account (after update, after insert) {
    
    //Get Account ID of the current record.
    Set<Id> acctId = new Set<Id>();
    for (Account acct : Trigger.new)
    acctId.add(acct.id);
    
    //Fetch all Contacts under the current Account using Account ID.
    Map<Id, Contact> contacts = new Map<Id, Contact>(
    [select id from contact
    where id in :acctId]);
    
    //Fetch all Portal Users using Contact IDs.
    Map<Id, User> users = new Map<Id, User>(
    [select id from user
    where id in : contacts.keySet()]);
    
    //Fetch all Accounts owned by Users using User IDs.
    Map<Id, Account> mapAccount = new Map<Id, Account>([select id,Owner_s_Client_Account__c from Account where id in : users.keySet()]);
    
    //Set Owner's Client Account with current Account ID.
    for(account a :Trigger.new)
    a.Owner_s_Client_Account__c = mapAccount.get(acct.id);
}