You need to sign in to do that
Don't have an account?
punith 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;}
}
}
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;}
}
}
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
Please take help from this code and change your code accordingly: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi