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
Heather_HansonHeather_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!!
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;
				}
			}
		}
	}

}

 
Best Answer chosen by Heather_Hanson
Raj VakatiRaj Vakati
Finally Got it 

Use this code Work_Orders__r is correct  
 
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_Orders__r 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 = Acc.Work_Orders__r ;
			 if(WO.size() == 1){
					pm.Installation_WO__c = WO[0].Id;
				}
			}
		}
	}

}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
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__r 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 = Acc.Work_Order__r ;
				if(WO.size() == 1){
					pm.Installation_WO__c = WO[0].Id;
				}
			}
		}
	}

}

 
Raj VakatiRaj Vakati
Refer this link  
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
Heather_HansonHeather_Hanson
Hi Raj,

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

 
Raj VakatiRaj Vakati
Can u give me two screen show 

1 . Account Custome field API NAme for Workorder 

and Workorder  field details on account 
Heather_HansonHeather_Hanson
Hi Raj,

Here is the account field API name in Work Order:

User-added image

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.
Raj VakatiRaj Vakati
Can you click on the above account field and give me the Details ( i wanted tosee the relationship field names)
Heather_HansonHeather_Hanson
Sorry!  See below...

User-added image
Raj VakatiRaj Vakati
Finally Got it 

Use this code Work_Orders__r is correct  
 
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_Orders__r 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 = Acc.Work_Orders__r ;
			 if(WO.size() == 1){
					pm.Installation_WO__c = WO[0].Id;
				}
			}
		}
	}

}

 
This was selected as the best answer
Heather_HansonHeather_Hanson
Raj!  My hero!  It works beautifully! So, other than the other tweaks you did, it was that when referencing the related list, you need to use the plural (so Work_Orders__r instead of Work_Order__r), right?

Thanks again!