0

I have 2 objects called WorkRequest__c and WorkOrders__c. Each time our client receives a new request, a record is created on the WorkRequest__c object. Depending on information in each request, a certain number of "Workorder" records are created.

The WorkRequest__c and WorkOrder__c objects are related by a lookup field that I've added to the WorkOrder__c object (WorkOrder__c.WorkRequest__c). You can have multiple "Workorders" for a single "Workrequest", but only one "Workrequest" for a given "Workorder".

I'm writing a trigger that for each time a WorkOrder is created or updated, I want to:

1) Query all other WorkOrders with the same WorkRequest Id (ie the value in the WorkOrder__c.WorkRequest__c lookup field)

2) Check each WorkOrder record to see if the WorkOrder__c.Status__c = 'Complete'.

3) If WorkOrder__c.Status__c = 'Complete' for all WorkOrder records associated with a given WorkRequest Id, then update the WorkRequest__c.Stage__c = 'Finalized'

Below is the trigger I am trying to create. But I'm getting the following error in my nested SOQL query - "Compile Error: Didn't understand relationship 'WorkOrders__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name".

Can someone help me correct?

**trigger DDStepFields on WorkOrder__c(after insert,after update){
List<WorkRequest__c> wReqobj = new List<WorkRequest__c>();
List<Id> listIds = new List<Id>();

for(WorkOrder__c orderObj : Trigger.new){
    listIds.add(orderObj.WorkRequest__c);
}


wReqobj = [SELECT Id, Name, Stage__c (SELECT Id, Status__c FROM WorkOrder__r)
                    FROM WorkRequest__c
                    WHERE ID IN :listIds];

for (WorkRequest__c req : wReqobj){
    for(WorkOrder__c wo : req.WorkOrder__r) {
         if(wo.Status__c == 'Closed') {
            req.Stage__c == 'Finalized';  
            }
        }
    }       

update wReqobj }

**

  • What you want to do, and what this code would actually do (assuming it were syntactically correct), are actually two different things. This code, if you fixed up all the syntax errors, would just "finalize" the request when any of the work orders close. That doesn't match the description of what you're trying to do. – sfdcfox Jul 5 '16 at 23:18
  • Thanks for catching that. is there an easy way to true that up in the logic? To check that all equal 'Closed'? – Clink Jul 5 '16 at 23:46
1

Common mistake.

You are using the object API name WorkOrder__c, and replacing the __c with __r, right?

It's not completely intuitve, but what you are actually supposed to use there is the Child Relationship name. You can find that by clicking Create > Objects and finding the Work Order object, going to the Work Request Lookup field, and look for the "Child Relationship Name". Use that name with __r appended to the end in both the SOQL query and the for loop, or anywhere else you want to reference the Work Order list on your individual Work Request query results.

  • Thanks. I checked though and the Child Relationship Name and API name are both WorkRequest__r. The lookup field actually sits on the WorkOrder__c object, so that when you are viewing a WorkOrder, you are able to see what WorkRequest it is associated with. – Clink Jul 5 '16 at 23:44

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.