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.
Case
, orWorkOrder
?