0

I want to update the picklist on object A, firstly by validating the status picklist on all related records object B = Rejected. This is my first go at apex and this is how far I got before I got stuck

trigger RejectClaimCLIRejected on Case (before insert) {

lstCLI = [
     SELECT Id, Status
     FROM WorkOrder
     WHERE Id IN :trigger.newMap.keyset()
         ];

for(Case claim : lstCase){
    if(claim.Status = 'Rejected'){
        Status = Rejected
            }
    }
}

I'm querying the related object and returning the status and ID, then in the for loop, I want to validate that all the related statuses = rejected - if true then update the Case status to Rejected.

I think I'm missing some boolean logic which returns the TRUE value if all statuses = rejected, but I'm not sure where.. or maybe overthinking - i'm aware the code is probably full of syntax errors

I'm thankful for any guidance

edit:

I think I need a class to evaluate Case line items and return a boolean true flag, in a checkbox field, if all related line items are rejected. Then i'll create a trigger on the case object which updates the status to Rejected if that flag = True. Does this sound like the right way to do this?

4
  • Can you formalize your task, like I want to update the status of the case in trigger context, to rejected if ..... Nov 1, 2019 at 10:03
  • Okay, I'll try; I want to update the status of object A, in trigger context (unless a class is a better fit for this?), that if the status of all related line items to that object = Rejected, it will update the status of object A to Rejected. Does this clarify things? Nov 1, 2019 at 10:08
  • Object A is a Case in the current situation, am I correct? On what object do you have trigger? Case , or WorkOrder ? Nov 1, 2019 at 10:12
  • re-wrote my previous comment for claritys sake - Yes object A is Case. Object B is WorkOrder, which is a Case line item in this instance. Nov 1, 2019 at 14:15

1 Answer 1

1

There are a couple issues with the code. I understand that you're new to Apex, so I'll try my best to explain things as simply as I can.

The first issue is that you said that you want to update the status on Cases if the Work Orders on those cases are all rejected. This necessitates that the we aren't just checking for inserts, and that the Case must exist for the Work Order to be attached to it. So, I don't think you want to use just "before insert", but rather "before insert, before update".

trigger RejectClaimCLIRejected on Case (before insert, before update) {

The next issue I noticed is that you are querying WorkOrder records by looking for their Ids in trigger.newMap.keySet(). However, trigger.newMap.keySet() contains Case Ids, not WorkOrder Ids. So, this query will always return 0 records. If there is a lookup from WorkOrder to the Case, you need to use that instead. Assuming that the lookup you're using is CaseId, then the query should look like:

List<WorkOrder> workOrderList = [SELECT Id, Status, CaseId FROM WorkOrder WHERE CaseId IN :trigger.newMap.keyset()]

The next thing you need to do is, for each Case, find out if there are any WorkOrders that are not in Rejected status. There are different ways to do this. The method I would probably go with would be to group the WorkOrder records by Case Id in a map and then loop through each Case and check all the related WorkOrders to see if they are all rejected.

Map<Id, List<WorkOrder>> caseIdToWorkOrderList = new Map<Id, List<WorkOrder>>();
for ( WorkOrder work : workOrderList ) {
    if ( !caseIdToWorkOrderList.containsKey( work.CaseId ) ) {
        caseIdToWorkOrderList.put( work.CaseId, new List<WorkOrder>() );
    }
    caseIdToWorkOrderList.get( work.CaseId ).add( work );
}
for ( Case caseRecord : trigger.new ) {
    Boolean allRejected = true;
    if ( caseRecord.Status != 'Rejected' && caseIdToWorkOrderList.containsKey( caseRecord.Id ) ) {
        for ( WorkOrder work : caseIdToWorkOrderList.get( caseRecord.Id ) ) {
            if ( work.Status != 'Rejected' ) {
                allRejected = false;
            }
        }
        if ( allRejected ) {
            caseRecord.Status = 'Rejected';
        }
    }
}

A couple notes: I am looping through trigger.new in a before context, so I can update the field directly without having to make an update call. I am also not making changes to the field or going through the work orders if the Case is already Rejected. If you need it to be able to flip back from rejected, you will have to adjust the code. I am also not changing the status if there are no WorkOrders, so a Case that is created without work orders will not be automatically Rejected.

Hope this solves all the issues you're having. If I have made any wrong assumptions, just let me know. Thanks.

1
  • I should also add that this code only updates the Case status when the Case is updated. If you need the updates when WorkOrders are updated, then you will need to create a WorkOrder trigger that then queries all other WorkOrders with the same CaseId. Then, if all of them are in the Rejected Status and the related Case is currently not Rejected, update the related Case status to Rejected.
    – Roti
    Nov 1, 2019 at 19:07

You must log in to answer this question.

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