+ Start a Discussion
MystwalkerMystwalker 

trigger for updating parent information

I've tried a couple solutions found on the boards but I always seem to get CANNOT_INSERT_UPDATE.. error when I try to update a field on a parent object from a ''after update'' or ''after insert'' trigger on the child (''before update'' or ''before insert'' doesn't work).

 

I've put an quick example of how I've made my trigger:

 

trigger opp_update on Child_Object__c (after update, after insert) {

for( Child_Object__c child: Trigger.new){
 ID parent_ID= child.Opportunity__c;
 

Opportunity opp_to_update= [select field_to_update__c from Opportunity where id =: parent_ID];
 opp_to_update.
field_to_update__c = 7777;
 

update opp_to_update;
 }
 
}

 

Does someone have a quick tip to help me solve this?

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Unless I'm missing something obvious (which is possible, it's late and I should be in bed...)  The Trigger is on Child_Object__c, not Opportunity.   Updating an Opportunity record from within this trigger should be fine, at least in terms of infinite looping.

 

You should add some error handling to your code to check for errors and process them correctly.

 

I'm wondering if the Update is failing because because you didn't select the ID in the list of select fields, the resulting Opportunity objects don't have ID's in them.

 

Lastly, you need to read up on bulk-safe triggers.  You have a Select statement within a loop.  Instead use an "in" expression in the "where" clause.  As in (off the top of my head):

 

 

trigger opp_update on Child_Object__c(after update, after insert) {

Set <Id> Opp_Ids = new Set<Id>();

for (Child_Object__c child: trigger.new) Opp_Ids.add(child.Opportunity__c);

 

List<Opportunity> Opps_To_Update = [select o.id, o.field_to_update__c from Opportunity o where o.id in :Opp_Ids]

for(Opportunity o: Opps_To_Update) o.field_to_update__c = 7777;

update Opps_To_Update;

 

best, Steve.

All Answers

ExecbizExecbiz
Is this a message you get in the SF window?  or does the code not even compile?
Siddhesh KabeSiddhesh Kabe

Dude are you kidding?? Writing a Update statement into a After Update trigger????

 

trigger opp_update on Child_Object__c (after update, after insert) {

for( Child_Object__c child: Trigger.new){
 ID parent_ID= child.Opportunity__c;
 

Opportunity opp_to_update= [select field_to_update__c from Opportunity where id =: parent_ID];
 opp_to_update.
field_to_update__c = 7777;
 

update opp_to_update; <-- This staement here will fire the trigger infite times boss..
 }
 
}

 

I will suggest go ahead and think of some new logic for the same!!!!

 

SteveBowerSteveBower

Unless I'm missing something obvious (which is possible, it's late and I should be in bed...)  The Trigger is on Child_Object__c, not Opportunity.   Updating an Opportunity record from within this trigger should be fine, at least in terms of infinite looping.

 

You should add some error handling to your code to check for errors and process them correctly.

 

I'm wondering if the Update is failing because because you didn't select the ID in the list of select fields, the resulting Opportunity objects don't have ID's in them.

 

Lastly, you need to read up on bulk-safe triggers.  You have a Select statement within a loop.  Instead use an "in" expression in the "where" clause.  As in (off the top of my head):

 

 

trigger opp_update on Child_Object__c(after update, after insert) {

Set <Id> Opp_Ids = new Set<Id>();

for (Child_Object__c child: trigger.new) Opp_Ids.add(child.Opportunity__c);

 

List<Opportunity> Opps_To_Update = [select o.id, o.field_to_update__c from Opportunity o where o.id in :Opp_Ids]

for(Opportunity o: Opps_To_Update) o.field_to_update__c = 7777;

update Opps_To_Update;

 

best, Steve.

This was selected as the best answer
KrishnadasKrishnadas

Hi, From my various trials to make a successfull update of a record you need to pass the Id values too. In your case you would have to append the following 


Opportunity opp_to_update= [select field_to_update__c from Opportunity where id =: parent_ID];
opp_to_update.field_to_update__c = 7777;

 
to


Opportunity opp_to_update =[select Id, field_to_update__c from Opportunity where Id = :parent_Id];
opp_to_update.Id = parent_ID;
opp_to_update.field_to_update__c = 7777;
update opp_to_update; 

 

Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com
 

MystwalkerMystwalker

Selecting the Id in the query was apparently the only thing missing in my trigger, looks like it was much more simple than I tough.

 

Thanks!