function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott M - SFDC FanScott M - SFDC Fan 

Trying new trigger and can't seem to find what is not happening.

Hi,

 

New to Apex and programming in Java in general here, so bear with me.

 

I have a junction object  set up CaseServiceOrderAssoc__c to connect cases to a custom object called Orders__c. The junction object is needed because I need a many to many relationship to orders and cases and this works great.  However, in the business process, we want to check to make sure all related cases to an order are closed, before I can also close the order.  I can create a roll-up summary field in Orders__c from CaseServiceOrderAssoc__c to do counting of open cases, but I need a trigger from the Case object to set the related junction object records to "true". This is what I've got as a trigger (through reading other solutions and piecing my solution from them.) Like I said, I have little experience with all this, so if the code is funny looking, you know why.

 

What am I doing wrong?

 

trigger updateCaseClosed on Case (after update) {
    
    // This trigger will update the closed case countable field in the Case 2 Order Association Object 
    
    Integer i = 0;
    Set<ID> CaseServiceOrderAssocIds = new Set<ID>();
    Map<ID, Boolean> id2CaseClosed = new Map<ID, Boolean>();
    
    for (Case c : Trigger.new)
    {
        if(c.IsClosed == True)
        {
            CaseServiceOrderAssocIds.add(c.Id);
            id2CaseClosed.put(c.Id, c.IsClosed);
        }
        i++;    
    } 
    List<CaseServiceOrderAssoc__c> c2oassocs = [select Id  from CaseServiceOrderAssoc__c where Id in :CaseServiceOrderAssocIds];
    
    for (CaseServiceOrderAssoc__c assocs : c2oassocs)
    {
        assocs.Related_Case_Closed_Countable__c = id2CaseClosed.get(assocs.Id);
    }
    
    update c2oassocs;
}
D.M.D.M.

When you get c2oassocs you don't retrieve Related_Case_Closed_Countable__c

Don;t this trow errors when you try to access it?

 

Do you need to store IsClosed when it can only be true?

 

What is i for?

Scott M - SFDC FanScott M - SFDC Fan

Hey, thanks for answering.

 

Good question about "i". Don't need that really. I think the code I was copying from used it. I will take it out.

 

Yes, only when Cases are closed, should there be an update to a CaseServiceOrderAssoc__c record to update the corresponding field.

 

As for errors, that is the problem. There aren't any.

 

I did have the Related_Case_Close_Countable__c field in the query at one point trying to fix this, but that didn't seem to help. I'll put it back in. This is the code I have now.

 

trigger updateCaseClosed on Case (after update) {
    
    // This trigger will update the closed case countable field in the Case 2 Order Association Object 
    
    Set<ID> CaseServiceOrderAssocIds = new Set<ID>();
    Map<ID, Boolean> id2CaseClosed = new Map<ID, Boolean>();
    
    for (Case c : Trigger.new)
    {
        if(c.IsClosed == True)
        {
            CaseServiceOrderAssocIds.add(c.Id);
            id2CaseClosed.put(c.Id, c.IsClosed);
        }
    } 
    List<CaseServiceOrderAssoc__c> c2oassocs = [select Id, Related_Case_Closed_Countable__c  from CaseServiceOrderAssoc__c where Id in :CaseServiceOrderAssocIds];
    
    for (CaseServiceOrderAssoc__c assocs : c2oassocs)
    {
        assocs.Related_Case_Closed_Countable__c = id2CaseClosed.get(assocs.Id);
    }
    
    update c2oassocs;
}
Scott M - SFDC FanScott M - SFDC Fan

Reading up some more, I think this would be better. Maybe??? Or does Apex not run the query, if there are no values in the c2oassocs list?

 

trigger updateCaseClosed on Case (after update) {
    
    // This trigger will update the closed case countable field in the Case 2 Order Association Object 
    
    Set<ID> CaseServiceOrderAssocIds = new Set<ID>();
    Map<ID, Boolean> id2CaseClosed = new Map<ID, Boolean>();
    
    for (Case c : Trigger.new)
    {
        if(c.IsClosed == True)
        {
            CaseServiceOrderAssocIds.add(c.Id);
            id2CaseClosed.put(c.Id, c.IsClosed);
        }
    } 
    List<CaseServiceOrderAssoc__c> c2oassocs = [select Id, Related_Case_Closed_Countable__c  from CaseServiceOrderAssoc__c where Id in :CaseServiceOrderAssocIds];
    
    for (CaseServiceOrderAssoc__c assocs : c2oassocs)
    {
        assocs.Related_Case_Closed_Countable__c = id2CaseClosed.get(assocs.Id);
    }

// only do an update, if there is something to update
    if (assocs != null)
{
     update c2oassocs;
}
}
D.M.D.M.

What do your Debug Logs think happens?

Scott M - SFDC FanScott M - SFDC Fan

Now I have to ask a noob question. Where do I find the debug logs?

 

 

amit_sfdcT-2amit_sfdcT-2

Hi ,

 

Click under your name :: Setup

 

In the left side, there is search window, put Debug Log.

 

You can find it.

 

Let me know if you have any queries.

 

Regards,

Amit Kumar Gupta

Salesforce.com