You need to sign in to do that
Don't have an account?
Trigger Help Needed - Update Child field based on update to Parent record
Hi, I'm fairly new to Triggers and am hoping someone can give me a bit of direction.
I want a custom field "Record_Mode__c" on Child Object B to be updated to value "Edit" when Parent Object A's custom field "Status__c" is updated to "Open". Basically, any time the parent record's status changes, I want the child record's Record Mode field to be updated accordingly.
The Record Mode change will then drive workflow that will flip record types and page layouts. I prefer to use out of the box solutions, but I cannot use cross object workflows as there is no master detail relationship between Parent Object A and Child Object B.
Any assistance with code or alternate solutions are much appreciated!
As you correctly stated that Workflows will not serve your purpose here, here is a code example for you:
I want a custom field "Record_Mode__c" on Child Object B to be updated to value "Edit" when Parent Object A's custom field "Status__c" is updated to "Open". Basically, any time the parent record's status changes, I want the child record's Record Mode field to be updated accordingly.
1. Querying child records from B Object related to A records that are being edited, inserted.
2. Iterating through all the child records and updating them as per the conditions.
3. If it is an insert, we simply check the status and if it meets the condition, update the child
4. If it is an update, we check if the Status value has been changed. If yes, then we update the child
5. Update all the related child records
This should help you get started.
All Answers
As you correctly stated that Workflows will not serve your purpose here, here is a code example for you:
I want a custom field "Record_Mode__c" on Child Object B to be updated to value "Edit" when Parent Object A's custom field "Status__c" is updated to "Open". Basically, any time the parent record's status changes, I want the child record's Record Mode field to be updated accordingly.
1. Querying child records from B Object related to A records that are being edited, inserted.
2. Iterating through all the child records and updating them as per the conditions.
3. If it is an insert, we simply check the status and if it meets the condition, update the child
4. If it is an update, we check if the Status value has been changed. If yes, then we update the child
5. Update all the related child records
This should help you get started.
Vishal,
First, thank you for your reply, your assistance is appreciated!
I plugged in the code you provided and am receiving an error "Error: Compile Error: unexpected token: 'Trigger.newMap.keySet' at line 4 column 137".
Below is updated parent/child information... maybe I entered information incorrectly?
Parent Object: MCC_Request__c
Parent Object Field: Status__c
Child Object: MCC_Relationship__c
Child Object Field: Record_Mode__c
Logic I'm after: Any time MCC_Request__c.Status__c is changed/updated, if MCC_Request__c.Status__c = "Draft" or "Rejected", the MCC_Relationship__c.Record_Mode__c should be updated to "EDIT", otherwise, the MCC_Relationship__c.Record_Mode__c should be updated to "READ"
Hope this makes sense and thanks again for your help!
oops, my bad! Sorry.
List<MCC_Relationship__c> childRecords = [Select MCC_Request__c, Record_Mode__c FROM MCC_Relationship__c WHERE MCC_Request__c IN :Trigger.newMap.keySet()];
I missed out the colon (:) in the SOQL.
Let me know if any questions/ issues!
I am having an issue with my trigger. The problem is probably that the Object B (Asset) is NOT a child record, but a related object via a lookup field.
Can anyone help with this code?
It is not compiling b/c of it, I guess: Save error: No such column 'Contract' on entity 'Asset'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
Below is my trigger
string OnHold = 'On Hold';
string HoldwithResume = 'Hold with Resume Date';
string Finished = 'Finished';
string OnProgram = 'On Program';
//Loop through all records in the Trigger.new collection
List<Opportunity> opportunity = [SELECT Id, Name, Account.Program_Status__c FROM Opportunity where Account.Id in :accountNewMap.keySet() Order By CreatedDate Desc LIMIT 1];
for(Opportunity accountsWithOpps : opportunity)
{
//opps.Id = accountsWithOpps.Id;
if(accountsWithOpps.Account.Program_Status__c == OnHold || accountsWithOpps.Account.Program_Status__c == HoldwithResume)
{
accountsWithOpps.StageName = 'On Hold';
}
else if(accountsWithOpps.Account.Program_Status__c == Finished || accountsWithOpps.Account.Program_Status__c == 'Never Started')
{
accountsWithOpps.StageName = 'Closed Lost';
accountsWithOpps.CloseDate = Date.today();
}
else if(accountsWithOpps.Account.Program_Status__c == OnProgram)
{
accountsWithOpps.StageName = 'On Program';
}
}
update opportunity;
Here Account is my parent object and Opportunity is my child object. What is my problem is If the record size is 1 means the update is working perfectly. But if we update from data loader it is updating the first record in file only. remaining records are not updated in child object.