Salesforce Integration using REST API

Working Sample Code for Salesforce Integration using REST API from Salesforce.

First register your app and select appropriate access to API with all relevant details.
Then register your callback url in CORS.

For more detail please refer to below Salesfore links:-

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_query.htm
https://developer.salesforce.com/forums/?id=906F000000099zpIAA
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm

HTML and JQuery Script below:-

<h3>REST API With Javascript</h3>
<button id=”connect”>Connect to the REST API</button>
<h4>Create Account Record</h4>
<label for=”name”>Account Name</label>
<input type=”text” name=”Name” id=”name”>
<br><br>
<button id=”button”>Submit</button>
<button id=”getButton”>Get Account</button>

$(document).ready(function () {
$(‘#connect’).click(function () {

//The clientId and consumerSecret are the ones from the Connected App
var clientId = “****************”;
var consumerSecret = “*************”;
//Authorization endpoint
var authEndPoint = “https://login.salesforce.com/services/oauth2/authorize&#8221;;
//response_type must be set to token
var responseType = “token”;
//This is the callback URL from the connected app
var redirectURI = “https://&#8221;;
//Construct the URL with the required parameters
var requestURL = authEndPoint + ‘?client_id=’ + clientId + ‘&response_type=’ + responseType + ‘&redirect_uri=’ + redirectURI;
//redirect the user to the endpoint
window.location = requestURL;
});

//Create Account Record button click
$(‘#button’).click(function () {

var accountName = $(‘#name’).val();
var accessToken = decodeURI(GetURLParameter(‘access_token’));
console.log(accessToken);
if (accountName != ”) {
//replace the below url with your domain name or yourInstance.salesforce.com
var requestUrl = ‘https://eu11.salesforce.com/services/data/v20.0/sobjects/Account/&#8217;;
//create a javascript object with the account field values
var accountInfo = {
“Name”: accountName
}
//convert the object to a JSON string
var accountInfoJson = JSON.stringify(accountInfo);
//send POST with ajax
$.ajax({
type: ‘POST’,
url: requestUrl,
//add the authorization header including the access_token
headers: {
‘Authorization’: ‘Bearer ‘ + accessToken,
‘Content-Type’: ‘application/json’
},
data: accountInfoJson,
success: function (response) {
displayAccountInfo(response);
},
error: function (response) {
displayError(response);
}
});

} else {
alert(‘Please specify name’);
}

});

$(‘#getButton’).click(function () {

var accessToken = decodeURI(GetURLParameter(‘access_token’));
console.log(accessToken);
if (1 != 2) {
//replace the below url with your domain name or yourInstance.salesforce.com
var requestUrl = ‘https://.salesforce.com/services/data/v20.0/query/?q=SELECT name from Account’;

$.ajax({
type: ‘GET’,
url: requestUrl,
//add the authorization header including the access_token
headers: {
‘Authorization’: ‘Bearer ‘ + accessToken,
‘Content-Type’: ‘application/json’
},
// data: accountInfoJson,
success: function (response) {
displayAccountData(response);
},
error: function (response) {
displayError(response);
}
});

} else {
alert(‘Please specify name’);
}

});

});
function displayAccountData(responseObject) {
var statusMessage = ‘

Account Detail

‘;
$(‘#status’).append(JSON.stringify(responseObject));
$(‘‘, {//replace this with your salesforce instance url
text: ‘
https://eu5.salesforce.com/&#8217; + responseObject.id,
href: ‘https://eu5.salesforce.com/&#8217; + responseObject.id,
target: ‘_blank’
}).appendTo($(‘#status’));
}
function displayAccountInfo(responseObject) {
var statusMessage = ‘

Success! Your account has been created, click on the link below.

‘;
$(‘#status’).append(statusMessage);
$(‘‘, {//replace this with your salesforce instance url
text: ‘
https://eu5.salesforce.com/&#8217; + responseObject.id,
href: ‘https://eu5.salesforce.com/&#8217; + responseObject.id,
target: ‘_blank’
}).appendTo($(‘#status’));
}
//Use this function to get the access token from the URL
function GetURLParameter(sParam) {
var mainURL = document.location + ”;
var pageUrls = mainURL.split(‘#’);
var sURLVariables = pageUrls[1].split(‘&’);
for (var i = 0; i A problem has occurred, please contact your administrator

‘;
$(‘#status’).append(statusMessage);
$(‘

‘, {
text: responseJson[0].errorCode + “: ” + responseJson[0].message
}).appendTo($(‘#status’));

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s