0

I have requirement where on new case insertion need to check AccountId and all associated cases with that account. Then need to compare startdate of all cases and whichever is earlier need to calculate duration from today.

A1-->c1   --------->  2/2/2020
A1-->c2   --------->  12/2/2020
A1-->c3   --------->  22/2/2020
A1-->c4   --------->  2/3/2020

Then need to calculate older date which is in above example 2/2/2020 and stamp duration from today in current case which is getting inserted.

I have written logic,but not working as expected .Please suggest

 public static void newCase(List<case> newCaseList){
    set<id> accIds = new set<id>();
    for(case c:newCaseList){
        accIds.add(c.accountid);
    }
   list<account> accList = new list<account>([select id from account where id in:accIds]);

    map<id,id> casemap = new map<id,id>();
    list<case> caseList = new list<case>([select id,AccountId,start_date__c,status from case where 
     accountid in:accIds]);
  list<case> updateCase =new list<case>();
    for(case c:caseList){
    casemap.put(c.accountid,c.id);  
    }

    for(account ac:accList) {
    for(case c:caseList){
        if(casemap.containsKey(ac.id)){
           date todays=system.today();
            date st = c.start_date__c;

            if(i !=null){
            if(c.start_date__c!=null) {                      
            integer i =todays.daysBetween(st);           
            integer j=i;
             j=i;
            }
            if(j!=NULL){
            c.Duration__c =j;
            updateCase.add(c);
            }
        }
    }
}
}
update updateCase ;
}

}

3
  • Are you trying to stamp the duration since the first case was raised in that account in a fields on all the cases i.e if a first case was raised 90 days back then you stamp 90 on all cases under that account?
    – manjit5190
    May 11 '20 at 12:05
  • I updated my question with example
    – Kamath
    May 11 '20 at 12:10
  • A1-->c1 ---------> 2/2/2020 A1-->c2 ---------> 12/2/2020 A1-->c3 ---------> 22/2/2020 A1-->c4 ---------> 2/3/2020 Then need to calculate older date which is in above example 2/2/2020 and stamp duration from today in current case which is getting inserted.
    – Kamath
    May 11 '20 at 12:10
2

Well, I suggest that you start with the approach of getting the oldest case for each account right from the DB rather than having that logic your selves because I do not see that calculations in your code.

The below code will give you an idea of how to proceed and you can fit this in your code.

Set<Id> accIds = new Set<Id>();
for (Case c : newCaseList) {
    accIds.add(c.AccountId);
}

// Get the accounts with the oldest case each

Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, (SELECT Id, CreatedDate, Status FROM Cases ORDER BY Start_Date__c ASC LIMIT 1) FROM Account WHERE Id IN:accIds]);

for (Case c : newCaseList) {
    List<Case> casesForThisAccount = accounts.get(c.AccountId).Cases;
    if (casesForThisAccount != null && !casesForThisAccount.isEmpty()) {
        // Add proper null checks here. 
        c.Duration__c = casesForThisAccount.get(0).Start_Date__c.daysBetween(c.Start_Date__c); 
    }
}

update newCaseList;// (If not a before trigger)
3
  • casesForThisAccount != null is not necessary. A query never returns a null list.
    – sfdcfox
    May 11 '20 at 17:24
  • Thank you @sfdcfox. I just read your article on that topic and learned something.
    – manjit5190
    May 11 '20 at 17:55
  • thanks Manjit and SFDCFOX.it helps
    – Kamath
    May 12 '20 at 2:42

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.