For the last month or so, I have been trying to come up with a way to convert a Javascript button to a Lightning component/action without much success.
Currently the button we have uses the following code:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var result = sforce.apex.execute("orderOutput","generate", {genericId:"{!Opportunity.Id}"});
window.alert(result );
window.location.reload(true);
This button uses a piece of Apex code in the background that basically allows us to generate a text EDI order that another system picks up and manipulates.
The code in the background looks like this:
global class orderOutput {
webservice static String generate(Id genericId) {
Set<String> validSObjects = new Set<String>();
validSObjects.add('Opportunity');
String result;
String sObjectType = genericId.getSObjectType().getDescribe().getName();
if (sObjectType == 'Opportunity') {
result = generateFromOpportunity(genericId);
} else {
result = 'Error: Generate called for unsupported SObject Type ' + sObjectType + '\r\n\r\nValid SObject Types are:\r\n';
for (String soType: validSObjects)
result += soType + '\r\n';
}
return result;
}
private static String generateFromOpportunity(Id opId) {
system.debug('***************************** generateFromOpportunity(Id opId) *********************************');
system.debug('opId : ' + opId);
String result;
List<Opportunity> validateList = new List<Opportunity>([SELECT Id FROM Opportunity WHERE Id = :opId AND Customer_Number__c != null AND Delivery_Customer_State_Province_updated__c != null AND Delivery_Customer_City__c != null AND Delivery_Customer_Name__c != null AND Delivery_Customer_Post_Code__c != null AND Delivery_Customer_State__c != null]);
if(validateList.isEmpty()) {
result = 'Error: Required fields missing.\r\n\r\nCustomer Number, Customer Name, Delivery Customer Address 3, Delivery Customer City, Delivery Customer Post Code and Delivery Customer State/Province are required for Output Order.';
} else {
date todaysDate = date.today();
List<Order_Output__c> ooDelete = new List<Order_Output__c>([SELECT Id FROM Order_Output__c WHERE Opportunity__c = :opId AND Order_Date__c = :todaysDate]);
if (!ooDelete.isEmpty())
delete ooDelete;
List<Order_Output__c> ooList = new List<Order_Output__c>();
List<Opportunity> oList = new List<Opportunity>([SELECT Id, Order_Output__c, (SELECT Id, Order_Output__c, PricebookEntry.Product2.Name, Quantity, LastModifiedDate FROM OpportunityLineItems WHERE Quantity > 0) FROM Opportunity WHERE Id = :opId]);
system.debug('oList : ' + oList);
if(!oList.isEmpty()) {
//get snapshot qtys
List<AggregateResult> aList = new List<AggregateResult>([SELECT OLI__c, SUM(Qty__c) FROM Order_Output__c WHERE Opportunity__c = :opId GROUP BY OLI__c]);
Map<Id, Integer> oliMap = new Map<Id, Integer>();
for(AggregateResult a : aList) {
oliMap.put(String.valueOf(a.get('OLI__c')), Integer.valueOf(a.get('expr0')));
}
Opportunity o = oList[0];
system.debug('o.OpportunityLineItems : ' + o.OpportunityLineItems);
if(!o.OpportunityLineItems.isEmpty()) {
Order_Output__c ooh = new Order_Output__c();
ooh.Opportunity__c = o.Id;
ooh.Output__c = o.Order_Output__c;
ooh.Order__c = 1;
ooh.Order_Date__c = System.Today();
system.debug('Adding ooh to ooList(line 148)');
ooList.add(ooh);
Order_Output__c oo;
for(OpportunityLineItem oli : o.OpportunityLineItems) {
system.debug('oli : '+ oli);
system.debug('oli.LastModifiedDate: ' + oli.LastModifiedDate);
//system.debug('maxOutputOrderDate: ' + maxOutputOrderDate);
//if (oli.LastModifiedDate > maxOutputOrderDate || maxOutputOrderDate == null) {
Integer Qty = Integer.valueOf(oli.Quantity);
if(oliMap.get(oli.Id) != null){
system.debug('oliMap.get(oli.Id) == null');
Qty = Qty - oliMap.get(oli.Id);
}
if(Qty > 0) {
system.debug('Quantity is greater than zero');
oo = new Order_Output__c();
oo.Opportunity__c = o.Id;
oo.Output__c = oli.Order_Output__c.replaceAll('\\{QTY}', String.valueOf(Qty)+'.0000');
oo.Order_Date__c = System.Today();
oo.Product_Name__c = oli.PricebookEntry.Product2.Name;
oo.Qty__c = Qty;
oo.OLI__c = oli.Id;
system.debug('Adding oo to ooList (line 174)');
ooList.add(oo);
}
//}
}
}
}
insert ooList;
result = ooList.Size() + ' Order Lines Outputted';
}
return result;
}
}
How can I convert this across?
The button is only used within the Opportunity object to create an EDI for products that have been added.
As an example, I have worked through the following: https://lightning.accorto.com/support/discussions/topics/1000081937
And have been able to get it working to an extent but the code used creates a pop-up page that doesnt format correctly. Within the page the buttons do work so I got half way there.
I have also tried working through the following: https://andyinthecloud.com/2013/07/16/how-to-call-apex-code-from-a-custom-button/