1

I have a need to populate a lookup field on a custom object with a partner user. The only problem is that I need this to be populated with the partner user who is set up as a contact under the account that is related to the custom object through a two lookup relationships.

Here is the structure:

Receiving__c: (Custom Object) I'm trying to populate a "Receiver_Contact__c" field with the partner user. I have a formula that populates the Account ID of the Partner User's Account and that is the "Receiver_Account_Number__c" field.

I basically want run a query and return user's where their "contact.AccountId" value matches "Receiver_Account_Number__c" field on my "Receiving__c" object and then set that User Id to my "Receiver_Contact__c" field on that same object.

My code is below, any help is greatly appreciated, as always!

   trigger getReceiverUser on Receiving__c (before insert, before update){

  //get the acct Id and store it in a set.
    set<Id> acctIdSet = new set<Id>();
    for(Receiving__c rec: trigger.new){
        if(rec.Receiver_Account_Number__c  != null){
            acctIdSet.add(rec.Receiver_Account_Number__c);
        }
    }

    //query the user records and get the associated accounts.
    map<id, User> usrMap = new map<id, User>([SELECT id, contact.accountid from user where contact.accountid IN: acctIdSet]);

    //update the Receiving value based on the account id in the record.
    for(Receiving__c rec: trigger.new){
        if(usrMap.containsKey(rec.Receiver_Account_Number__c)){
            rec.Receiver_Contact__c  = usrMap.get(rec.Receiver_Account_Number__c).contact.accountid;
        }       
    }

}
0

I think you we're on the right track, although when you have this line

usrMap.containsKey(rec.Receiver_Account_Number__c)

You are actually saying: Return true or false if the Id of the User will equal the Receiver_Account_Number__c field which will always be false.

Also I checked and there is an actual field in the User table that is called AccountId. It might be easier to use that instead of contact.AccountId. Let me know if you have a problem with that.

Third point which is more of a suggestion you can narrow the SOQL query event more by adding another filter to the where clause to check if the User has the Partner licence type or a specific profile for this kind of user. You can improve the SOQL query even more by adding the filter isActive = true to make sure you don't select any inactive users

        trigger getReceiverUser on Receiving__c (before insert, before update){

            //get the acct Id and store it in a set.
            set<Id> acctIdSet = new set<Id>();
            Map<Id,Receiving__c> recMap = new Map<Id, Receiving__c>();
            for(Receiving__c rec: trigger.new){
                if(rec.Receiver_Account_Number__c  != null){
                    acctIdSet.add(rec.Receiver_Account_Number_c);
                    recMap.put(rec.Receiver_Account_Number_c, rec)
                }
            }

            //query the user records and get the associated accounts.
            List<User> usrList = new List<User>([SELECT id, AccountId from user where AccountId IN: acctIdSet]);

            for(User usr : usrList){
                if(recMap.containsKey(usr.AccountId)){
                    recMap.get(usr.AccountId).Receiver_Contact__c = usr.Id;
                }
            }

        }

You could also get rid of the if clause in the second for loop that checks if it contains a key as you already make sure in the SOQL query to only retrieve Users where the AccountId matches.

Hope it helps

  • 1
    That worked like a charm, thank you for the help Mihai! Much appreciated. – Sesaco Admin Jan 21 '15 at 20:45
  • Glad I could help. – Mihai Neagoe Jan 21 '15 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.