You need to sign in to do that
Don't have an account?
Tim Andrews
Help with simple cross-object field update trigger
Hello all, I am new to Apex and am trying to do a simple field update between two custom objects; I looked through other posts and tried to model mine on the others I saw, but I'm getting stuck. In a nutshell, when a date is entered into the Date_Received__c field on the Invoices__c object, that date should be written to the Invoice_Received_Date__c field on the Work_Products__c object. Work_Products__c is linked to Invoices__c via a lookup relationship. Below is the code I have written:
trigger DateReceivedUpdate on Invoices__c (before insert, before update){
List<ID> WorkProductIds = New List<ID>();
for(Invoices__c a : Trigger.new){
if(a.Date_Received__c != null){
WorkProductIds.add(a.Work_Product_Name__c);
}
}
List<Invoices__c> WorkProductsList = [SELECT id, Date_Received__c FROM Invoices__c WHERE id in :WorkProductIds];
Map<id,Invoices__c> mInvtoWP = New Map<id,Invoices__c>();
for(Work_Products__c p : trigger.new)
mInvtoWP.put(p.Invoice_Received_Date__c, p);
for(integer i = 0 ; i < WorkProductList.size(); i++){
WorkProductsList[i].Work_Products__c = mInvtoWP.get(WorkProductsList[i].ID).Invoices__c;
}
update WorkProductsList;
}
Any help that I can get would be greatly appreciated.
Thanks!
trigger DateReceivedUpdate on Invoices__c (before insert, before update){
List<ID> WorkProductIds = New List<ID>();
for(Invoices__c a : Trigger.new){
if(a.Date_Received__c != null){
WorkProductIds.add(a.Work_Product_Name__c);
}
}
List<Invoices__c> WorkProductsList = [SELECT id, Date_Received__c FROM Invoices__c WHERE id in :WorkProductIds];
Map<id,Invoices__c> mInvtoWP = New Map<id,Invoices__c>();
for(Work_Products__c p : trigger.new)
mInvtoWP.put(p.Invoice_Received_Date__c, p);
for(integer i = 0 ; i < WorkProductList.size(); i++){
WorkProductsList[i].Work_Products__c = mInvtoWP.get(WorkProductsList[i].ID).Invoices__c;
}
update WorkProductsList;
}
Any help that I can get would be greatly appreciated.
Thanks!
If you have access to the Process Builder, you should use that instead of a trigger. I believe your requirements can be accomplished using Process Builder and therefore avoid using code.
That being said, here is how you would accomplish it with a trigger. I am making a few assumptions since you did not provide this information.
- Invoice is the parent object and Work Products is the child object.
- This means that there is a lookup field on the Work Products object that relates it to an Invoice.
- I assumed the the lookup field on the Work Products object is called "Invoice123__c". Im pretty sure that is not the case, so you will have to update any reference to "Invoice123__c" with the correct field API name.
Trigger code:Good luck!!
If that is the case, you could run into a situation in which one "Work Product" can have many Invoice childs. You will have to decide which Date_Received__c of the child Invoices you will be using to set the Invoice_Received_Date__c on the Work Product.
Keep that in mind. I think in terms of the code, you will to make some modifications to make it work.
I'm here if you have any questions.