You need to sign in to do that
Don't have an account?
Tamara Hay
Trigger not working when i add multiple of the same product
I have the below trigger which adds a dropdown called Provisioning_Detail__c when a product is added. This works fine when adding single product or different products, but if we want to add more than 1 or the same product, we need to click "Save" and go through the entire product selector again or the proisioning details object does not save to the product line item.
Can someone help me amend the code so we dont need to save and re-add the product every time.
trigger CreateProvisioningDetailNew on OpportunityLineItem (after insert) {
List<Provisioning_Detail__c> newProvDetailList = new List<Provisioning_Detail__c>();
Map<Id,opportunityLineItem> oppAndIdMap = new Map<Id,opportunityLineItem>();
Map<Id,Id> oppLineAndProvUpdateInfoMap = new Map<Id,Id>();
String oppAndProductName;
Database.SaveResult[] lsr;
Set<Id> insertedProvId = new Set<Id>();
for(opportunityLineItem oppl:[select id,opportunity.id,opportunity.name,pricebookEntry.Name from opportunityLineItem where id in :trigger.newMap.keyset()]){
oppAndIdMap.put(oppl.Id,oppl);
}
for(OpportunityLineItem oli:trigger.new){
if((oli.Provisioning_Detail__c==null) &&(oli.Provisioning_Detail_Required__c=='Yes')){
Provisioning_Detail__c newProvDet = new Provisioning_Detail__c();
newProvDet.Product_Family__c = oli.Product_Family__c;
//newProvDet.Opportunity__r.id = oli.Opportunity.id;
newProvDet.Opportunity__c=oli.OpportunityId;
newProvDet.Opp_Line_Item_ID__c = oli.id;
oppAndProductName =oppAndIdMap.get(oli.id).opportunity.name + oppAndIdMap.get(oli.id).pricebookEntry.Name;
newProvDet.Name =oppAndProductName.left(80);
newProvDet.Product_Name__c = oppAndIdMap.get(oli.id).pricebookEntry.Name;
newProvDetailList.add(newProvDet);
}
}
try{
lsr = database.insert(newProvDetailList);
}catch(DmlException de){
system.debug('---------->>>>>>>>>>>>>'+de.getMessage());
}
for(Database.SaveResult dsr : lsr){
insertedProvId.add(dsr.getId());
}
for(Provisioning_Detail__c p:[select id,Opp_Line_Item_ID__c from Provisioning_Detail__c where id in:insertedProvId]){
oppLineAndProvUpdateInfoMap.put(p.Opp_Line_Item_ID__c,p.id);
}
List<opportunityLineItem> oppLineItemList = new List<opportunityLineItem>([select id,Provisioning_Detail__c from opportunityLineItem where id in:oppLineAndProvUpdateInfoMap.keyset()]);
for(opportunityLineItem ol:oppLineItemList ){
ol.Provisioning_Detail__c = oppLineAndProvUpdateInfoMap.get(ol.Id);
}
try{
database.update(oppLineItemList);
}catch(DmlException dmll){
system.debug('------------>>>>>>>>>>>>>>>>>>'+dmll.getMessage());
Can someone help me amend the code so we dont need to save and re-add the product every time.
trigger CreateProvisioningDetailNew on OpportunityLineItem (after insert) {
List<Provisioning_Detail__c> newProvDetailList = new List<Provisioning_Detail__c>();
Map<Id,opportunityLineItem> oppAndIdMap = new Map<Id,opportunityLineItem>();
Map<Id,Id> oppLineAndProvUpdateInfoMap = new Map<Id,Id>();
String oppAndProductName;
Database.SaveResult[] lsr;
Set<Id> insertedProvId = new Set<Id>();
for(opportunityLineItem oppl:[select id,opportunity.id,opportunity.name,pricebookEntry.Name from opportunityLineItem where id in :trigger.newMap.keyset()]){
oppAndIdMap.put(oppl.Id,oppl);
}
for(OpportunityLineItem oli:trigger.new){
if((oli.Provisioning_Detail__c==null) &&(oli.Provisioning_Detail_Required__c=='Yes')){
Provisioning_Detail__c newProvDet = new Provisioning_Detail__c();
newProvDet.Product_Family__c = oli.Product_Family__c;
//newProvDet.Opportunity__r.id = oli.Opportunity.id;
newProvDet.Opportunity__c=oli.OpportunityId;
newProvDet.Opp_Line_Item_ID__c = oli.id;
oppAndProductName =oppAndIdMap.get(oli.id).opportunity.name + oppAndIdMap.get(oli.id).pricebookEntry.Name;
newProvDet.Name =oppAndProductName.left(80);
newProvDet.Product_Name__c = oppAndIdMap.get(oli.id).pricebookEntry.Name;
newProvDetailList.add(newProvDet);
}
}
try{
lsr = database.insert(newProvDetailList);
}catch(DmlException de){
system.debug('---------->>>>>>>>>>>>>'+de.getMessage());
}
for(Database.SaveResult dsr : lsr){
insertedProvId.add(dsr.getId());
}
for(Provisioning_Detail__c p:[select id,Opp_Line_Item_ID__c from Provisioning_Detail__c where id in:insertedProvId]){
oppLineAndProvUpdateInfoMap.put(p.Opp_Line_Item_ID__c,p.id);
}
List<opportunityLineItem> oppLineItemList = new List<opportunityLineItem>([select id,Provisioning_Detail__c from opportunityLineItem where id in:oppLineAndProvUpdateInfoMap.keyset()]);
for(opportunityLineItem ol:oppLineItemList ){
ol.Provisioning_Detail__c = oppLineAndProvUpdateInfoMap.get(ol.Id);
}
try{
database.update(oppLineItemList);
}catch(DmlException dmll){
system.debug('------------>>>>>>>>>>>>>>>>>>'+dmll.getMessage());
Sorry im not a developer so im not sure if my explanation is clear. We had an external company create all of this for us.
I hope this helps. Please let me know if you need further clarification.