0

This code queries the current opportunity's line item Name, Quantity, TotalPrice and Description and places them into custom fields and executes without error.

The problem is it only places the first two characters of the Name. The receiving field is a text (100) field

By commenting out this line, it works as expected but I don’t know how to fix it. I was graciously helped with this by the community so I am not sure what this line is supposed to do or if I even need it

if(strProductNames.length>0){ strProductNames = strProductNames.substring(0,strProductNames.length-2); } ​

Here is the code in its entirety

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

var record = new sforce.SObject("Opportunity"); 
record.Id = '{!Opportunity.Id}'; 


result = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice, Description, ServiceDate From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')"); 
records = result.getArray("records"); 

var strProductNames = ''; 
var strProductQuantity = ''; 
var strProductPrice = ''; 
var strDescription = ''; 
var strServiceDate = ''; 
for(var i=0; i<records.length ; i++){ 
strProductNames = records[i].PricebookEntry.Product2.Name 
strProductQuantity = records[i].Quantity 
strProductPrice = records[i].TotalPrice 
strDescription = records[i].Description 
strServiceDate = records[i].ServiceDate; 
} 

if(strProductNames.length>0){ 
strProductNames = strProductNames.substring(0,strProductNames.length-2); 
} 
record.Sample_Name__c = strProductNames; 
record.Sample_Quantity__c = strProductQuantity; 
record.Sample_Price__c = strProductPrice; 
record.Sample_Description__c = strDescription; 
record.Sample_Service_Date__c =strServiceDate; 

sforce.connection.update([record]); 
window.location.reload(); 

strServiceDate = records[i].ServiceDate; 
} 

if(strProductNames.length>0){ 
System.debug('strProductNames is: ' + strProductNames); 
strProductNames = strProductNames.substring(0,strProductNames.length-2); 
} 
record.Sample_Name__c = strProductNames; 
record.Sample_Quantity__c = strProductQuantity; 

  • 1
    this is javascript not apex so use console.log. And don't use this substring(0,strProductNames.length-2) if you don't want to concat the name. – Tushar Sharma Sep 4 '16 at 7:26
0

It looks to me like the line is doing what it's supposed to do, and if I run the following in SFDC's execute anonymous window, it looks like it's working correctly:

String myString = 'This is a test of the length function'; Integer endPoint = myString.length(); System.debug(myString.substring(0, (endpoint-2)));

This returns "This is a test of the length functi"

Try capturing the endPoint in a separate call. I know it might be wasteful, but eliminate that failure point first. Then, just before

strProductNames = strProductNames.substring(0,strProductNames.length-2);

use the following

System.debug('strProductNames is: ' + strProductNames);

just to be SURE that you have the right data.

See where that takes you.

  • @Scott. Made the edit (see above) but it throws system not defined error – Stavros_McGillicuddy Sep 3 '16 at 16:55
  • Sorry - I was thinking you were trying to do this in apex, not in javascript. Not sure why. At any rate, it looks like the person who gave you the code was trying to ensure that the source data will always come back with two less characters than what it gets, rather than the two characters that you are getting. But - if you can guarantee that the accepting field will always be larger than the data it is receiving, why not get rid of that line (especially if it works without it!). – Scott Cromie Sep 4 '16 at 19:47
0

The number of characters of "PricebookEntry.Product2.Name" will never be more than 100 so I commented out that section.

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.