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
BrandiTBrandiT 

Custom OnClick JavaScript Button to Update Date Field

I'm trying to create a custom OnClick JavaScript button that will update a date field on an Opportunity when clicked with today's date. 

 

Here is the code I'm using:

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
 
var newRecords = [];
 
var c = new sforce.SObject("Opportunity");
c.id ="{!Opportunity.Id}";
c.{!Opportunity.Media_Plan_Approved__c} = new Date();
newRecords.push(c);
 
result = sforce.connection.update(newRecords);
 
window.location.reload();

 

 

When I test the button though, I get a message in a pop up window that says:

"A problem with the OnClick JavaScript button or link was encountered:

Expected Identifier"

 

I really don't know Java that well.  Can anyone tell me what I'm doing wrong?

 

Thanks!!

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Hey,

Nearly there, just one lil tweak

 

c.{!Opportunity.Media_Plan_Approved__c} = new Date();

 

should read

 

c.Media_Plan_Approved__c = new Date();

All Answers

Ritesh AswaneyRitesh Aswaney

Hey,

Nearly there, just one lil tweak

 

c.{!Opportunity.Media_Plan_Approved__c} = new Date();

 

should read

 

c.Media_Plan_Approved__c = new Date();

This was selected as the best answer
BrandiTBrandiT

That was almost too easy   :)  Thanks so much!!  Worked beautifully!!!

Ritesh AswaneyRitesh Aswaney

Sweet. All's well that ends well :)

SiimSiim

Hello,

Am having the same issue. I can update a text field but not a field of type checkbox.

Has anyone been able to update a checkbox field?Is my syntax wrong?

 

{!requireScript("/soap/ajax/22.0/connection.js")}
{!requireScript("/soap/ajax/22.0/apex.js")}
var callArray = new Array();
var c= new sforce.SObject("Call__c");
c.Initialize__c  = true;//field type checkbox
c.CallObjectives__c = "test";
 
c.set("Id","{!Call__c.Id}");
callArray .push(c);
try
 {
// update the Group of Records
var responseUpdate = sforce.connection.update(callArray);
}
catch (error)
{
alert("Failed to Update with error: " + error);
}

 

 

Thanks

Siim