You need to sign in to do that
Don't have an account?

Cross object field update
This is my first attempt at an APEX trigger or any kind of Apex.
I am trying to update a field in a child object from a field in a parent object. It is giving an error on line 4: Parent_Object.Parent_Custom_Field__r variable is not found
ie a Parent_Custom_Field__c from Parent_Object__c into the Child_Object__c Child_Custom_Field__c
trigger NEWTRIGGER on Child_Object__c (before insert) {
For (Child_Object__c ChildField : Trigger.new)
{ChildField.Child_Custom_Field=Parent_Object.Parent_Custom_Field__r;
}
}
I am trying to update a field in a child object from a field in a parent object. It is giving an error on line 4: Parent_Object.Parent_Custom_Field__r variable is not found
ie a Parent_Custom_Field__c from Parent_Object__c into the Child_Object__c Child_Custom_Field__c
trigger NEWTRIGGER on Child_Object__c (before insert) {
For (Child_Object__c ChildField : Trigger.new)
{ChildField.Child_Custom_Field=Parent_Object.Parent_Custom_Field__r;
}
}
You will need to build up a set of parent record Ids in order to query for these parent records, as they will not be part of your trigger data by default. I would advise querying them directly into a Map<Id, Parent_Object__c> as this will automatically put the Parent Object's Id values as the key, and the Parent Record as the value.
Then by iterating over the child records in trigger.new, you can use the parent record's Id to get the necessary parent record from the map, then update the child record with the appropriate value.
Hope this helps you get going!
All Answers
You will need to build up a set of parent record Ids in order to query for these parent records, as they will not be part of your trigger data by default. I would advise querying them directly into a Map<Id, Parent_Object__c> as this will automatically put the Parent Object's Id values as the key, and the Parent Record as the value.
Then by iterating over the child records in trigger.new, you can use the parent record's Id to get the necessary parent record from the map, then update the child record with the appropriate value.
Hope this helps you get going!
Apex trigger CAPANC caused an unexpected exception, contact your administrator: CAPANC: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.CAPANC: line 15, column 1
Trigger.new in an After Update, After Insert, or After Undelete trigger is the version of the record that is already in the database, and does not get saved to the database again. If your trigger needs to be After Update, then you will need to collect the records you want to update into a separate list and perform an update on that list. (This would require some additional work also, because that update would fire the same trigger again).
Documentation on this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
Something else comes to mind, the work of this trigger could also be accomplished with a workflow rule/field update, because you are traversing up to the parent record to get another of its values and writing it to the child record. Your field update formula would look like this: "Parent_Object__c.Parent_Custom_Field__c", would be set to update "Child_Custom_Field__c", and the workflow rule could be set to fire whenever the record is updated or when it is updated and the Child_Custom_Field__c is null.