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
Emilee Crocker 3Emilee Crocker 3 

How to change javascript to visualforce

I'm trying to create a visualforce page from the following javascript but I keep getting multiple error messages.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}

sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
sforce.connection.update([E]);

//refresh the page
window.location.reload();
Best Answer chosen by Emilee Crocker 3
Aman MalikAman Malik
This is strange as the same code working for me. Can you please make sure your code is like below:
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);


//redirect to lead detail page
window.location.href = '{!$Site.BaseUrl}/{!Lead.Id}';

</script>
</apex:page>
Thanks
 

All Answers

Aman MalikAman Malik
Hi,
Can you provide more detail on it.
What kind of errors you are getting.

Thanks,
Aman
Emilee Crocker 3Emilee Crocker 3
The first one I am getting is REQUIRESCRIPT cannot be used
Aman MalikAman Malik
Hi Emilee,
In vf page, we have to include js in different way. So try the below code snippet:
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
sforce.connection.update([E]);

//refresh the page
window.location.reload();
</script>
Kindly let me know if this helps.

Please like the answer and mark it as best if this helps.

Thanks,
Aman
 
Emilee Crocker 3Emilee Crocker 3
It is giving me the error <apex:page> is required and must be the outermost tag
Aman MalikAman Malik
Hi,

Yes, compiler is correct. sorry, forgot to add <apex:page > in snippet. Please try update one:
<apex:page >
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
sforce.connection.update([E]);

//refresh the page
window.location.reload();
</script>
</apex:page >
Thanks,
Aman
 
Emilee Crocker 3Emilee Crocker 3
I added it in myself and got the same code you have above. It now ways Unknown property 'Lead.Id' referenced
Aman MalikAman Malik
Hi,
Got the error.
Seems you are not passing lead record id in the page url. Kindly pass it like below:
{Your org base url}/apex/yourpagename?id=00Q5600000251OG

Also update the snippet again to add Lead standard controller like below:
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);

if (result[0].getBoolean("success")) {
  alert("lead with id " + result[0].id + " updated");
} else {
  log("failed to update lead " + result[0]);
}

//refresh the page
window.location.reload();
</script>
</apex:page >

Thanks,
Aman
Emilee Crocker 3Emilee Crocker 3
I made the changes as you have said, after trying this, the page keeps updating and never stops. Do i need to add stop update into the code?
 
Aman MalikAman Malik
Correct.
Emilee Crocker 3Emilee Crocker 3
How would I do so as it not a tag?  Would below be correct?

/window.location.reload();
Aman MalikAman Malik
Nope,  try below one:
//window.location.reload();
Emilee Crocker 3Emilee Crocker 3
It is still updating over and over with the message. I input the //window.location.reload();
 
Aman MalikAman Malik
It shouldn't. 
Can you show me your snippet? 
Aman MalikAman Malik
Just checking. 
Have you save your changes? 
Emilee Crocker 3Emilee Crocker 3
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);


//refresh the page
window.location.reload();


//window.location.reload();

</script>
</apex:page>
Emilee Crocker 3Emilee Crocker 3
Yes, I have saved after each change, logged out and logged back in to test this.

 
Aman MalikAman Malik
I can see in your code that, you forgot to comment window. location. reload(). So update your code like below:
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);


//refresh the page
//window.location.reload();

</script>
</apex:page>

Thanks
Emilee Crocker 3Emilee Crocker 3
The code is working but it is coming up as a blank page afterwards instead of going back to the lead
 
Aman MalikAman Malik
Hi, 
So you want to redirect to lead detail page once your lead get updated. For that update code like below:
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);


//redirect to lead detail page
window.open('/{!Lead. Id}');

</script>
</apex:page>

Kindly likes the answers if they helps and Mark answer as best when you have done with the task. 

Thanks 
Emilee Crocker 3Emilee Crocker 3
It still is not working. The refresh is opening a new page and leaving the old one blank and in SalesforceOne, it is bringing up a blank screen.
Aman MalikAman Malik
Hi,
Can you try to replace
window.open('/{!Lead. Id}');
with
window.location.href = '{!$Site.BaseUrl}/{!Lead. Id}';

This should work. 

Thanks
Emilee Crocker 3Emilee Crocker 3

It's coming up with the error: Unknown property 'LeadStandarController.Site'

 

Aman MalikAman Malik
This is strange as the same code working for me. Can you please make sure your code is like below:
<apex:page standardController="Lead">
<apex:includeScript value="/soap/ajax/33.0/connection.js"/>
<apex:includeScript value="/soap/ajax/33.0/34.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";

var E = new sforce.SObject("Lead");
E.id = '{!Lead.Id}';

// Replace below with your field.
E.To_Be_Set_Up__c=true;

// Replace below with your field.
E.Status = 'Converted';

// save the change
result = sforce.connection.update([E]);


//redirect to lead detail page
window.location.href = '{!$Site.BaseUrl}/{!Lead.Id}';

</script>
</apex:page>
Thanks
 
This was selected as the best answer
Emilee Crocker 3Emilee Crocker 3
User-added image
The above is exactly what I have typed. I just retyped it all in to see if I made a mistake but it is still coming up with a blank page after clicking the button.
Aman MalikAman Malik
The same code working for me. Not sure, what is wrong at your end.
Emilee Crocker 3Emilee Crocker 3
Not sure, Works on the desktop! Just a weird bug on Salesforce One now