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
force shahidforce shahid 

How to fetch account from opportunity object ?

Hi Friends,
I Created a custom lookup field on Opportunity object to Contact object. I need to fetch the account details when i create new opportunity under contact object.

While creating new opportunity under contact object , ihave only contact details, but i need to fetch this particular contact account details also. 

Code:
 Set<Id> cId=new Set<Id>();
        for(Opportunity opp : newList){
        cId.add(opp.Primary_Broker__c);
        }
              List<Contact> Updcon = new List<Contact>();
              List<Contact> con = [select id,AccountId,Open_Opportunities__c from Contact where Id in:cId ]; 
              List<Opportunity > opportunity= [select id,Primary_Broker__c,StageName from Opportunity where  Primary_Broker__c in:cId AND ( StageName NOT IN ('Closed Won','Closed Lost')) ];
           for(Contact c :con )
           {
             c.Open_Opportunities__c=opportunity.size();
             Updcon.add(c);
           }update Updcon;

Thanks in advance,
Shahid.
Shruti SShruti S
You can use Cross Object Formula fields to achieve this. i.e You can create a Formula field on the Opportunity object which would fetch a value from the Account fields.

Example: Assume you have a lookup field to Contact on your Opportunity object, say its API name is Contact__c . Now you want to display that Contact's related Account's Name in one of the field on Opportunity object. For that, create a Formula field with Text as return type on Opportunity and write the below code in it - 
Contact__r.Account.Name
Note: Please use the Insert Field button on the formula to insert fields and never type them as it is prone to make mistakes.

The formula field which was created on the Opportunity object is known as Cross Object Formula.
force shahidforce shahid
Hi Shruthi,

Thanks for your reply. But no need to create formula field. In opportunity object there is Account lookup field. I want to write a code on this apex class. 
Thanks,
Shahid.
Shruti SShruti S
Could you please tell me if it the Account of the related Contact of Opportunity that we need to populate on the Opportunity record?
force shahidforce shahid
Hi Shruti,

Yes,  I need to populate the account record name on the Opportunity record 
Shruti SShruti S
I have replicated this is my Org, and I was able to do it. Here is the code for the trigger - 
trigger ForceShahidTrigger on Opportunity ( before Insert, before Update ) {
    Set<Id> cId = new Set<Id>();
    for( Opportunity opp : Trigger.new ) {
        if( opp.Primary_Broker__c != NULL ) {
            cId.add( opp.Primary_Broker__c );
        }
    }
    
    if( !cId.isEmpty() ) {
        List<Contact> cons = [
            SELECT  Id
                    ,AccountId
            FROM    Contact 
            WHERE   Id IN :cId 
        ];
        
        Map<Id, String> conAccMap = new Map<Id, String>();
        for( Contact con : cons ) {
            if( con.AccountId != NULL ) {
                conAccMap.put( con.Id, con.AccountId );
            }
        }
        
        if( !conAccMap.isEmpty() ) {
            for( Opportunity opp : Trigger.new ) {
                if( opp.Primary_Broker__c != NULL ) { 
                    opp.AccountId = conAccMap.get( opp.Primary_Broker__c );
                }
            }
        }
    }
}
Please feel free to get back if you have any more doubts.
force shahidforce shahid
Hi Shruti,

Thank you so much. My exact requirement is calculate the how many opportunities are open under this Contact record. I did this one. with the above code  I mentioned in my question. 
And I want to check populate account name also in opportunity new record under contact object.
 ( My requirement is when i create new opportunity under contact object RL , then i need to poplate Account name also according this contact record. ). 
But i didn;t understand where i add this code in my code. Can you please help me.
Thanks,
Shahid.