0

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; 
}

I have an update on this. I've made some edits based on the advice below and I am now receiving a different error message:

enter image description here

For reference here is what my script now looks like:

{!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 OpportunityId = '{!Opportunity.Id}'"); 
var newRecords = []; 
var oppProdMirror = new sforce.SObject("Opportunity_Product_Mirror__c"); 
var records = queryRes.getArray("records"); 
for (var i=0; i< records.length; i++) { 
oppProdMirror.Quantity__c = records[i].Quantity; 
oppProdMirror.Sales_Price__c = records[i].UnitPrice; 
oppProdMirror.Opportunity_Product_Id__c = '{!OpportunityLineItem.Id}'; 
oppProdMirror.Product__c = records[i].Product2Id; 
oppProdMirror.Opportunity__c = records[i].OpportunityId; 
oppProdMirror.Total_Price__c = records[i].TotalPrice; 
oppProdMirror.CurrencyIsoCode = records[i].CurrencyIsoCode; 
oppProdMirror.SBQQ_Quantity__c = records[i].SB_Quantity__c; 
oppProdMirror.Description__c = records[i].Description; 
oppProdMirror.Segment_Index__c = records[i].Segment_Index__c; 
oppProdMirror.Start_Date__c= records[i].Start_Date__c; 
oppProdMirror.End_Date__c = records[i].End_Date__c; 
newRecords.push(oppProdMirror); 
} 
var oppProdUpdRes = sforce.connection.update(newRecords); 

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; 
}
3

The basic change you need to make is to your query. Once you move this button to the Opportunity object, change:

"... From OpportunityLineItem Where Id = '" + oppProduct.Id + "' LIMIT 1"

To:

"... FROM OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}'"

Note that you change the field from Id to OpportunityId and remove the LIMIT clause. You will also have to change your data types so that you have a list of mirror objects, but it should be pretty straightforward once you change the query.

  • That makes sense. What do you mean by " You will also have to change your data types so that you have a list of mirror objects'" ? – user3480962 Jan 17 '17 at 13:07
  • @user For instance, right now you have var oppProdMirror = new sforce.SObject("..."). Instead it would be var oppProdMirrors = [] and then you would populate the array with individual mirror records. – Adrian Larson Jan 17 '17 at 13:12
  • thanks for replying. I know I'm being a pain but I'm still not quite sure what I'd need to edit in the array. – user3480962 Jan 17 '17 at 14:09
0

Try this.

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 OpportunityId = '{!Opportunity.Id}'"); 
var newRecords = [];
var oppProdMirror = new sforce.SObject("Opportunity_Product_Mirror__c"); 
var records = queryRes.getArray("records"); 
for (var i=0; i< records.length; i++) {
    oppProdMirror.Quantity__c = records[i].Quantity; 
oppProdMirror.Sales_Price__c = records[i].UnitPrice; 
oppProdMirror.Opportunity_Product_Id__c = '{!OpportunityLineItem.Id}'; 
oppProdMirror.Product__c = records[i].Product2Id;
oppProdMirror.Opportunity__c = records[i].OpportunityId;
oppProdMirror.Total_Price__c = records[i].TotalPrice;
oppProdMirror.CurrencyIsoCode = records[i].CurrencyIsoCode;
oppProdMirror.SBQQ_Quantity__c = records[i].SB_Quantity__c;
oppProdMirror.Description__c = records[i].Description;
oppProdMirror.Segment_Index__c = records[i].Segment_Index__c;
oppProdMirror.Start_Date__c= records[i].Start_Date__c;
oppProdMirror.End_Date__c = records[i].End_Date__c;
    newRecords.push(oppProdMirror);
}
var oppProdUpdRes = sforce.connection.create(newRecords); 
  • Is this replacing a portion of the script? – user3480962 Jan 18 '17 at 11:08
  • @user3480962 I didn't add full code as rest of the code is same. – Reshma Jan 18 '17 at 11:10
  • I thought that was the case but unfortunately I'm getting an error: "OppProductMirrorInsertRes not defined" – user3480962 Jan 23 '17 at 15:36
  • @user3480962 Did you miss declaring OppProductMirrorInsertRes? – Reshma Jan 24 '17 at 3:43
  • i believe i have it in but I am getting a different error. I have updated my original post with the new script to you can see it in full. I really appreciate your help – user3480962 Jan 24 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.