1

I have a custom object related to the Account object and when the look-up fields on that custom object get updated it should update all of the associated contact records. Everything in my code works except when I try to populate the contact fields I get the user ID, not the user name. The fields on the contact are text fields.

Here is the code:

public class AccountHelper {
    public Static final String BU;
    static {
        BU='TL';
    }
    public static void updateAccountContacts(set<Id> accIds, list<TL_Business_Relationship__c> accRellist){
        
        List<Contact>conlist=new List<Contact>();   
        map<Id,TL_Business_Relationship__c> 
            accRelmap=new map<Id,TransLoc_Business_Relationship__c>();
       
        List<Account> Acclist=[select Id,name,Account_Sub_type__c,
                               TL_Implementation_Completed_Date__c,
                               Account_Type_System_Use_Only__c,Product_s_Purchased__c,
                               Product_Interest__c,Industry,RFP_in_Progress_Do_not_Contact__c,
                               (select Id,owner.companyname,Industry__c,Product_s_Purchased__c,
                                Active__c,Product_Interest__c,RFPINPROGRESS__c,
                                                                                                                                                                                         TL_Relationship__c FROM Contacts where Active__c=True)  from Account where Id IN:accIds];
        for(TL_Business_Relationship__c accRel : accRellist){
            
            if(accRel.Business_Unit__c==BU){
                accRelmap.put(accRel.Account__c,accRel);
            }
        }
        //Updating fields on Contact from Account when Account relationship business unit is 'TL'
        for(account acc: accList)
        {
            for (contact con:acc.contacts)
            {            
                con.Account_Type__c=acc.Account_Type_System_Use_Only__c;
                con.Account_Sub_Type__c=acc.Account_Sub_type__c;
                con.TL_Implementation_Completed_Date__c=acc.TL_Implementation_Completed_Date__c;
                con.Industry__c=acc.Industry;
                con.product_Interest__c=acc.Product_Interest__c;
                con.RFPINPROGRESS__c=acc.RFP_in_Progress_Do_not_Contact__c;
                con.Product_s_Purchased__c=acc.Product_s_Purchased__c;
                if(accRelmap.get(acc.Id)!=null){
                    con.Partner_Manager__c=accRelmap.get(acc.Id).Partner_Manager__c;
                    con.Partner_Specialist__c=accRelmap.get(acc.Id).Partner_Specialist__c;
                    con.TL_Account_CSM__c=accRelmap.get(acc.Id).TL_Account_CSM__c;
                    con.TL_Relationship__c=accRelmap.get(acc.Id).TL_Relationship_Type__c;
                                       
                    
                }                
                conlist.add(con);
            }            
        }    
        update conlist;
    }
}

1 Answer 1

0

Yes, that's how lookup fields work. To get the name, you'd have to use the relationship to get it, such as:

con.Partner_Manager__c=accRelmap.get(acc.Id).Partner_Manager__r.Name;

Where __r means you want to use a relationship, and .Name is the field you want to access. Note that you need to also update your query to include this field, or you'll get an exception message "SObject row was retrieved via SOQL without querying the requested field."

Note that I have no idea which field(s) you're talking about, so you'll need to adjust this answer to whichever field(s) are not working correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .