+ Start a Discussion
kevindotcarkevindotcar 

Problem setting values on a VF page via Javascript

 

Hi all,

 

I'm trying to build a custom button on a page layout for a custom object  that calls javascript that updates other fields on the page.  I defined the behavior as "execute javascript" and the Content Behavior as "OnClick Javascript".  The source is simple;

 

<script language="JavaScript">
try { {!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} {!Appointment__c.Confirmed_Date__c}={!Appointment__c.ApptDate__c} ; {!Appointment__c.Confirmed_Time__c}={!Appointment__c.ApptTime__c} ; } catch(err){ alert(err); }
</script>

 

...But I keep on getting a popup that says "syntax error"  - help?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sf_evolutionsf_evolution

Hi Hemantgarg,

You are absolutely correct.

 

This is the code that worked:

 

{! REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
try{ 
var updateRecord = new Array(); 
var msgString = "You are about to set theconfirmation date/time with the reservation date/time\n"; 
msgString += "This WILL NOT CHANGE THE STATUS OF THE RESERVATION!!!. Continue?"; 

var myquery = "select id, Confirmed_Date__c, Confirmed_Time__c, RezDate__c, RezTime__c from Reservation__c where name='{!Reservation__c.Name}'"; 
result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

if(records[0]) { 
var update_Rez = records[0]; 

var r=confirm(msgString); 
if (r==true) { 
update_Rez.Confirmed_Date__c = '{!Reservation__c.RezDate__c}'; 
update_Rez.Confirmed_Time__c ='{!Reservation__c.RezTime__c}'; 
updateRecord.push(update_Rez); 
result = sforce.connection.update(updateRecord); 
//alert("Result Follows..." + result ); 
parent.location.href = parent.location.href; 
} 
} 
} 
catch (ex) { 
alert("Error Follows..." + ex); 
}

 ...I guess I was under-thinking the problem :-)

 

All Answers

SamuelDeRyckeSamuelDeRycke

Hey

i've not used that feature of SF a lot, so I can't comment on your js code (i think the merged fields will only enable you to read a value). But you don't need the <script> tags, just put your js code in that box and it should run.

 

 

 

JitendraJitendra

Hi,

 

if the button type is "Execute javascript", then there is no need to enclose the "<script>" tag. If it still not solve your problem then

 

Change declaration from 

<script language="JavaScript">

 

to

 

<script type="text/javascript">

 OR

<script>

 because your script decalarion is not valid.

hemantgarghemantgarg

I think it does not work in this way. You have to create an instance of the object and update the values in database using AJAX api. this way you can not assign/update values in a record.

sf_evolutionsf_evolution

Hi Hemantgarg,

You are absolutely correct.

 

This is the code that worked:

 

{! REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
try{ 
var updateRecord = new Array(); 
var msgString = "You are about to set theconfirmation date/time with the reservation date/time\n"; 
msgString += "This WILL NOT CHANGE THE STATUS OF THE RESERVATION!!!. Continue?"; 

var myquery = "select id, Confirmed_Date__c, Confirmed_Time__c, RezDate__c, RezTime__c from Reservation__c where name='{!Reservation__c.Name}'"; 
result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

if(records[0]) { 
var update_Rez = records[0]; 

var r=confirm(msgString); 
if (r==true) { 
update_Rez.Confirmed_Date__c = '{!Reservation__c.RezDate__c}'; 
update_Rez.Confirmed_Time__c ='{!Reservation__c.RezTime__c}'; 
updateRecord.push(update_Rez); 
result = sforce.connection.update(updateRecord); 
//alert("Result Follows..." + result ); 
parent.location.href = parent.location.href; 
} 
} 
} 
catch (ex) { 
alert("Error Follows..." + ex); 
}

 ...I guess I was under-thinking the problem :-)

 

This was selected as the best answer