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
Ankit Singh 6Ankit Singh 6 

Create a custom button and click to external api

Hi

I want to create a custom button under lead/contact layout. On click of this button I want to open 3rd party webapp page in a new tab.
Please let me know how to achieve the above requirement.
Best Answer chosen by Ankit Singh 6
KaranrajKaranraj
If its a non authenticated site. Then you can create a custom button and select the 'Content Source' as URL then in the script window past the URL of the site. If it authenticated site, then you have to make successfull authentication and then redirect to the url which you want and also you have to select the 'content source' as 'java script' while creating button. Check this below example for the one drive authentication using custom java script button

 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var url = "https://login.live.com/oauth20_authorize.srf?client_id=XXXXXXXXXXXX&scope=onedrive.readwrite&response_type=token&redirect_uri= https://login.salesforce.com/5009000000dssdJ"; 
popup(url); 

function popup(url) { 
var width = 525, 
height = 525, 
screenX = window.screenX, 
screenY = window.screenY, 
outerWidth = window.outerWidth, 
outerHeight = window.outerHeight; 

var left = screenX + Math.max(outerWidth - width, 0) / 2; 
var top = screenY + Math.max(outerHeight - height, 0) / 2; 

var features = [ 
"width=" + width, 
"height=" + height, 
"top=" + top, 
"left=" + left, 
"status=no", 
"resizable=yes", 
"toolbar=no", 
"menubar=no", 
"scrollbars=yes"]; 
var popup = window.open(url, "oauth", features.join(",")); 
if (!popup) { 
alert("failed to pop up auth window"); 
} 

popup.focus(); 

} 

function onAuthCallback() { 
var authInfo = getAuthInfoFromUrl(); 
var token = authInfo["access_token"]; 
var expiry = parseInt(authInfo["expires_in"]); 
setCookie(token, expiry); 
window.opener.onAuthenticated(token, window); 
} 

function getAuthInfoFromUrl() { 
if (window.location.hash) { 
var authResponse = window.location.hash.substring(1); 
var authInfo = JSON.parse( 
'{"' + authResponse.replace(/&/g, '","').replace(/=/g, '":"') + '"}', 
function(key, value) { return key === "" ? value : decodeURIComponent(value); }); 
alert(authInfo); 
return authInfo; 
} 
else { 
alert("failed to receive auth token"); 
} 
}

The above example use the access token login mechanism to authenticate to 'onedrive' application from custom java script button in salesforce

 

All Answers

KaranrajKaranraj
If its a non authenticated site. Then you can create a custom button and select the 'Content Source' as URL then in the script window past the URL of the site. If it authenticated site, then you have to make successfull authentication and then redirect to the url which you want and also you have to select the 'content source' as 'java script' while creating button. Check this below example for the one drive authentication using custom java script button

 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var url = "https://login.live.com/oauth20_authorize.srf?client_id=XXXXXXXXXXXX&scope=onedrive.readwrite&response_type=token&redirect_uri= https://login.salesforce.com/5009000000dssdJ"; 
popup(url); 

function popup(url) { 
var width = 525, 
height = 525, 
screenX = window.screenX, 
screenY = window.screenY, 
outerWidth = window.outerWidth, 
outerHeight = window.outerHeight; 

var left = screenX + Math.max(outerWidth - width, 0) / 2; 
var top = screenY + Math.max(outerHeight - height, 0) / 2; 

var features = [ 
"width=" + width, 
"height=" + height, 
"top=" + top, 
"left=" + left, 
"status=no", 
"resizable=yes", 
"toolbar=no", 
"menubar=no", 
"scrollbars=yes"]; 
var popup = window.open(url, "oauth", features.join(",")); 
if (!popup) { 
alert("failed to pop up auth window"); 
} 

popup.focus(); 

} 

function onAuthCallback() { 
var authInfo = getAuthInfoFromUrl(); 
var token = authInfo["access_token"]; 
var expiry = parseInt(authInfo["expires_in"]); 
setCookie(token, expiry); 
window.opener.onAuthenticated(token, window); 
} 

function getAuthInfoFromUrl() { 
if (window.location.hash) { 
var authResponse = window.location.hash.substring(1); 
var authInfo = JSON.parse( 
'{"' + authResponse.replace(/&/g, '","').replace(/=/g, '":"') + '"}', 
function(key, value) { return key === "" ? value : decodeURIComponent(value); }); 
alert(authInfo); 
return authInfo; 
} 
else { 
alert("failed to receive auth token"); 
} 
}

The above example use the access token login mechanism to authenticate to 'onedrive' application from custom java script button in salesforce

 
This was selected as the best answer
Ankit Singh 6Ankit Singh 6
Hi Karanraj

Thanks for reply.
I have one more doubt. The url is unauthenticated and I need to send some parameter while hitting url(eg leadId, firstname, lastname and phone).
How could we send these parameters while hitting url ? Think it as a post request to a url.