Ask Search:
Jeremy BardetJeremy Bardet 

trigger to update child records (oppty -- > oppty line item...)

Problem:  Cannot do cross-object workflow
Prolbem to possible workaround:  Cannot fire workflow off formula field.

So I need a trigger that will force an "edit/save" (with no changes necessary) to opportunity products when an opportunity date field: "SO_Opened__c" is populated.

Any help, be it code itself or links to get me further along would be great.

Thanks!

Jeremy
Alfred DellicicchiAlfred Dellicicchi
Hi, probably too late for you but here's the jist of it for a general "parent object": 

-You want to do an "After Update" trigger. 
-You want to take the id of the parent object and use it to query for a list of child objects.
-Then you loop thru the children, comparing the updated field value of the parent to the children. (might always be different, but this is good practice for oother situations).
-If they are fdifferent, add each child to a list for updating.
-Update the list of modifed Children once you've checked all of them (this avoids updating records that don't need it, and it only counts against your DML limits as one  Update operation instead of
'X number of children'.

Trigger body>>

for( ParentObjectType parent: Trigger.new)
{

List<ChildObjectType> children = [ SELECT Id, ParentIdField, FieldToUpdate__c from
ChildObjectType where ParentIdField = :parent.Id];

List<ChildObjectType> childrenToUpdate = new List<ChildObjectType>();

for(ChildObjectType thischild: children )
{
   if( thischild.FieldToUpdate__c !=  parent.FieldToUpdate__c)
   {
       thischild.FieldToUpdate__c =  parent.FieldToUpdate__c;
      childrenToUpdate.add(thischild)
   }
}

if( !childrenToUpdate.isEmpty)
{
   update childrenToUpdate;
}

}

This code is basic, in that its written to handle the case where one Prent is edited at a time. But you should write triggers optimized for when multiple objects are updated, so you don't hit  the SQL query limits on triggers. If you were updating multiple Parent objects at once, you would hit the limit of 'SELECT statements per trigger' pretty quick.

To avoid that problem  - you'd want to do one big SQL query of all the updated parents and their children at once... instead of looping thru each parent  record in the trigger, one at a time, and querying for their children.

do something like
If(Trigger.isUpdate){
     
     Set<ID> ids = Trigger.new.keySet();
     list<ParentObjectType> updatedParents = [SELECT Id, 
                 (Select SELECT Id, ParentIdField, FieldToUpdate__c 
                  from ChildObjectType__r ) FROM ParentObjectType 
                 WHERE Id in :ids];
         List<ChildObjectType> childrenToUpdate = new List<ChildObjectType>();

         //Then loop through each parent object in 'updated parent
         for ( ParentObject Type p : updatedParents)
         {
                //and loop thru each kid in the child set}
               for(ChildObjectType kid : p.ChildObjectType__r)
               {
                         ///update logic from above
                          if( kid .FieldToUpdate__c !=  p.FieldToUpdate__c)
{
       kid .FieldToUpdate__c =  p.FieldToUpdate__c;
      childrenToUpdate.add(kid )
}
                }
        }
       if( !childrenToUpdate.isEmpty)
       {
            update childrenToUpdate;
       }

}
There is documentation in the Apex Docs about this - very handy to know or you'll be banging your head rewriting code in the future.

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_patterns_bulk.htm

ps -the exact name you would use to query the child records is determined by the relationship query. see docs for 'understand relationship names' 

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_calls_soql_relationships.htm?SearchType=Stem

Instead of ChildObjectType__r  it will be something like "Childs" 

Ramesh BabuRamesh Babu
i want to insert Child object field based on the Parent Object field value.
when Account Picklist Field Value Like (0-5, 5-10, 10-15) is inserted or updated, i need to update the Opportunity Text Field like (Min, Average, Max)

can any boby write trigger .

I writen trgiiger like :

trigger opptyupdate on Opportunity (after Insert, after Update) {
List<Opportunity> opp = [Select id, name,Account.Rank__c  FROM Opportunity WHERE opp IN :Trigger.new()];

   for (Opportunity Oppty : opp) {
    system.debug('====================== opp id :'+oppty.id);
 
    if (Oppty.Account.Rank__c == '0-5000') {Oppty.Rank_value__c = 'Min';}
    if (Oppty.Account.Rank__c == '5000-10000') {Oppty.Rank_value__c = 'Average';}
    if (Oppty.Account.Rank__c == '10000-50000') {Oppty.Rank_value__c = 'Max';}
}

}


But here error is like Variable does not exist: Trigger.New

Please help me out this