You need to sign in to do that
Don't have an account?
Heather_Hanson
Help with Apex Trigger to pull Account Child ID into another Account related custom object based on field value
I'm still reallly new to triggers. I have 2 custom objects, "Work_Order__c" and "OrderProject__c", Work Order is a child of Account and Order Project is just related to Account with a look up field.
Order Project and Work Order are also related. I have a related list for Work Order in the Order Project object. So Work Orders are created from an Order Project.
I created a field in Order Project called "Installatin_WO__c". I need this field to be populated with the "Name" of the Work Order in the related list found in the Order Project that has the Work_Order_Type__c of "Installation".
I've only ever successfully created one trigger before and it is very similar to what I want to accomplish here, but I'm running into a couple of errors so I think I may be lacking understanding....
Below is what I have and the 2 error messages I have are:
Line: 9 Name, Work_Order_Type__c from Work_Order__c Where Work_Order_Type__c
^
ERROR at Row:1:Column:60
Didn't understand relationship 'Work_Order__c' 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. Please reference your WSDL or the describe call for the appropriate names.
AND
Line 17: Variable does not exist: WO
Any help correcting this would be greatly appreciated!!
Order Project and Work Order are also related. I have a related list for Work Order in the Order Project object. So Work Orders are created from an Order Project.
I created a field in Order Project called "Installatin_WO__c". I need this field to be populated with the "Name" of the Work Order in the related list found in the Order Project that has the Work_Order_Type__c of "Installation".
I've only ever successfully created one trigger before and it is very similar to what I want to accomplish here, but I'm running into a couple of errors so I think I may be lacking understanding....
Below is what I have and the 2 error messages I have are:
Line: 9 Name, Work_Order_Type__c from Work_Order__c Where Work_Order_Type__c
^
ERROR at Row:1:Column:60
Didn't understand relationship 'Work_Order__c' 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. Please reference your WSDL or the describe call for the appropriate names.
AND
Line 17: Variable does not exist: WO
Any help correcting this would be greatly appreciated!!
trigger PM_WO_Installation on OrderProject__c (before insert, before update) { set<Id> AccountIds = new set<Id>(); for (OrderProject__c pm: trigger.new) { AccountIds.add(pm.Account_ID__c); } map<Id, Account> AccountWOMap = new map<Id, Account>(); list<Account> AccWOs = new list<Account>(); AccWOs = [Select Id, Name, (Select Id, Name, Work_Order_Type__c from Work_Order__c Where Work_Order_Type__c = 'Installation') from Account where Id IN : AccountIds]; for(Account acc : AccWOs){ AccountWOMap.put(acc.Id, acc); } for (OrderProject__c pm: trigger.new) { if(pm.Installation_WO__c == null){ Account Acc = AccountWOMap.get(pm.Account_ID__c); if(Acc != null){ List<Work_Order__c> WO = new list<Work_Order__c>(Acc.WO); if(WO.size() == 1){ pm.Installation_WO__c = WO[0].Id; } } } } }
Use this code Work_Orders__r is correct
All Answers
to Understanding Relationship Names, Custom Objects, and Custom Fields
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm
Thanks for the help! I had tried using __r for the Work Order reference, but couldn't figure it out. I tried using your code and I'm still getting the same error messages (see below):
I've checked my APIs several times and Work Order is definitely a child of Account....any other suggestions?
Name, Work_Order_Type__c from Work_Order__r Where Work_Order_Type__c
^
ERROR at Row:1:Column:60
Didn't understand relationship 'Work_Order__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. Please reference your WSDL or the describe call for the appropriate names.
and
Variable does not exist: Work_Order__r
1 . Account Custome field API NAme for Workorder
and Workorder field details on account
Here is the account field API name in Work Order:
We don't have a Work Order field in account...we just have the related list. Could that be my problem? It's a related list because there can be many work orders for one account.
Use this code Work_Orders__r is correct
Thanks again!