Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Calling the Revenue Recognition Service with APEX Triggers

You can call the Revenue Recognition service with a custom APEX trigger.

For a list of input and output parameters, see Revenue Recognition Service Developer Guide.

Example

In this example, we want to create a revenue schedule for an invoice line after its status changes to Posted. The org uses legal entities, so we included the legal entity in an input.

Always review field validations and mapping to ensure you can trigger your process without any errors.

Important

1//In this example, we want to create a revenue schedule for an invoice line after its status changes to Posted.
2//The example uses the InvoiceLine object in the billing package, where the API name is blng__InvoiceLine__c.
3//For your specific use case, replace blng__InvoiceLine__c with the desired API name object.
4
5trigger GenerateRevenueOnInvoiceLineActivation on blng__InvoiceLine__c (before insert) {
6   
7    blng__InvoiceLine__c[] newInvoiceLine = Trigger.new;
8    blng__InvoiceLine__c[] oldInvoiceLine = Trigger.old;
9    
10    blng.RevenueRecognitionInput[] inputs = new List<blng.RevenueRecognitionInput>();
11    
12    Integer i = 0;
13    for (blng__InvoiceLine__c newInvoiceLine : newInvoiceLine) {
14        if (newInvoiceLine.blng__InvoiceLineStatus__c != oldInvoiceLine[i].blng__InvoiceLineStatus__c && 
15            newInvoiceLine.blng__InvoiceLineStatus__c == 'Posted') {
16            blng.RevenueRecognitionInput input = new blng.RevenueRecognitionInput();
17            input.source = newInvoiceLine.ID;
18            input.sourceFieldName = 'InvoiceLine';
19            input.revenueAmount = newInvoiceLine.blng__Balance__c;
20            input.startDate = newInvoiceLine.blng__StartDate__c;
21            input.endDate = (Date.today()).addMonths(2);
22            input.revenueRecognitionRuleId = newInvoiceLine.blng__Product__r.blng__RevenueRecognitionRule__c;
23            input.legalEntityId = newInvoiceLine.blng__LegalEntity__c;
24            inputs.add(input);
25        }
26        i++;
27    }
28    
29    if (inputs != null && inputs.size() > 0) {
30        List<blng.RevenueRecognitionResponse> response = blng.RevenueRecognition.recognizeRevenue(inputs);
31    }
32 
33}