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
RyanYoung429RyanYoung429 

Field Value Setting in Java Script

Hello!

I found the script below to create a Clone button on Opportunity Product but I am curious if anyone knows how to set values to specific fields like this from a regular Custom Button: "00N36000006vcDW={!IF(MONTH(Opportunity.CloseDate) + 1 == 13, DATE(YEAR( Opportunity.Start_Date__c ) + 1, 01, 01), DATE(YEAR(Opportunity.Start_Date__c ), MONTH(Opportunity.Start_Date__c ) + 1, 01))}& "

Typically it is prety straight forward but just not clear on the syntax to use for setting a few fields.


This script does the job after getting errors with the URL hack but I'm not clear on editing the script to set new field values:

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
var metadata = sforce.connection.describeSObject('OpportunityLineItem'); 
var dontdo = ['TotalPrice']; //Add fields to blank here. Total price or Unit price is needed at minimum 
var fields = []; 
for (var i = 0; i < metadata.fields.length; i++) { 
if (metadata.fields[i].createable == "true" && dontdo.indexOf(metadata.fields[i].name) == -1){ 
fields.push(metadata.fields[i].name) 


fieldlist = fields.join([separator = ',']) 
var cloner = sforce.connection.query("SELECT " + fieldlist + " from OpportunityLineItem where id ='{!OpportunityLineItem.Id}' limit 1"); 

records = cloner.getArray("records"); 

delete records[0].Id; 
//records[0].Product_Stage__c = 'Closed Won'; // Manually override any fields down here 

result = sforce.connection.create(records); 
var editparams = ''; 
//var editparams = "&00N34000004DVHD=Closed%20Won&00X64000002DBDS=&00N24000006XBDp=&ServiceDate=&VT00S53000003CFSA=&DW00D52000002FXGs="; // Any url hacking style parameters you want added to the edit window when we open the cloned object. Can be used to blank a field that was required forcing the agent to override the default. 
window.location = '/' + result[0]['id'] + "/e?&retURL=" + result[0]['id'] + editparams;
Richard Jimenez 9Richard Jimenez 9
Hi Ryan,

This hack/ script is pretty nasty to read and maintain. I would recommend creating a visualforce page which you can redirect to from a custom button passing the opp prod id through. The vf page does the clone (using a controller class) and then redirects to the cloned opportunity product (so you dont really get to see the page). Also, you can have tests to ensure the clone is always working correctly with any future configuration change on the opportunity product.

Thanks,
Richard.

 
RyanYoung429RyanYoung429
That sounds interesting but in fact it is waaaaay outside my skill set.

It there really no way to set field values in a Java button?
Richard Jimenez 9Richard Jimenez 9
Hi Ryan,

You can set values in a javascript button, and setting a single value is a good use case for using javascript buttons. When the requirement is fixed and there is very little logic the button is straight forward to maintain.

Cloning functions can be a bit more involved and tend to need maintainence as you add new fields that need to be cloned, or require different initial values that need to be set. At this point, this is when having the logic in apex will become easier to maintain, test and reuse.

If you use a javascript button, it will still work if that is what you are more comfortable in using/supporting going forward.

Thanks,
Richard.
RyanYoung429RyanYoung429
@Richard, do you know where I could find documentation on how to create the button and the flow that would clone a record and set new values?