Skip to main content
Hi all, I have a JavaScript button that sits on the Opportunity Product object. When clicked it creates a new record on a custom object Called Opportunity Product Mirror, creating a record and copying the selected fields to that new record. This process works great for each line item but it does require the user to have to go into each opportunity line item record and hit that button, which on some opportunities might be 20-50 line items. I wonder if someone could help me/advise me if its possible to create a similar process from a button that sits at the opportunity level, when clicked it creates an Opportunity Product Mirror record for each line item on the opportunity? That would be a massive time saver for the users involved. I have included the code from the current button below:

 

    {!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 

 

    var subMirrorId = '{!OpportunityLineItem.Opportunity_Product_MirrorId__c}'; 

 

    if(subMirrorId == null || subMirrorId == ''){ 

 

    var oppProduct = new sforce.SObject("OpportunityLineItem"); 

 

    var oppProdMirror = new sforce.SObject("Opportunity_Product_Mirror__c"); 

 

    var mirrorLookup = '{!OpportunityLineItem.Opportunity_Product_MirrorId__c}'; 

 

    oppProduct.Id = '{!OpportunityLineItem.Id}'; 

 

    var queryRes = sforce.connection.query("Select UnitPrice, Quantity, TotalPrice, Product2Id, CurrencyIsoCode, SB_Quantity__c, Segment_Index__c, Description, Start_Date__c, End_Date__c, OpportunityId From OpportunityLineItem Where Id = '" + oppProduct.Id + "' LIMIT 1"); 

 

    var records = queryRes.getArray("records"); 

 

    oppProdMirror.Quantity__c = records[0].Quantity; 

 

    oppProdMirror.Sales_Price__c = records[0].UnitPrice; 

 

    oppProdMirror.Opportunity_Product_Id__c = '{!OpportunityLineItem.Id}'; 

 

    oppProdMirror.Product__c = records[0].Product2Id;

 

    oppProdMirror.Opportunity__c = records[0].OpportunityId;

 

    oppProdMirror.Total_Price__c = records[0].TotalPrice;

 

    oppProdMirror.CurrencyIsoCode = records[0].CurrencyIsoCode;

 

    oppProdMirror.SBQQ_Quantity__c = records[0].SB_Quantity__c;

 

    oppProdMirror.Description__c = records[0].Description;

 

    oppProdMirror.Segment_Index__c = records[0].Segment_Index__c;

 

    oppProdMirror.Start_Date__c= records[0].Start_Date__c;

 

    oppProdMirror.End_Date__c = records[0].End_Date__c;

 

    var oppProductMirrorInsertRes = sforce.connection.create([oppProdMirror]); 

 

    if(oppProductMirrorInsertRes[0].getBoolean("success")){ 

 

    oppProduct.Opportunity_Product_Mirror__c = oppProductMirrorInsertRes[0].id; 

 

    var oppProdUpdRes = sforce.connection.update([oppProduct]); 

 

    if(oppProdUpdRes[0].getBoolean("success")){ 

 

    window.top.location.href = '/' + oppProductMirrorInsertRes[0].id 

 

    } 

 

    else{ 

 

    alert('Error creating sub item container: ' + oppProdUpdRes[0]); 

 

    } 

 

    } 

 

    else{ 

 

    alert('Error creating sub item container: ' + oppProductMirrorInsertRes[0]); 

 

    } 

 

    } 

 

    else{ 

 

    window.top.location.href = '/' + subMirrorId; 

 

    }

 

 
1 answer
0/9000