+ Start a Discussion
gemgemgemgem 

Javascript API

Okay, so I have been trying fruitlessly, for awhile now, to wrap my head around the Javascript API that is provided for the Salesforce Sales app. I've opened a number of tickets, scoured StackOverflow, watched a number of videos, read some articles I've found on the main saiesforce website, all to answer a few simple questions. Perhaps I haven't taken the time to fully educate myself on the platform, fine, but given the extreme simplicity of what I'm trying to do, the labyrinthian, outdated developer pages have been a huge pain.

 

With that aside, perhaps someone here can answer the following questions for me.

 

A) What is the current version of the AJAX library? 

 

Admittedly, this is the least of my problems, and hasn't been much of an issue thus far. With that said however, I'm amazed at how difficult finding an answer to this has been. All over the Salesforce documentation, I've found references to different versions in the 

{!REQUIRESCRIPT} statement. A quick Google search brought me to this page: http://www.salesforce.com/us/developer/docs/ajax/ but, when I try to include it, I get a 404 error in the console. 

 

 

B) What on Earth is "{!...}" supposed to mean?

 

I've noticed that any Javascript I write will first be fed through a preprocessor in order to provide more convenient interfaces with the API. Fine. But am I doing with them exactly? What is the difference between the same expression the form of a string "{!Foo.id}" and not {!REQUIRESCRIPT..}? Nowhere in the documentation have I seen this addressed.

 

 

C) How can I modify fields through javascript?

 

This is along the same lines as the previous question. I take it that these "{!...}" expressions will allow me to interface with the api. Specifically, what I would like to do is modify a field value on button click. This has been difficult simply because I don't understand the method in which I can access fields. What is in the global scope? What objects can I access? How do I access them? 

 

I've tried the following:

 

    // Maybe I object is in the global scope

    console.log(Case)

 

    // Oops! Hmm, maybe I can access it with those strange {!...} expressions

    console.log({!Case})

    console.log("{!Case}")

 

    // No dice... but oddly enough "{!Case.id}" works... what the hell?

    // I have to create an "SObject" ...?

    var foo = new sforce.SObject("Case"); // not sure what the parameters are, but all examples I've seen do something similar...

    

    // Okay, now maybe I can access the desired fields from my Object

    > foo.get("MyField__c")

    null

    

     // Drat! Maybe it's expecting the label...

     >foo.get("MyField")

     null

 

Sorry if I've come off a bit rude or irritated, but after speaking with multiple people over the phone, had multiple tickets closed without my input, and hours of searching, I've lost patience.

 

Thanks

 

    

gemgemgemgem

Is no one able to answer this? 

 

Was my question too vague?

 

Does no one deal with this stuff?

Thomas DvornikThomas Dvornik

Hey GemGem, 

 

I'm not an expert when it comes to the Javascript API or VF, but maybe I can help a little bit. 

 

A) The current version of the of the AJAX library is the current version of the API. This means that when your instance is upgraded, you will still be using the same version of the AJAX library so you're code will not break if any changes were made to the js library. If you want to use the new version when your instance is upgrade, just change the version in the URL.

 

For Summer'13, the version is 28. So it would look like the following:

 

{!requireScript("/soap/ajax/28.0/connection.js")}

 You also don't need to use requireScript if you don't want to. Just do an include script but with the "/soap/ajax/28.0/connection.js" or  "<your instance>.salesforce.com/soap/ajax/28.0/connection.js".

 

B) Visualforce is put through a preprocessor. Using {!...} is the expression that is ran in the preprocessor, weither in JS, in visualforce tags, or just on a page. For example, if you want to create a VF page with your name, you can create the following. 

 

<apex:page> {!$User.name} </apex:page>

 When you use those expressions, you are just telling salesforce to process something on the server before the page is rendered. So in your example, the {!Foo.id} will just render the id to the page, and {!requirescript..} will just renader an import tag to the page.

 

C) The {!..} doesn't neccessarly allow you to interface through the api, but get values from the server on your page or in your script. The easiest thing to try is to store it in a JS variable. For example:

 

var name = '{!$User.name}';
console.log(name);

 Now you can use your name in your scripts. 

 

The objects in global scrop can be found here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global.htm

 

The expressions can't be the object itself, but a field. That is why Case.id works but not Case. I think that is only if you are on a case detail page though, but I could be wrong.

 

 

The reason why your fields are null, is because you just create an empty object.

var foo = new sforce.SObject("Case"); 
foo.get("MyField__c"); //Null
foo.set("MyField__c", "Hey");
foo.get("MyField__c"); //Hey

//Or you can access it like any other JS fields
foo.MyField__c = ''MyContent';
 
sforce.connection.create([foo]);
//Now your case with MyField__c equal to MyContent will be in salesforce, unless the request fails. 

 

 

Hope that helps.