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
Ashim ChapagainAshim Chapagain 

How to display the picklist field value of opportunity object in account object?

We have a picklist field in opportunity object named "Solution Category" which has 8 picklist values. what i need to do is to display the "Solution Category" fields values of the opportunity object into the account object. However, i am finding difficulty in doing so because account has multiple opportunities. Is there any way that i can display the value of "Solution Category" object for all ooprtunities in the account object? 
Deepali KulshresthaDeepali Kulshrestha
Hi Ashim,

I would suggest you create a custom field on Account with name SolutionCategory with field type Text Area(Long) and populate the SolutionCategory picklist value every time a new opportunity is created.

I have done this using a trigger that will work on after insert of an opportunity.
//Trigger
trigger OnOpportunity on Opportunity (after insert) {
    if(Trigger.isAfter&&Trigger.isInsert){
        OpportunityHandler.populateAccountSolutionCategory(Trigger.new);
    }

}

//Handler Class
public class OpportunityHandler {
    public static void populateAccountSolutionCategory(List<Opportunity> opportunity_List){
        Set<Id> AccountIdList = new Set<Id>();
        for(Opportunity op : opportunity_List){
            AccountIdList.add(op.AccountId);
        }
        
        List<Account> accountList = [SELECT Id, Name,SolutionCategory__c FROM Account WHERE ID IN :AccountIdList];
        //Account Map
        Map<Id,Account> accountMap = new Map<Id,Account>();
        if(accountList.size()>0){
            for(Account a : accountList){
                accountMap.put(a.Id,a);
            }
        }
        //AccountId and List of Opportunity Map
        Map<Id,List<Opportunity>> accountOpportunityMap = new Map<Id,List<Opportunity>>();
        for(Opportunity o : opportunity_List){
            if(accountOpportunityMap.containsKey(o.AccountId)){
                List<Opportunity> oList = accountOpportunityMap.get(o.AccountId);
                oList.add(o);
                accountOpportunityMap.put(o.AccountId,oList);
            }
            else{
                accountOpportunityMap.put(o.AccountId,new List<Opportunity>());
                List<Opportunity> oList = accountOpportunityMap.get(o.AccountId);
                oList.add(o);
                accountOpportunityMap.put(o.AccountId,oList);
            }
        }
        List<Account> accToUpdate = new List<Account>();
        for(Id accId : accountOpportunityMap.keySet()){
            Account acc = accountMap.get(accId);
            String selectCategory= acc.SolutionCategory__c;
            List<Opportunity>  opList = accountOpportunityMap.get(accId);
            for(Opportunity o : opList){
                selectCategory += o.SolutionCategory__c+';';
            }
            acc.SolutionCategory__c = selectCategory;
            accToUpdate.add(acc);
        }
        update accToUpdate;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com