Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Trigger Help Needed - Update Child field based on ...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Trigger Help Needed - Update Child field based on update to Parent record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 11:37 AM
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!
Solved! Go to Solution.
Re: Trigger Help Needed - Update Child field based on update to Parent record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 09:21 PM
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.
trigger tgrA on A__c (after insert, after update){
// 1
List<B__c> childRecords = [Select A__c, Record_Mode__c FROM B__c WHERE A__c IN Trigger.newMap.keySet()];
// 2
for(B__c child :childRecords){
// 3
if(trigger.isInsert && child.Status__c == 'Open'){
child.Record_Mode__c = 'Edit';
}
// 4
else if(trigger.isUpdate && child.Status == 'Open' && trigger.NewMap.get(child.A__c).Status__c != trigger.OldMap.get(child.A__c).Status__c){
child.Record_Mode__c = 'Edit';
}
}
// 5
if(childRecords.size() > 0)
update childRecords;
}
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.
Kudos a post if it helps, mark it as solution if it solves.
http://exploretheforce.blogspot.in
Re: Trigger Help Needed - Update Child field based on update to Parent record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-07-2013 08:48 AM
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".
trigger tgrMCCReq on MCC_Request__c (after insert, after update){
// 1
List<MCC_Relationship__c> childRecords = [Select MCC_Request__c, Record_Mode__c FROM MCC_Relationship__c WHERE MCC_Request__c IN Trigger.newMap.keySet()];
// 2
for(MCC_Relationship__c child :childRecords){
// 3
if(trigger.isInsert && child.Status__c == 'Open'){
child.Record_Mode__c = 'Edit';
}
// 4
else if(trigger.isUpdate && child.Status == 'Open' && trigger.NewMap.get(child.MCC_Request__c).Status__c != trigger.OldMap.get(child.MCC_Request__c).Status__c ){
child.Record_Mode__c = 'Edit';
}
}
// 5
if(childRecords.size() > 0)
update childRecords;
}
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!
Re: Trigger Help Needed - Update Child field based on update to Parent record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-07-2013 10:32 PM
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!
Kudos a post if it helps, mark it as solution if it solves.
http://exploretheforce.blogspot.in
Re: Trigger Help Needed - Update Child field based on update to Parent record
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
03-15-2013 03:25 PM
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.
trigger ContractStatusUpdatesAsset on Contract (after insert, after update)
{
// 1
List<Asset> childAsset = [Select Contract, Status FROM Asset WHERE Asset IN: Trigger.newMap.keySet()];
// 2
for(Asset child :childAsset)
{
// 3
//if(trigger.isInsert && child.Status == 'Active', 'Pending')
// {
// child.Status = 'Inactive';
// }
// 4
//else
if(trigger.isUpdate && child.Status == 'Active', 'Pending' && trigger.NewMap.get(child.Contract).Status != trigger.OldMap.get(child.Contract).Status)
{
child.Status = 'Inactive';
}
}
// 5
if(childRecords.size() > 0)
{
//System.debug('Chris has values to insert = '+ newConList.size());
try
{
update childAsset;
}
catch (System.Dmlexception e)
{
system.debug (e);
}
}
}
}
cbro
Certified Salesforce Admin
Connect with me
Linked In: www.linkedin.com/in/chrisbroach/

