You need to sign in to do that
Don't have an account?

Javascript Button to Go to outside URL
I have a Javascript button in Lightning that needs to go out to SpringCM and pulls a form up that our users complete, it uses fields from the Opportunity that it pass through the URL. I am desperately trying to figure out how to do this and have tried the Trailhead Module: Lightning Alternatives to JavaScript Buttons and either it does not explain for my example or I am unable to understand. Can someone point me to any other solutions?
The Button is below:
Your help is greatly appreciated.
The Button is below:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} //Should not have to change this at all function getEOSInfo(objectid,objecttype,callback) { var baseUrl = "{!$Api.Partner_Server_URL_260}".substring(0, "{!$Api.Partner_Server_URL_260}".indexOf("/services")); var url = baseUrl +"/services/apexrest/SpringCMEos/eosinfo?objecttype="+objecttype+"&objectid="+objectid; var authkey = "Bearer {!$Api.Session_ID}"; sforce.connection.remoteFunction({ url : url, requestHeaders: {"Authorization":authkey,"Accept":"application/json"}, onSuccess : function(response) { var eosInfoResponse = JSON.parse(response); callback(eosInfoResponse); } }); } //Here you write your own code, this is a simple call var objectid = "{!Opportunity.Id}"; var objecttype = "Opportunity"; getEOSInfo(objectid,objecttype,function(eosInfo){ var formuid = "8be07531-XXXX-e611-XXXX-6c3be5a7XXXX"; var url = "https://XXX21.springcm.com/atlas/Forms/UpdateFormDoc.aspx?aid=9723&FormUid=" + formuid + "&SFFTE={!Opportunity.FTEs__c}&SFIND={!Account.Industry}&SFPSD={!Opportunity.ProgramStartDate__c}&SFID=" + eosInfo.SFId + "&SFNAME={!Opportunity.Name}&SFWF=ABC&SFCAFTYPE=CAAF&SFTYPE=Salesforce.Opportunity&SFPATH=" + eosInfo.FolderPath + "&ru=%2Fatlas%2Fbpm%2FWorkList.aspx"; window.open(url); });
Your help is greatly appreciated.
- You would create a lightning component that would show the button on the record page.
- On click of the button you would call the Apex class
- You would send the opportunity recordId from the lightning component by using the force:hasRecordId (Refer this (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_interfaces_force_hasrecordid.htm) link to know more about it)
- You can get all the variables that you require in the component's Apex controller(like session Id), you can also make rest calls from the Apex controller
- Once you have all the variables to create you URL you will return the url back to your component and use force:navigateToURL (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_navigateToURL.htm) to navigate to the created URL.
Please note that window.open does not always work in Salesforce Lightning.Reply in case you face any challenge.
Hope this helps,
Sandeep
All Answers
- You would create a lightning component that would show the button on the record page.
- On click of the button you would call the Apex class
- You would send the opportunity recordId from the lightning component by using the force:hasRecordId (Refer this (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_interfaces_force_hasrecordid.htm) link to know more about it)
- You can get all the variables that you require in the component's Apex controller(like session Id), you can also make rest calls from the Apex controller
- Once you have all the variables to create you URL you will return the url back to your component and use force:navigateToURL (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_force_navigateToURL.htm) to navigate to the created URL.
Please note that window.open does not always work in Salesforce Lightning.Reply in case you face any challenge.
Hope this helps,
Sandeep
Thank you, I will start to work on it now. I do appreciate your help.
Robert
Lightning Component Code and the Apex Controller - 2017 / 08 / 03
LightningCAF.cmp
<aura:component implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global"
controller="SpringCMApexController">
<aura:attribute name="recordId" type="Id" />
<div id="aura-page">
<div class="container" style="background-color:#f2f2f7">
<ui:button class="btn" label="CAF Form" press="{!c.navigate}"/> SpringCM Contract Amendment Form Button
</div>
</div>
</aura:component>
Apex Controller - SpringCMApexController.apxc
public class SpringCMApexController {
@AuraEnabled
public static Opportunity getOpportunity(ID opportunityId){
if(opportunityId == null) {
return null;
}
List<Opportunity> opps = [SELECT id,
Opportunity.FTEs__c,
Opportunity.Account.Industry ,
Opportunity.TotalNumberofSeats__c ,
Opportunity.CAPEX_Required__c ,
Opportunity.Amount ,
Opportunity.Annualized_Revenue__c,
Opportunity.ProgramStartDate__c ,
Opportunity.Account.Name ,
Opportunity.Name
FROM Opportunity
WHERE Id =: opportunityId];
if(opps.size()>0){
return opps[0];
}
return null;
}
}
LightningCAFController.js
({
navigate : function(component, event, helper) {
helper.navigate(component);
}
})
LightningCAFHelper.js
({
navigate : function(component) {
var action = component.get("c.getOpportunity");
var idOpp = component.get("v.recordId");
if(!$A.util.isEmpty(idOpp))
{
action.setParams({opportunityId: idOpp});
// Create a callback that is executed after
// the server-side action returns
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var opp = response.getReturnValue();
/* Here you can access like opp.Id, opp.FTEs__c, opp.Account.Industry and form the URL */
var fullTimeEmployees = opp.FTEs__c;
var industry = opp.Account.Industry;
var programStartDate = $A.localizationService.formatDate(Date(opp.ProgramStartDate__c),"MM/DD/YYYY");
var opportunityId = opp.Id;
var opportunityName = opp.Name;
var filePath = '/Accounts/'+ opp.Account.Name + '/Opportunities/&ru=%2Fatlas%2Fbpm%2FWorkList.aspx'
var partOneURL = 'https://uatna11.springcm.com/atlas/Forms/UpdateFormDoc.aspx?';
var partTwoURL = 'aid=7284';
var formUid = '8be07531-d9b0-e611-bb8d-6c3be5a75f4d';
var finalURL = partOneURL + partTwoURL
var finalURL = finalURL + '&FormUid=' + formUid;
var finalURL = finalURL + '&SFFTE=' + fullTimeEmployees;
var finalURL = finalURL + '&SFIND=' + industry;
var finalURL = finalURL + '&SFPSD=' + programStartDate;
var finalURL = finalURL + '&SFID=' + opp.Id;
var finalURL = finalURL + '&SFNAME=' + opportunityName;
var finalURL = finalURL + '&SFWF=ABC';
var finalURL = finalURL + '&SFCAFTYPE=CAAF';
var finalURL = finalURL + '&SFTYPE=Salesforce.Opportunity';
var finalURL = finalURL + '&SFPATH=' + filePath;
var urlEvent=$A.get("e.force:navigateToURL");
urlEvent.setParams({"url": finalURL});
urlEvent.fire();
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +
errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
// optionally set storable, abortable, background flag here
// A client-side action could cause multiple events,
// which could trigger other events and
// other server-side action calls.
// $A.enqueueAction adds the server-side action to the queue.
$A.enqueueAction(action);
}
else
{
console.log('===Record Id is not present====');
}
}
})
#JS_Alternative #SpringCM