One may have a need to send such a request to:
- Read an existing record
- Update a record
- Load a collection
object.xhr.done()
Rather than using raw JavaScript, one should leverage the parameters that can be defined as part of the method. In the case of .fetch(), it would look similar to the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myCustomJSMethod: function(module, guid){ | |
//Instantiate a SugarBean object representing record identified by GUID | |
var beanObject = app.data.createBean(module, {id:guid}); | |
//Send request to REST v10 API to retrieve the record | |
beanObject.fetch({ | |
//Define the anonymous function that will execute on a successful response | |
success: function(data){ //the data parameter represents the JSON response from the API | |
//Do something with the response upon successful request | |
console.log(data); //This will simply output response to JS console | |
} | |
}); | |
} |
As illustrated in the code, an anonymous function is defined for the success: parameter. This is the code that will be automatically executed upon a response being received and is capable of interacting with the JSON response -- found in the data variable.
Note that a similar approach can also be used to define an error anonymous.