You need to sign in to do that
Don't have an account?
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
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:
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