+ Start a Discussion
Debbie ChrysochouDebbie Chrysochou 

How to get an Opportunity field (Total_Price__c) into Account

Hello,


I am looking to create a custom formula field in Accounts to read the custom field I have created in Opportunities. While trying to insert the field in advanced formula creator, it seems that I am not able to find Opportunities and link the appropriate field. Any suggestions?
Best Answer chosen by Debbie Chrysochou
JyothsnaJyothsna (Salesforce Developers) 
Hi ,

You cannot use a cross object formula on Parent object to fetch child's field, but vice versa is possible. e.g you can use a cross object formula field in opportunity object if you want to fetch the account field BUT you cannot use cross object formula to fetch account object's field.
 
Instead of that Create new custom fields on the Account and select Rollup Summary as the datatype.

Hope this helps you!
Best Regards,
Jyothsna

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi ,

You cannot use a cross object formula on Parent object to fetch child's field, but vice versa is possible. e.g you can use a cross object formula field in opportunity object if you want to fetch the account field BUT you cannot use cross object formula to fetch account object's field.
 
Instead of that Create new custom fields on the Account and select Rollup Summary as the datatype.

Hope this helps you!
Best Regards,
Jyothsna
This was selected as the best answer
Mahesh DMahesh D
Hi Debbie,

One Account will have multiple Opportunities.

Do you want to sum of Total_Proce__c from all the related Opportunities?

As it is a Lookup relationship, you can't use the rollup summary field. Hence you have to create a trigger to calculate it.

Below is the sample code:

 
//
// OpportunityRollupTrigger trigger to handle the roll up summary on Account
//
trigger OpportunityRollupTrigger on Opportunity (after insert, after delete, after undelete, after update) {
    set<Id> accIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Opportunity opp: Trigger.new){
            if(Trigger.isInsert || Trigger.isUndelete || (opp.AccountId != Trigger.oldMap.get(opp.Id).AccountId))
                accIdSet.add(opp.AccountId);
        }
    }
    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Opportunity opp: Trigger.old){
            if(Trigger.isDelete || (opp.AccountId != Trigger.newMap.get(opp.Id).AccountId))
                accIdSet.add(opp.AccountId);
        }
    }    
    
    if(!accIdSet.isEmpty()) {
        List<Account> accList = [select Id, Count__c, (Select Id, Total_Price__c from Opportunities) from Account Where ID IN: accIdSet];
        
        for(Account acc : accList){
            
            acc.Total_Price__c = 0;
            for(Opportunity opp: acc.Opportunities) {
                acc.Total_Price__c += opp.Total_Price__c;
            }
        }
        update accList;
    }
}

Please do let me know if it helps you.

Regards,
Mahesh
Debbie ChrysochouDebbie Chrysochou
Thank you both for your answers. Both works well but the one from Jyothsna is much easier.

Thanks again
JyothsnaJyothsna (Salesforce Developers) 
Hi,

Roll-up Summary  feature is working properly between Account and Opportunity objects. I tried this one and it is working properly.

The recently announced Winter 08 feature - Roll-up Summary Fields for Standard Objects (Opp-Opp Line in Winter 08) - is another great step forward. While this will extend the Roll-Up Summary Field functionality to work with Opportunities and their Line Items.

Please check the below link for more information.
https://success.salesforce.com/ideaview?id=08730000000BraSAAS

Please check the below screenshot.

User-added image

Let me know if it helps you!
Best Regards,
Jyothsna