+ Start a Discussion
TuckoTucko 

sForce not defined on JS

Hello, 

I got this code that I can't get to work and I do not know what I am doing wrong
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/21.0/apex.js")}

var test = new sForce.SObject("Test__c");

if(test.Id == "{!Test__c.Id}"){
test.pole1__c = "{!Test__c.pole1__c}";
test.pole2__c = "{!Test__c.pole2__c}";
test.pole3__c = "{!Test__c.pole3__c}";
test.pole4__c = "{!Test__c.pole4__c}";
test.pole6__c = "{!Test__c.pole6__c}";

test.pole1__c = null;
test.pole2__c = null;
test.pole3__c = null;
test.pole4__c = null;
test.pole6__c = null;
}
var result = sforce.connection.update(test);

When I click on the button I get sForce not defined error.
Best Answer chosen by Tucko
TuckoTucko
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/34/apex.js")}

This piece of code fixed the error.

All Answers

Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Tucko,
Please try this 
{!REQUIRESCRIPT("/soap/ajax/30/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30/apex.js")}

intead of 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/21.0/apex.js")}
and let me know if it is worked.

Thanks :)

 
TuckoTucko
Still getting the same error:

sForce is not defined
Prosenjit Sarkar 7Prosenjit Sarkar 7
hey Tucko,
Previously I thought it was the version problem. But JavaScript is case sensitive language. In your code please change the 4th line. You have written sForce instead of sforce. Change the code and let me know if it is worked. 19th line is correct as per my knowledge. 
TuckoTucko
Still not solved.
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hey Tucko,

First of all, you were getting sForce not defined  because you had written sForce instead of sforce.  It was a syntactical error.

As per my understanding you want to update a record by clicking on an onClick JavaScript button at the detail page. If so then follow these codes. I think it will solve all your issues. But still I cannot understand the logic you have wrtiien as you are assigning something to the fields of Test__c and then again assigned them to by null. So I have written a proper structure for you,
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/21.0/apex.js")}

// Create an instance of test/
var test = new sforce.SObject("Test__c");

// Assign test records id from where you have clicked the onlick javascript button.
test.Id = "{!Test__c.Id}";

// Do your assignment here 
test.something__c = "{!test.something_else__c}"

// Update the record.
var result = sforce.connection.update([test]);

// If you wish you can reload the record detail page by this
window.location.reload();

While you are updating you had used var result = sforce.connection.update(test);
Whereas, this update method requires an array of instances. i.e. var result = sforce.connection.update([test]); 

Thanks :)
Prosenjit Sarkar 7Prosenjit Sarkar 7
Let me know if it is solved. 
TuckoTucko
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/34/apex.js")}

This piece of code fixed the error.
This was selected as the best answer