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
jjagtjjagt 

Custom Button to Update using another fields value

I am trying to update a custom field using a button for the the custom Object.  

 

BACKGROUND:

The custom object is Services_Selection__c

Inside this custom Object is a field called Base_Fee__c that has a value

I want to update the Product_Attributes_Value__c field with the Base_Fee__c field

 

When the loop is Running, I want to update all selected records to be the Base_Fee__c value, instead of the hardcoded "200" I placed for testing purposes.  The code works fine, I just cannot get the field to automatically update based on the field above. 

 

Any thoughts?  I am sure this is a very easy question, but I have been struggling on this for a while.  Code is below :

 

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

var records = {!GETRECORDIDS($ObjectType.Services_Selection__c)};

var newRecords = [];

 

if (records[0] == null)

{

alert("Please select at least one row")

}

else

{

for (var n=0; n<records.length; n++)

{

var c = new sforce.SObject("Services_Selection__c");

c.id = records[n];

c.Product_Attributes_Value__c = 200;

newRecords.push(c);

}

 

result = sforce.connection.update(newRecords); window.location.reload(); }

Ispita_NavatarIspita_Navatar

Please try the following after updating the highlighted text:-

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Services_Selection__c)};
var newRecords = [];
if (records[0] == null)
{
alert("Please select at least one row")
}
else
{

var strQry;

var result;

var c2 = new sforce.SObject("Services_Selection__c");
for (var n=0; n<records.length; n++)
{
var c = new sforce.SObject("Services_Selection__c");
c.id = records[n];

// use this if it is on the detail page of the object
c.Product_Attributes_Value__c = "{!Services_Selection__c.Base_Fee__c}";

newRecords.push(c1);

// else use this where you have to fire a query to get the field value

strQry="Select s.Id,s.Base_Fee__c from Services_Selection__c where id="+ c.id;

result=sforce.connection.query(strQry);

arrCustomRecords = result.getArray("records");

c2.Product_Attributes_Value__c=arrCustomRecords[0].Base_Fee_c;

newRecords.push(c2);
}
result = sforce.connection.update(newRecords); window.location.reload();

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

jjagtjjagt

Thanks for the quick response......

 

I tried your option 1 since the data resides in the detail of the Services selection Object, however I do not believe it like the quotes.  I also tried it without the quotes, and the {.  Some cases it works, and some cases it doesnt.

 

If I simply use c.Base_Fee__c instead of what you put,the code actually fully operates, however the values do not update accordingly.  When i try and display it using "alert" it says its undefined. 

 

Any thoughts?

Ispita_NavatarIspita_Navatar

It is saying undefined as it is recognising it as a variable . To avoid that try this as you alert :-

 

alert(' " '+yourvar+' " ');

I think it should work , give it another shot else do post your latest code I will try to find the problem.

 

jjagtjjagt

Really appreciate your assistance.....

 

Below is the code......the line highlighted in redis the one thats not working.  Like i said i can hardcode a numeric value like 200 in its place and it updates, but not a field within the Services_selections Object like i have highlighted

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Services_Selection__c)};
var newRecords = [];

if (records[0] == null)
{
alert("Please select at least one row")
}
else
{
for (var n=0; n<records.length; n++)
{
var c = new sforce.SObject("Services_Selection__c");
c.id = records[n];

c.Product_Attributes_Value__c = {!Services_Selection__c.Base_Fee__c};

newRecords.push(c);
}


result = sforce.connection.update(newRecords);
window.location.reload();
}