function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MayankDkPantMayankDkPant 

Trigger on Same Object For Bulk insert

Hi,

 

My scenario:

 

I have a 4 picklist fields in opportunity and I have a program custom object which has same 4 picklist with same values.
I have a look-up relationship b/w opportunity and program. when user selects a program on opportunity level and clicks on save button I want the all 4 picklist values on opportunity object should be auto saved with their corresponding product values from program object.

 

My Problem: I have written a trigger that is working fine for one insert but it is not working for bulk insert.

My Trigger:

 

trigger trgautoPopulateProductVariation on Opportunity (before insert, before update)
{  
  Set<Id> idopp = new Set<Id>();
    Set<Id> idProg = new Set<Id>();


   for(Opportunity opp : trigger.new)
    {
        idopp.add(opp.id);        
        idProg.add(opp.Program__c);

    }
 
      List<Opportunity> oppList = [SELECT Id, Name,Product_Group__c, Product_Area__c,
                         Product_Family__c, Product_Family_Child__c  FROM opportunity
                       Where Id IN :idopp];    

    List<Program__c> progList = [Select Id,Product_Group__c,
                   Product_Area__c, Product__c, Product_Family_Child__c
                   From Program__c Where Id IN :idProg];
                   
   for(Opportunity opp : trigger.new)
   { Integer i=0;
     for(Program__c prog: progList)
              {
                   if(prog.id==opp.Program__c)
                    {
                        trigger.new[i].Product_Group__c =prog.Product_Group__c;
                        trigger.new[i].Product_Area__c = prog.Product_Area__c;
                        trigger.new[i].Product_Family__c = prog.Product__c;
                        trigger.new[i].Product_Family_Child__c = prog.Product_Family_Child__c;
                        System.debug('I value = '+i);
                     }
              }  
              
       i++;
   }
       
 }

 

Can any one help me out on this. Any help will be appreciated.

 

With Regards,

Mayank Pant

 

SFAdmin5SFAdmin5

requirements aren't totally clear here, but, assuming you have the lookup field on the opp object, looking up to the Program__c object, this trigger is essentially one that takes 4 child field values and stamps those values on the 4 corresponding fields on the parent when hte child is updated or inserted.  let me know if this works.  this works for me in a dev environment as it is, but again i'm not sure exactly what your requirements are:

 

trigger setParentFieldsFromChildTrigger on Opportunity (before insert, before update) 
{
          
     List<Program__c> programList = new List<Program__c>();
     
     Set<id> Ids = new Set<id>();
     for (Opportunity o : Trigger.new) {
         Ids.add(o.Program__c);
        }
     
     Map<id, Program__c> owners = new Map<id, Program__c>([Select Id,Product_Group__c,Product_Area__c,Product_Family__c,Product_Family_Child__c from Program__c Where Id in :Ids]);  
     
     for (Opportunity o : Trigger.new) 
     {
                 Program__c prgrm= owners.get(o.Program__c);
                 prgrm.Product_Group__c = o.Product_Group__c;
                 prgrm.Product_Area__c = o.Product_Area__c;
                 prgrm.Product_Family__c = o.Product_Family__c;
                 prgrm.Product_Family_Child__c = o.Product_Family_Child__c;
                 programList.add(prgrm);

     }
     update programList;
     }

 

SFAdmin5SFAdmin5

re-reading the requirements I'm thinking maybe you want this trigger on the custom object and not on opportunity, actually.  i'm thinking maybe the trigger should populate the 4 opp fields when a program custom obecjt record's inserted or updated and associated to the opp?  again it's not clear, but this trigger will populate the 4 opp fields from the program__c field values for an associated program__c record

 

trigger setParentFieldsFromChildTriggeronProgram on Program__c (before insert, before update) 
{
          
     List<Opportunity> oppList = new List<Opportunity>();
     
     Set<id> Ids = new Set<id>();
     for (Program__c prgm : Trigger.new) {
         Ids.add(prgm.Opportunity__c);
        }
     
     Map<id,Opportunity> oppMap = new Map<id,Opportunity>([Select Id,Product_Group__c,Product_Area__c,Product_Family__c,Product_Family_Child__c from Opportunity Where Id in :Ids]);  
     
     for (Program__C prgm : Trigger.new) 
     {

                 Opportunity o = oppMap.get(prgm.Opportunity__c);
                 o.Product_Group__c = prgm.Product_Group__c;
                 o.Product_Area__c = prgm.Product_Area__c;
                 o.Product_Family__c = prgm.Product_Family__c;
                 o.Product_Family_Child__c = prgm.Product_Family_Child__c;
                 oppList.add(o);

     }
     update oppList;
     }