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
KarinEisKarinEis 

Need Lightning alternative to convert a Java Script Button that sends data to our internal system

I need to replace this code with a Lightning Action or some other alternative so I can add a button to the Opportunity Page in LEX.  I have been unsuccessful with finding a template to use as a guide and I'm not a Developer.  Any guidance would be greatly appreciated.

{!REQUIRESCRIPT('//code.jquery.com/jquery-1.6.2.min.js')}
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var myquery = "SELECT Id, Opportunity.Order_Num__c FROM Opportunity WHERE Id = '{!Opportunity.Id}'";
var result = sforce.connection.query(myquery);
var records = result.getArray("records");
var recordToUpdate;
if (records[0]) {
recordToUpdate = records[0];
}
if (recordToUpdate.Order_Num__c) {
alert("Order already exists");
}
else if (!recordToUpdate) {
alert("Record not found (shouldn't happen)");
}
else {
var dataToSend = {
summary: "{!Opportunity.Name}",
purchaseOrder: "{!Opportunity.Purchase_Order_or_Job__c}",
reporter: "{!Opportunity.OwnerFullName}"
};

jQuery.ajax(
{
dataType: "jsonp",
crossDomain: true,
data: dataToSend,
url: 'https://Something.CompanyABC.com:1234/order/createOrder'
})
.done(function (data) {
if (data.error) {
alert("Error: " + data.error);
}
else if (data) {
setSaleforceValues(data);
}
else {
alert("No response data returned!");
}
})
.fail(function (error) {
alert("Error occurred!");
alert(error);
})
.always(function () {
"use strict";
//alert( "complete" );
});
}

function setSaleforceValues(data) {
var newRecords = [];
if (recordToUpdate) {
var errorMessage = "";
var haveRecordChange = false;
if (data.orderUrl) {
recordToUpdate.Order_Num__c = data.orderUrl;
haveRecordChange = true;
}
else {
errorMessage += "No orderUrl returned!";
}

if (data.orderKey) {
recordToUpdate.Order_Key__c = data.orderKey;
haveRecordChange = true;
}
else {
errorMessage += "\nNo orderKey returned!";
}

if (data.ordersAppUrl) {
recordToUpdate.Model_Reports__c = data.ordersAppUrl;
haveRecordChange = true;
}
else {
errorMessage += "\nNo ordersAppUrl returned!";
}

if (errorMessage.length) {
alert(errorMessage);
}

if (haveRecordChange) {
newRecords.push(recordToUpdate);
}
}

if (newRecords.length) {
result = sforce.connection.update(newRecords);
window.location.reload();
}
else {
alert("No updates to Salesforce made");
}
}
Alain CabonAlain Cabon
The Lightning Action is here a Lightning component (Aura/LWC, javascript code) called like a quick action that will open and close immediatly a modal window and that will fire the results with "toasts" (also temporary modal windows with messages (success/error)).

This code does many things even if it short and the standard quick action by just clicking (administrator) is not sufficient.

The less standard part is the use of a cross-site scriptiing hack with JSONP with jQuery Ajax that you want probably to keep (hacking was sometimes the easiest way, it is not standard and not recommended nowadays but ... that still works, for fetching data and here you are updating external data).

The advent of JSONP — essentially a consensual cross-site scripting hack — has opened the door to powerful mashups of content. Many prominent sites provide JSONP services, allowing you access to their content via a predefined API. A particularly great source of JSONP-formatted data is the Yahoo! Query Language, which we'll use in the following example to fetch news about cats.
https://learn.jquery.com/ajax/working-with-jsonp/

jQuery.ajax(
{
dataType: "jsonp",
crossDomain: true,
data: dataToSend,
url: 'https://Something.CompanyABC.com:1234/order/createOrder'
})
.done(function (data) {
if (data.error) {
alert("Error: " + data.error);
}
else if (data) {
setSaleforceValues(data);
}
....

That will become an call in Apex (code) with a REST API call to your external CompanyABC.

The new code will remain short. 

The Lightning Component framework uses Content Security Policy (CSP) to impose restrictions on content. The main objective is to help prevent cross-site scripting (XSS) and other code injection attacks.
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_api_calls_platform.htm
KarinEisKarinEis
Thank you for your feedback.  I will look everything over!