Apex Global Methods and Services
Apex global methods are special methods that are declared with the "global" access modifier. These methods are part of Apex classes and are accessible from any Apex code in any organization, including triggers, classes, and flows, outside of Nue on Salesforce managed package.
Nue provides a list of global services for Salesforce admins to streamline and manage custom business processes. Salesforce admins can invoke these Apex global services from Apex triggers and flows.
Invoke Apex Global Services from Salesforce Flows
Here are the step-to-step instructions on how to invoke Apex Global Methods from Salesforce flows:
-
Create an Apex Class: First, you need to have an Apex class that performs the desired functionality you want to invoke from the flow. Ensure that the class is properly designed to handle the input and output variables required for integration with the flow. Here's an example of a simple Apex class that adds two numbers:
apexCopy codepublic class AddNumbers { @InvocableMethod(label='Add Numbers' description='Adds two numbers') public static List<Integer> addTwoNumbers(List<Integer> numbers) { Integer num1 = numbers[0]; Integer num2 = numbers[1]; Integer sum = num1 + num2; return new List<Integer>{sum}; } }
-
Create a Flow: In your Salesforce org, go to the Flow Builder and create a new flow (or use an existing one).
-
Add a New Action in the Flow: Inside the flow, add a new action element by dragging the "Apex Action" element from the palette onto the canvas.
-
Select the Apex Class: In the Apex Action properties, you'll need to select the Apex class you want to invoke. Click on the "Apex Class" dropdown and find your desired class from the list.
-
Map Input and Output Variables: If your Apex class takes input parameters, you'll need to map those inputs to the appropriate flow variables. Similarly, if your Apex class returns any output values, you'll need to map those outputs to flow variables as well.
-
Configure Fault Handling (Optional): You can also configure fault handling in case there are any errors when invoking the Apex class. This will allow you to define what should happen if an exception occurs during the Apex execution.
-
Save and Activate the Flow: Once you have configured the Apex action and mapped the input/output variables, save and activate the flow.
Now, when the flow runs, it will invoke the specified Apex class, passing the input variables, and receive the output variables back to continue with the flow logic.
Keep in mind that you need the appropriate permissions to create and invoke Apex classes and flows in your Salesforce org. Also, make sure the Apex class logic is thoroughly tested before using it in a flow.
Sample Use Case
SAMPLE USE CASE When a quote gets accepted by the customer, it will be finalized to an active order, then the subscriptions will be generated automatically. |
The following is a list of step-by-step instructions to implement this use case:
STEP 1 Create a class QuoteToOrderService, and add a method with @InvocableMethod
annotation as below. This method will call the global method Nue on Salesforce provided to finalize a list of quotes to active orders
public class QuoteToOrderService {
@InvocableMethod(label='Finalize quote to order' description='Finalize quote to order')
public static void FinalizedQuoteToOrder(List<Id> quoteIds){
Ruby.GlobalQuoteService qs = new Ruby.GlobalQuoteService();
//Parameters:
//quoteIds: a list of quote id need to be finalized
//activateOrders: a boolean value indicates whether the generated orders will be activated
qs.generateOrders(quoteIds, true);
}
}
STEP 2 Create a flow on Quote object named Finalize Accepted Quotes to Orders as below:
1. Set the start condition: Status equals Accepted
2. Add a new step to call the apex method QuoteToOrderService.FinalizedQuoteToOrder
created in above and set the input values to the triggering record ID
STEP 3 Save and activate the flow
STEP 4 Now you can create a quote record, and change its status to 'Accepted'
STEP 5 After the quote is accepted, you can see an order is created and the subscriptions are generated automatically
The subscription can be seen in the Lifecycle Manager:
Global Quote Services
The namespace of Nue on Salesforce managed package is Ruby. So please access all global Apex methods with prefix Ruby.
Recalculate a quote
Recalculate all prices for the given quote and its line items based on the quantity, subscription term, start date, end date and price tags already applied to each quote line item.
Signature
void Ruby.GlobalQuoteService.recalculate(String quoteId);
Sample Code
Id quoteId = '0Q04w000002RzITCA0';
new Ruby.GlobalQuoteService().recalculate(quoteId);
Re-apply all price tags for a Quote
Load currently applicable price tags for all line items of the quote according to the Start Date and End Date of each line item, and recalculate all prices for the given quote and its line items based on the quantity, subscription term, start date, end date and price tags.
Signature
void Ruby.GlobalQuoteService.reapplyPriceTags(String quoteId);
Sample Code
Id quoteId = '0Q04w000002RzITCA0';
new Ruby.GlobalQuoteService().recalculate(quoteId);
Copy quote from template
Copy quote line items from a template quote to a target quote. The target quote should not already have line items. The price book of the target quote needs to be the same as the price book of the template quote.
Signature
void Ruby.GlobalQuoteService.copyQuoteFromTemplate(String templateQuoteId, String targetQuoteId);
Sample Code
Id targetQuoteId = '0Q04w000002RzITCA0';
Id templateQuoteId = '0Q04w000002RzITCA1';
// Copy quote line items from the template quote to the target quote. Note that the target quote should not already have line items.
new Ruby.GlobalQuoteService().copyQuoteFromTemplate(templateQuoteId, targetQuoteId);
Generate orders
Generate orders from a list of quotes with an option to activate the generated orders.
Signature
List<Orders> orders Ruby.GlobalQuoteService.generateOrders(List<Id> quoteIds, Boolean activateOrders);
Sample Code
List<Id> quoteIds = new List<Id>();
quoteIds.add('0Q04w000002RzIT');
quoteIds.add('0Q04w000002RzmiCAC');
Boolean activateOrders = true;
// Generate orders for the list of quotes and activate the generated orders
List<Orders> orders = new Ruby.GlobalQuoteService().generateOrders(quoteIds, activateOrders);
Recalculate a list of quotes
Recalculate all prices for the given list of quotes and their line items based on the quantity, subscription term, start date, end date and price tags of each line item.
Signature
List<Orders> orders Ruby.GlobalQuoteService.recalculate(List<Id> quoteIds);
Sample Code
List<Id> quoteIds = new List<Id>();
quoteIds.add('0Q04w000002RzIT');
quoteIds.add('0Q04w000002RzmiCAC');
List<Orders> orders = new Ruby.GlobalQuoteService().recalculate(quoteIds);
Re-apply price tags for a list of quotes
Load currently applicable price tags for all line items of a list of quotes according to the Start Date and End Date of each line item, and recalculate all prices for the given quotes and their line items based on the quantity, subscription term, start date, end date and price tags of each line item.
Signature
List<Orders> orders Ruby.GlobalQuoteService.reapplyPriceTags(List<Id> quoteIds);
Sample Code
List<Id> quoteIds = new List<Id>();
quoteIds.add('0Q04w000002RzIT');
quoteIds.add('0Q04w000002RzmiCAC');
List<Orders> orders = new Ruby.GlobalQuoteService().reapplyPriceTags(quoteIds);
Global Order Services
The namespace of Nue on Salesforce managed package is Ruby. So please access all global Apex methods with prefix Ruby.
Recalculate an order
Recalculate all prices for the given order and its line items based on the quantity, subscription term, start date, end date and price tags of each line item. The order must be in Draft status.
Signature
void Ruby.GlobalOrderService.recalculate(String orderId);
Sample Code
Id orderId = '8014w000008LN3kAAG';
new Ruby.GlobalOrderService().recalculate(orderId);
Re-apply all price tags for an order
Load currently applicable price tags for all line items of a given order according to the Start Date and End Date of each line item, and recalculate all prices for the given order and their line items based on the quantity, subscription term, start date, end date and price tags of each line item. The orders must be in Draft status.
Signature
void Ruby.GlobalOrderService.reapplyPriceTags(String orderId);
Sample Code
Id orderId = '8014w000008LIDXAA4';
new Ruby.GlobalOrderService().reapplyPriceTags(orderId);
Recalculate a list of order
Recalculate all prices for a list of orders and their line items based on the quantity, subscription term, start date, end date, and price tags of each line item. The orders must all be in Draft status.
Signature
void Ruby.GlobalOrderService.recalculate(List<Id> orderIds);
Sample Code
List<Id> orderIds = new List<Id>();
orderIds.add('8014w000008LIDXAA4');
// Generate orders for the list of quotes and activate the generated orders
new Ruby.GlobalOrderService().recalculate(orderIds);
Re-apply all price tags for an order
Load currently applicable price tags for all line items of a list of orders according to the Start Date and End Date of each line item, and recalculate all prices for the given orders and their line items based on the quantity, subscription term, start date, end date and price tags of each line item. The orders must be in Draft status.
Signature
void Ruby.GlobalOrderService.reapplyPriceTags(List<Id> orderIds);
Sample Code
List<Id> orderIds = new List<Id>();
orderIds.add('8014w000008LIDXAA4');
// Generate orders for the list of quotes and activate the generated orders
new Ruby.GlobalOrderService().reapplyPricetags(orderIds);
Cancel a list of active orders
Cancel the given list of active orders. Please refer to this article for more details about order cancellation.
Signature
void Ruby.GlobalOrderService.cancelActiveOrders(List<Id> orderIds);
Sample Code
List<Id> orderIds = new List<Id>();
orderIds.add('8014w000008LIDXAA4');
// Generate orders for the list of quotes and activate the generated orders
new Ruby.GlobalOrderService().cancelActiveOrders(orderIds);
Preview Invoices for Quotes and Orders
Preview the invoices for a particular quote or order. Please refer to this article for more details about Invoice Preview.
Signature
global class InvoicePreviewService {
global static final Integer OPTION_FIRST_INVOICE = 0;
global static final Integer OPTION_ALL_INVOICES = 1;
global static final Integer OPTION_INVOICES_WITH_TARGET_DATE = 2;
global static final String INVOICE_STATUS_PREVIEW = 'Preview';
/**
* Preview invoices for a quote or order. The previewed invoices will be returned in a JSON representation.
*
* @param objectId The quote, order, or account ID.
* @param objectType Quote, Order, or Account
* @param previewOption The preview options. Possible values are: OPTION_FIRST_INVOICE, OPTION_ALL_INVOICES, OPTION_INVOICES_WITH_TARGET_DATE
* @param targetDate If OPTION_INVOICES_WITH_TARGET_DATE is chosen, then this is the invoice target date.
*
* @return Invoices
*/
global InvoicesPreviewApiResponse previewInvoices(Id objectId, String objectType, Integer previewOption, Date targetDate) {};
}
Sample Code
The following sample code previews invoices for a particular quote with an invoice target date.
Ruby.InvoicePreviewService previewService = new Ruby.InvoicePreviewService();
// Specify the object ID and objectType. The object type can be Quote or Order.
String objectType = 'Quote';
Id objectId = '0Q08G0000006AObSAM'; // The Quote ID
Date targetDate = Date.parse('1/1/2025');
Ruby.InvoicePreviewService.InvoicesPreviewApiResponse response =
previewService.previewInvoices(
objectId,
objectType,
Ruby.InvoicePreviewService.OPTION_INVOICES_WITH_TARGET_DATE,
targetDate);
// Optionally, save the previewed invoices
List<Ruby__Invoice__c> invoices = previewService.saveInvoicePreview(response);
Create and activate change orders
Nue provides a list of global services designed to create and activate change orders of various types. These methods can be utilized by admin users to automate processes within Salesforce. The supported change types include:
- Renew: This includes renewing to evergreen.
- UpdateTerm: This includes updating the term to evergreen.
- CoTerm: Co-terming one or more subscriptions.
- UpdateQuantity: Adjusting the quantity of assets.
- Cancel: Cancelling assets.
- EvergreenToCoTerm: Switching from evergreen to co-term.
- EvergreenToUpdateTerm: Switching from evergreen to update term.
- BulkSubscriptionRenewal: Handling bulk renewals of subscriptions.
The following is a code example:
//This class supports applying multiple changes with different types on assets
Ruby.GlobalChangeOrderService
//The following are the global change order requests
Ruby.GlobalRenewRequest
Ruby.GlobalAdjustPriceRequest
Ruby.GlobalAssetCancelRequest
Ruby.GlobalAssetChangeRequest
Ruby.GlobalCOTermRequest
Ruby.GlobalUpdateQuantityRequest
Ruby.GlobalUpdateTermRequest
Ruby.GlobalSubscriptionRenewalRequest
Ruby.GlobalCOTermForEvergreenRequest
Ruby.GlobalUpdateTermForEvergreenRequest
Ruby.GlobalBulkSubscriptionRenewalRequest
Sample Code
The following is some sample code:
Ruby.GlobalChangeOrderOptions options = new Ruby.GlobalChangeOrderOptions(Ruby.GlobalChangeOrderProceedOption.CreateOrder);
options.activateOrder = true;
//Start to assemble a list of change requests
List<Ruby.GlobalAssetChangeRequest> changeRequests = new List<Ruby.GlobalAssetChangeRequest>();
//Renew a subscription by 5 months
Ruby.GlobalRenewRequest renewRequest = new Ruby.GlobalRenewRequest();
renewRequest.assetNumber = 'SUB-000014';
renewRequest.renewalTerm = 5;
changeRequests.add(renewRequest);
//Update the quantity by adding 5 units, effective today
Ruby.GlobalUpdateQuantityRequest updateQuantityRequest = new Ruby.GlobalUpdateQuantityRequest();
updateQuantityRequest.assetNumber = 'SUB-000014';
updateQuantityRequest.startDate = Date.today();
updateQuantityRequest.quantity = 5;
changeRequests.add(updateQuantityRequest);
//Adjust price to $4 per unit, for the following 5 months, effective today
Ruby.GlobalAdjustPriceRequest adjustPrice = new Ruby.GlobalAdjustPriceRequest();
adjustPrice.assetNumber = 'SUB-000014';
adjustPrice.term = 5;
adjustPrice.netSalesPrice = 4;
changeRequests.add(adjustPrice);
//Execute the change order that includes all the above changes.
Ruby.GlobalChangeOrderService service = new Ruby.GlobalChangeOrderService();
Ruby.GlobalChangeOrderResult result = service.changeOrder(new Ruby.GlobalChangeOrder(options, changeRequests));
The following is additional sample code by use cases:
USE CASE | SAMPLE CODE |
---|---|
Renew |
|
Update Term (EvergreenToUpdateTerm) |
|
Update Term |
|
Update Term to Evergreen |
|
Cancel |
|
Co-term |
|
Bulk Renew |
|