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
jameskCAjameskCA 

Need help updating fields from account / contact to opportunity depending on person account

I'm very new to Apex so I'm just trying to get pointed in the right direction.  My goal, when a lead is converted and a new opportunity is created, I have mapped a lead "preferred contact method" field to the same field on a contact record.  We have person accounts enabled, so this works for both person accounts and business accounts with contacts.

I also want to carry this field over to the oppotunity that is automatically created.  I'm hoping someone can point me in the right direction either conceptually or with some basic code.  I would think I need to loop through every opportunity trigger:new, figure out if it's a person or business account and put each type of account into a separate list - I'm not sure if that's the best approach or, if it is, what to do next.  

Any help would be appreciated.  
Abhishek BansalAbhishek Bansal

Hi Jim,

If you can explain your requirement a liitle bit more than i think we can provide you some help on this.
May be you are clear with your requirements but somehow i was not able to understand that  which field in the opportunity and contact you want to map with the converted lead. 
It would be good if you can provide answers to the following queries :
1. What do you mean by the statement "so this works for both person accounts and business accounts with contacts.". Please elaborate
2. Which field you want to carry on the converted Opportunity ?

Please let me know if anything is unclear to you.

Thanks,
Abhishek Bansal

jameskCAjameskCA
Sure, sorry - let me elaborate.

1. I have a preferred contact method picklist field on the lead.  I have the same preferred contact method picklist field on the contact object.  If a lead is converted to a person account or a business account, both account types can access this field.  If it's a person account, the api name is Preferred_Contact_Method__pc on the account object.  If it's a business account, it's Preferred_Contact_Method__c on the contact object.  Hope that makes sense.

2. As the lead mapping for the preferred contact method is contact.preferred contact method - when the oppotunity is created, I want to pull the value of preferred contact method Preferred_Contact_Method__c or Preferred_Contact_Method__pc into the opportunity, depending on whether or not it is a person account. 

Does the make sense?
jameskCAjameskCA
This is as close as I can get:
 
trigger OpportunityBeforeTest on Opportunity (before insert) {   
Set<Id> accts = new Set<Id>(); 

    for(Opportunity o:Trigger.New){
        if(o.AccountId != null){
		accts.add(o.AccountId);
            
        }
        
    }
    Map<Id,Account> mAccount = new Map<Id,Account>([SELECT Id, Name, Alt_Contact__c, Alt_Phone__c, Alt_Email__c, IsPersonAccount, Preferred_Contact_Method__pc
                        FROM Account WHERE Id = :accts]);
    
    Map<Id,Contact> mContact = new Map<Id,Contact>([SELECT Id,AccountId,Preferred_Contact_Method__c FROM Contact WHERE AccountId = :accts LIMIT 1]);
    system.debug('contacts map ' + mContact);
    
    for(Opportunity o:Trigger.New){
        If(mAccount.get(o.AccountId).IsPersonAccount){
            
            o.Preferred_Contact_Method__c = mAccount.get(o.AccountId).Preferred_Contact_Method__pc;
        }
        else{
            o.Preferred_Contact_Method__c = mContact.get(o.AccountId).Preferred_Contact_Method__c;
        }
        
    }
}

I know it won't work - I would need the mContact map to somehow have the key of the AccountId and the value of the contact sobject.  Any ideas?
jameskCAjameskCA
I've continued working on it and come up with a solution that seems to work - but it's very limited - e.g. it won't work for more than 1 contact.  This is okay for now as we don't have more than one contact for any business account - but I'd still like feedback on if there is a better way to do this (I'm sure there is) and some best practice advice.  
trigger OpportunityBeforeTest on Opportunity (before insert) {   
Set<Id> accts = new Set<Id>(); 

    for(Opportunity o:Trigger.New){
        if(o.AccountId != null){
		accts.add(o.AccountId);
            
        }
        
    }
    Map<Id,Account> mAccount = new Map<Id,Account>([SELECT Id,(SELECT id, Preferred_Contact_Method__c,name,title FROM Contacts Limit 1) Name, Alt_Contact__c, Alt_Phone__c, Alt_Email__c, IsPersonAccount, Preferred_Contact_Method__pc
                        FROM Account WHERE Id = :accts]);
    
    Map<Id,Contact> mContact = new Map<Id,Contact>([SELECT Id,AccountId,Preferred_Contact_Method__c FROM Contact WHERE AccountId = :accts LIMIT 1]);
    system.debug('contacts map ' + mContact);
    
    for(Opportunity o:Trigger.New){
        If(mAccount.get(o.AccountId).IsPersonAccount){
            
            o.Preferred_Contact_Method__c = mAccount.get(o.AccountId).Preferred_Contact_Method__pc;
        }
        else{

    o.Preferred_Contact_Method__c = mAccount.get(o.AccountId).Contacts[0].Preferred_Contact_Method__c;
            system.debug('mAccount.get(o.AccountId).Contacts[0].Preferred_Contact_Method__c ' +mAccount.get(o.AccountId).Contacts[0]);
        }
        
    }
}