0

There is One Account to Many Opportunities. There is Amount field in Opportunity. I am trying to find Opportunity with Minimum Amount against Account Id. I need a map like this Map<Accid,Opportunity> Opptoupdate;

I tried this way but I am not sure how to proceed as you can see I am comparing amount but I need to put this in above map(Map<Accid,Opportunity> Opptoupdate;) with minimum amount opp.

opportunity temp = new opportunity();
map < id, list < Opportunity >> mapwithoppList = new map < id, list < Opportunity >> ();
list < Account > AccList = new list < Account > ([Select id, Name, (Select id, Amount from opportunities) from Account where name = 'GenePoint']);
for (Account a: AccList) {
    if (a.opportunities.size() > 0) {
        mapwithoppList.put(a.id, a.opportunities);
    }
    for (ID st: mapwithoppList.keyset()) {
        temp = new opportunity();
        for (Opportunity op: mapwithoppList.get(st)) {
            temp.Amount = op.Amount;
        }
    }
}
  • You can obtain minimums in a single aggregate query call. – Keith C Jun 29 '16 at 10:36
  • Keith thanks for response ...but I want in the format of Map<Accid,Opportunity> Opptoupdate; – Salesforce Learner Jun 29 '16 at 10:37
  • Check this link salesforce.stackexchange.com/questions/5740/… – Saroj Bera Jun 29 '16 at 10:38
  • Saroj I checked that link but I have to something like this at end of second for loop Opportunity temp = mapwithoppList .get(st.id).[0].Name; and after this I need this opp to put against acc id in this was Map<Accid,Opportunity> Opptoupdate; – Salesforce Learner Jun 29 '16 at 10:41
1
list < Account > AccList = new list < Account > ([Select id, Name, (Select id, Amount from opportunities) from Account where name = 'GenePoint']);
Map<Accid,Opportunity> Opptoupdate = new Map<Accid,Opportunity>();

for( Account account : AccList) {
    for(Opportunity opportunity : account.opportunities) {
        if(opportunity.Amount == null || Opptoupdate.get(opportunity.AccountId).Amount > opportunity.Amount) {
            Opptoupdate.put(opportunity.AccountId, opportunity);
        }

    }
}

Check if this work for you

  • I am getting this error "Comparison arguments must be compatible types: Opportunity, Decimal " – Salesforce Learner Jun 29 '16 at 11:02
  • I tried this in developer console – Salesforce Learner Jun 29 '16 at 11:05
  • Check now, I have updated the code – Ishwar Mete Jun 29 '16 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.