1

I have create a Map like:

Map<Id, List<Case>> Mapcase = new Map<Id, List<Case>>();

which is giving the result as Id of Account as key value and List of case as value of map. i want to iterate all the cases of account and need to do some calculations.

List<Account> listaccount = new List<Account>([SELECT Id, 
                                          FROM Account where ID IN: mapCase.KeySet()]);
    for(Account  acc : listaccount){

        for(List<Case> lstc : mapcase.values()){
            for (Case c : lstc)
            {
                if (c.Date__c!= null) {

                    if (math.abs(c.Date__c.daysBetween(system.today())) < 28) {

                    }
                    else if (math.abs(c.Date__c.daysBetween(system.today())) > 28) { }
                }
                if(GetCreatedYearofCase(c)){
                   ..
                }
            }

        }

            setacc.add(acc);

    }
    lstAcc.addAll(setacc);

but it is calculating all the cases i am having doubt, that i am making mistake on

for(List<Case> lstc : mapcase.values()){

line. but not able to rectify it.

please help me to resolve this.

Thanks.

  • Where is the code you have written to group accounts to list cases which belong to the same account? – RedDevil Nov 21 '18 at 11:11
  • The source code seems copy-pasted from a project and it seems that OP didn't open the documentation of the map class (use of the get on a map). Helping him is noble but, IMHO, stackexchange is not meant to be a 'do my job' forum. Isn't he supposed to anonymize the code (legal purpose) and to thouroughly search for an answer (source : salesforce.stackexchange.com/help/how-to-ask) and show attempts ? ? – Answers_Seeker Nov 21 '18 at 11:44
0

If you want to work on the list of Cases related to the Account instance acc, then the code would be:

for(Account  acc : listaccount){
    ...
    List<Case> lstc = mapcase.get(acc.Id);
    if (lstc != null) {
        for (Case c : lstc) {
            ...
        }
    }
}

The null check is needed if you don't have a non-null value for every Account in the map.

| improve this answer | |
1

Try to use the get method to access the list of cases by the passing account ID. You will get a list of cases for that particular Account.

mapcase.values() contains the list of caseList,In that case you will looping all the cases repeatedly

List<Account> listaccount = new List<Account>([SELECT Id, No_of_blades_docked_in_last_1_year__c, No_of_blades_docked_in_last_28_days__c,GE90_Direct_Discount_Discount_Price__c,GE90_100_Direct_Discount_Discount_Price__c,GENx_Direct_Discount_Discount_Price__c,GENx1B_Direct_Discount_Discount_Price__c,GENx2B_Direct_Discount_Discount_Price__c 
                                      FROM Account where ID IN: mapCase.KeySet()]);

for(Account  acc : listaccount){
    integer less_28 = 0;
    integer more_28 = 0;
    integer GE90 = 0 ;
    integer GE90_100 = 0;
    integer GENx_1B = 0 ;
    integer GENx_2B = 0;
    integer GENx = 0;

    List<Case> lstc = new list<case>();
    lstc.addAll(mapcase.get(acc.id)); 

/* Here You were looping through all the cases instead we get the list of cases using the account ID*/

        for(case c: lstc){
            if (c.Dock_Date__c != null) {
                System.debug('CFAN_DockDateCalculationTrigger: chp-2:   ' + c.Dock_Date__c);
                if (math.abs(c.Dock_Date__c.daysBetween(system.today())) < 28) {
                    less_28++;
                    more_28++;
                }
                else if (math.abs(c.Dock_Date__c.daysBetween(system.today())) > 28) {more_28++; }
            }
            if(GetCreatedYearofCase(c)){
                if(c.Programme__c == 'GE90'){ GE90++; }
                if(c.Programme__c == 'GE90-100'){ GE90_100++; }
                if(c.Programme__c == 'GEnx-1B'){ GENx_1B++; }
                if(c.Programme__c == 'GEnx-2B'){ GENx_2B++; }
                if(c.Programme__c == 'GE9X'){ GENx++; }
            }
        } 
    acc.No_of_blades_docked_in_last_28_days__c = less_28;
        acc.No_of_blades_docked_in_last_1_year__c = more_28;
        acc.GE90_Direct_Discount_Discount_Price__c = GE90;
        acc.GE90_100_Direct_Discount_Discount_Price__c =GE90_100;
        acc.GENx_Direct_Discount_Discount_Price__c = GENx;
        acc.GENx1B_Direct_Discount_Discount_Price__c = GENx_1B;
        acc.GENx2B_Direct_Discount_Discount_Price__c = GENx_2B;
        setacc.add(acc);

}
lstAcc.addAll(setacc);
| improve this answer | |
  • Thank you so much , Dinesh. it helps me a lot. I understand my mistake from your solution. – Atul Pandey Nov 21 '18 at 11:46
  • Glad I helped You @AtulPandey – SFDC Nov 29 '18 at 13:03

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.