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
punith narasimhapunith narasimha 

how to get all Opportunity names saparated by comma under account

here i want to display all opp names under account and each name saparated ny comma(,) but i did not get this requriment its displaying all opp names and i dont want to use for loop inside forr loop
please help me
public class AccountOppNamesTotalAmount 
{
    //method
    @AuraEnabled
    public static list<wrapperclass> alloppnames()
    {
        list<wrapperclass> wrplist=new list<wrapperclass>();
        set<id> accids=new set<id>();
        set<string> accname=new set<string>();
        map<string,wrapperclass> wrpmap=new map<string,wrapperclass>();
        String oppnames=null;
        String lastSplitoppnames=null;
        string glue = ',';
        map<id,string> mapoppnames = new map<id,string>();
        map<string,string> allnamemap = new map<string,string>();
        //fetching all account rec
        for(account acc:[select id,name,Type,phone from account limit 49999])
        {
            accids.add(acc.Id);
            accname.add(acc.Name);
            wrapperclass wrpcls=new wrapperclass();
            wrpcls.allaccnames=acc.Name;
            wrpcls.acctype=acc.Type;
            wrpcls.accphone=acc.Phone;
            wrpmap.put(acc.Id,wrpcls);
        }
        // for opp names
        
        for(Opportunity opp:[select id,name,AccountId from Opportunity where AccountId IN:accids and  AccountId !=null])
        {
            mapoppnames.put(opp.AccountId, opp.Name);
            if(mapoppnames.containsKey(opp.AccountId))
            {
                oppnames +=glue+opp.Name;
                String delimiter = ',';
                lastSplitoppnames=oppnames.substringAfter(delimiter);//for avoiding null at starting
                allnamemap.put(opp.AccountId,lastSplitoppnames);
            }
        }
        system.debug('=======>'+lastSplitoppnames);
        
        
        for(String acid:accids)
        {
            wrapperclass wc1=new wrapperclass();
            if(wrpmap.containsKey(acid))
            {
                wc1=wrpmap.get(acid);
                if(allnamemap.containsKey(acid))
                    wc1.alloppnames= allnamemap.get(acid); 
                wc1.alloppnames=lastSplitoppnames;
                wrplist.add(wc1);
            }
        }
        return wrplist;
    }
    
    //wrwpper class 
    public class wrapperclass
    {
        @AuraEnabled public String allaccnames        {set;get;}
        @AuraEnabled public String alloppnames        {set;get;}
        @AuraEnabled public String accphone            {set;get;}
        @AuraEnabled public String acctype            {set;get;}
        @AuraEnabled public decimal oppamount        {set;get;}
        
    }
    
}
punith narasimhapunith narasimha
User-added image
Yogeshwar TailorYogeshwar Tailor
Hi Punith,

Use the Map<String,List<String>> mpOfAccountAndOpp = new Map<String,List<String>>();

Use the inner query like : [SELECT Id,Name,(SELECT Id,Name from Opportunities) from Account)]

Iterate on the above query and put the key -> Account Name and Value -> List of opportunity name

And Use that map for the show the key and value on your VF page.

Thanks,
Yogesh
Ajay K DubediAjay K Dubedi
Hi Punith,
Please take help from this code and change your code accordingly:
public class GetAllOpportunity {
    public static void findOpportunity() {
        try {
            List<Opportunity> oppList = new List<Opportunity>();
            Map<Id, String> AccNameVsOpportunity = new Map<Id, String>();
            String temp = '';
            oppList = [SELECT Id, Name, AccountId FROM Opportunity WHERE AccountId != null LIMIT 10000];
            for(Opportunity op : oppList) {
                if(!AccNameVsOpportunity.containsKey(op.AccountId)) {
                    AccNameVsOpportunity.put(op.AccountId, op.Name);
                }
                else {
                    temp = AccNameVsOpportunity.get(op.AccountId);
                    temp = temp + ',' +  (op.Name);
                    AccNameVsOpportunity.put(op.AccountId, temp);
                }
            }
            system.debug('--AccNameVsOpportunity---' + AccNameVsOpportunity);
        }
        catch(Exception e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
            System.debug('The following Line : ' + e.getLineNumber());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
punith narasimhapunith narasimha
Thanks Every one