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
SFTerrSFTerr 

JavaScript and Visualforce

Hi, 

 

I have a custom object called "Project_Attendance__c"

 

On this record I have a button which opens up a custom visualforce page and the same time I want a tickbox to be ticked. 

 

To tick this I am using a JavaScript code

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var newRecords = []; 
var c = new sforce.SObject("Project_Attendance__c"); 
c.id ="{!Project_Attendance__c.ID}"; 
c.Attended__c = true;
newRecords.push(c); 
result = sforce.connection.update(newRecords); 
window.location.reload();

 

 

I am then referencing this in the visualforce page

 

<apex:includeScript value="{!$Resource.tickattendeed_js}"/>

 But it does not work, the visualforce page opens up fine but it does not tick my checkbox. 

 

Any ideas on why it is not working?

 

Thanks,

Michael

 

 

 

kiranmutturukiranmutturu
can you try like this

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

var c = new sforce.SObject("Project_Attendance__c");
c.id ="{!Project_Attendance__c.ID}";
c.Attended__c = true;

var result = sforce.connection.update([c]);
window.location.reload();
SFTerrSFTerr

Thanks Kiran, 

 

That code now works but it doesn't work when I try to use with the visualforce page. 

 

Would the javascript fire before loading the visualforce page? I am using the following outputs in the visualforce page which I know are read-only so I am just wondering if that is causing a problem. Or would the javascript file fire in the original page which is read/write?

<apex:outputfield value" 
sfdcfoxsfdcfox

Your code, if it worked, would produce an infinite loop; you're not checking to see if the value was already updated. I suspect that a simple code error is keeping it from working, which works in your favor in this case (since the page would just loop infinitely).

 

If your goal is to update the view state, add an onload handler for the page, and call a named actionFunction that updates the view state. If your goal is to update the database, consider having the actionFunction perform the update; using sforce.connection.update counts against API limits, while using an actionFunction does not.