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
Stavros McGillicuddyStavros McGillicuddy 

Code only moves first two characters of a field. Please see code

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); } ​
 
{!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();

 
Best Answer chosen by Stavros McGillicuddy
Magesh Mani YadavMagesh Mani Yadav
Hi Stavros,
If you are sure about the number of characters of "PricebookEntry.Product2.Name " data is not more than 100 character then you dont need 
below lines of code
if(strProductNames.length>0){ 
strProductNames = strProductNames.substring(0,strProductNames.length-2); 
}

you can simply comment it out.

If you are not sure about the length of this field data then you need to either increase the length of field "record.Sample_Name__c" or you have to truncate the original size of to "PricebookEntry.Product2.Name " to 100 by doing this
 
if(strProductNames!=null && strProductNames.length>100){ 
strProductNames = strProductNames.substring(0,99); 
}

try this should work for you and let us know if you still have issue.

All Answers

Magesh Mani YadavMagesh Mani Yadav
Hi Stavros,
If you are sure about the number of characters of "PricebookEntry.Product2.Name " data is not more than 100 character then you dont need 
below lines of code
if(strProductNames.length>0){ 
strProductNames = strProductNames.substring(0,strProductNames.length-2); 
}

you can simply comment it out.

If you are not sure about the length of this field data then you need to either increase the length of field "record.Sample_Name__c" or you have to truncate the original size of to "PricebookEntry.Product2.Name " to 100 by doing this
 
if(strProductNames!=null && strProductNames.length>100){ 
strProductNames = strProductNames.substring(0,99); 
}

try this should work for you and let us know if you still have issue.
This was selected as the best answer
Stavros McGillicuddyStavros McGillicuddy
Thanks Magesh. The number of characters of "PricebookEntry.Product2.Name " will never be more than 100 so I commented out that section.