Newer Version Available

This content describes an older version of this product. View Latest

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, review 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've included the legal entity in an input. Make sure to always review field validations and mapping to ensure you can trigger your process without errors.

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
3
4trigger GenerateRevenueOnInvoiceLineActivation on InvoiceLine (before insert) {
5    InvoiceLine[] newInvoiceLine = Trigger.new;
6    InvoiceLine[] oldInvoiceLine = Trigger.old;
7    
8    RevenueRecognitionInput[] inputs = new List<RevenueRecognitionInput>();
9    
10    Integer i = 0;
11    for (InvoiceLine newInvoiceLine : newInvoiceLines) {
12        if (newInvoiceLine.blng__InvoiceLineStatus__c != oldInvoiceLines[i].blng__InvoiceLineStatus__c && 
13            newInvoiceLine.blng__InvoiceLineStatus__c == 'Posted') {
14            RevenueRecognitionInput input = new RevenueRecognitionInput();
15            input.source = newInvoiceLine.ID;
16            input.sourceFieldName = 'InvoiceLine';
17            input.revenueAmount = newInvoiceLine.blng__Balance__c;
18            input.startDate = newInvoiceLine.blng__StartDate__c;
19            input.endDate = (Date.today()).addMonths(2);
20            input.revenueRecoginitionRuleId = newInvoiceLine.blng__Product__c.blng__RevenueRecognitionRule__c;
21            input.legalEntity = newInvoiceLine.blng_LegalEntity__c;
22            inputs.add(input);
23        }
24        i++;
25    }
26    
27    if (inputs != null && inputs.size() > 0) {
28        RevenueRecognitionService revenueRecognitionService = RevenueRecognitionService.getInstance();
29        List<RevenueRecognitionResponse> response = revenueRecognitionService.createRevenueSchedulesForAnyEntity(inputs);
30    }
31}