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
Tamara HayTamara 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,,,pricebookEntry.Name  from opportunityLineItem where id in :trigger.newMap.keyset()]){
        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.Opp_Line_Item_ID__c =;
            oppAndProductName =oppAndIdMap.get( + oppAndIdMap.get(; 
            newProvDet.Name =oppAndProductName.left(80);
            newProvDet.Product_Name__c = oppAndIdMap.get(; 
       lsr = database.insert(newProvDetailList);
    }catch(DmlException de){
    for(Database.SaveResult dsr : lsr){
    for(Provisioning_Detail__c p:[select id,Opp_Line_Item_ID__c  from Provisioning_Detail__c where id in:insertedProvId]){
   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);
   }catch(DmlException dmll){
ShashankShashank (Salesforce Developers) 
Can you please elaborate a little on the object relationships between opportunity, opportunitylineitem and provisioning_detail__c in your scenario so that I can understand it better and help? Also, please let me know if there is any VF involved in the whole process.
Tamara HayTamara Hay
HI. Firstly, yes there is visualforce for our product selector (our salesforce is quite customised). We have our products add page work on a filter based selector. When we click on "Add product" we are taken to the visualforce page which selects products and groups them together. Then each products has associated provisioning details that are field sets based on the product family.

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.