2

I've put together some code to automate the completion of CaseMilestones when the user makes certain changes to a Case.

trigger completeCaseMilestone on Case (after update) {

    List<WEID__c> weids = WEID__c.getall().values();
    Set<Id> validRecordTypeIds = new Set<Id>();

    // add Case Record Type Ids from Custom Setting to list of valid Ids      
    for (WEID__c weid: weids) {
        try {
            validRecordTypeIds.add(weid.WECaseId__c);
        } catch (System.StringException e) {
            System.debug(
                System.LoggingLevel.ERROR,
                'Invalid Record Type Id ' + weid.WECaseId__c
            );
        }
    }

    // create a list of Cases with valid Record Types
    List<Case> caseIds = new List<Case>();
    for (Case c : Trigger.new){

        if(validRecordTypeIds.contains(c.RecordTypeId))
        {
            caseIds.add(c);
        }      
    }
    if(caseIds.isEmpty() == true){
        system.debug('No Cases with both a valid Record Type Id were found');
        // exit trigger if no Cases with valid Record Type Ids are found
        return;
    }
    else{

            List<Id> acceptedIds = new List <Id>();
            for (Case c1 : caseIds){

                Case oldCase1 = Trigger.OldMap.get(c1.Id);
                if(oldCase1 != null && oldCase1.OwnerId != c1.OwnerId)
                {
                    acceptedIds.add(c1.Id);
                    System.debug('acceptedIds size is ' + acceptedIds.size());
                }

                if(acceptedIds.isEmpty() == true){
                    System.debug('No Cases with new owners were found');}
                else{

            List<Id> closedIds = new List <Id>();
            for (Case c2 : caseIds){

                Case oldCase2 = Trigger.OldMap.get(c2.Id);
                if(oldCase2.IsClosed == false && c2.IsClosed == true)
                {
                    closedIds.add(c2.Id);
                    System.debug('closedIds size is ' + closedIds.size());
                }

                if(closedIds.isEmpty() == true){
                    system.debug('No newly closed Cases were found');}

The below section isn't working as expected

        List<Id> closedIds = new List <Id>();
        for (Case c2 : caseIds){

            Case oldCase2 = Trigger.OldMap.get(c2.Id);
            if(oldCase2.IsClosed == false && c2.IsClosed == true)
            {
                closedIds.add(c2.Id);
                System.debug('closedIds size is ' + closedIds.size());
            }

            if(closedIds.isEmpty() == true){
                system.debug('No newly closed Cases were found');}

Whereas both system debug messages are 'printed' depending on whether the criteria has been met or not for the acceptedIds section, the closedIds debug only works when the list is empty.
When the Case has been closed - the Date/Time Closed field is being populated - the debug message stating the size of that list isn't printed (and neither is the message stating that 'no newly closed Cases were found').

1 Answer 1

0

I made some changes to your code. Not had a chance to fully test it though but I think it should work.

trigger completeCaseMilestone on Case (after update) {

List<WEID__c> weids = WEID__c.getall().values();
Set<Id> validRecordTypeIds = new Set<Id>();

// add Case Record Type Ids from Custom Setting to list of valid Ids      
for (WEID__c weid: weids) {
        try {
            validRecordTypeIds.add(weid.WECaseId__c);
        } catch (System.StringException e) {
            System.debug(
            System.LoggingLevel.ERROR,
           'Invalid Record Type Id ' + weid.WECaseId__c
        );
    }
}
// create a list of Cases with valid Record Types
List<Case> caseIds = new List<Case>();
for (Case c : Trigger.new){
    if(validRecordTypeIds.contains(c.RecordTypeId))
    {
        caseIds.add(c);
    }      
}
if(caseIds.isEmpty() == true){
    system.debug('No Cases with both a valid Record Type Id were found');
    // exit trigger if no Cases with valid Record Type Ids are found
    return;
}else{
    List<Id> acceptedIds = new List <Id>();
    for (Case c1 : caseIds){
        Case oldCase1 = Trigger.OldMap.get(c1.Id);
        if(oldCase1 != null && oldCase1.OwnerId != c1.OwnerId)
        {
            acceptedIds.add(c1.Id);
            System.debug('acceptedIds size is ' + acceptedIds.size());
        }           
    }
    if(acceptedIds.isEmpty() == true){
        System.debug('No Cases with new owners were found');
    }
    List<Id> closedIds = new List <Id>();
    for (Case c2 : caseIds){
        Case oldCase2 = Trigger.OldMap.get(c2.Id);
        if(oldCase2.IsClosed == false && c2.IsClosed == true)
        {
            closedIds.add(c2.Id);
            System.debug('closedIds size is ' + closedIds.size());
        }   
    }
    if(closedIds.isEmpty() == true){
        system.debug('No newly closed Cases were found');
    }
}

}

8
  • 1
    Thanks for the suggestion, first - as far as I can tell, the changes that you've made to my code are to re-position the curly brackets after the lines containing the system.debug messages which state the the size of each list of ids. If that's the case, could you please edit your answer to show these changes only. It's quite difficult to spot them when you have to review all of the code. Second - unfortunately even after the changes, the debug message stating the number of closedIds in the list still isn't being printed.
    – Alex S
    Commented May 23, 2015 at 19:16
  • The changes I made was to remove the if/else statement - if(acceptedIds.IsEmpty() == true) from inside the for (Case c1 : caseIds){}. I made similar changes inside for (Case c2 : caseIds){} by removing the if(closedIds.isEmpty() == true){}. If you are still having issues spotting the differences you can do a compare of my changes to your original code in a text editor such as notepad++. Addiditonally, put in debug statement after setting oldCase2 : System.debug('oldCase2 is ' + oldCase2.IsClosed); and System.debug('C2 is ' + C2.IsClosed); after setting the oldCase2 variable to trigger.Oldmap Commented May 23, 2015 at 19:46
  • 1
    The changes I made were to the order of execution in your code pretty much. Add the debug statements I proposed in my earlier comments to see what is actually happening before the if(oldCase2.IsClosed == false && c2.IsClosed == true){}. Commented May 23, 2015 at 19:50
  • The changes I suggested to the formatting of your answer were mainly for other users benefit - so that they can spot them without having to compare the two sets of code in notepad++ (or a Gist repository, as I did). I've now added System.debug('oldCase2 is '+ oldCase2.IsClosed); System.debug('c2 is ' + c2.IsClosed); below Case oldCase2 = Trigger.OldMap.get(c2.Id); and even these messages aren't displayed in the debug log. Does that suggest that this is where the issue is?
    – Alex S
    Commented May 23, 2015 at 19:59
  • That would suggest the the "for (Case c2 : caseIds){}" loop is not being run. You may want to add a debug statement - System.debug('caseIds is ' + caseIds); right below List<Id> closedIds = new List <Id>(); and see what's going on. Commented May 23, 2015 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .