Hi Folks,
I recently faced issue where PO confirmation business event was not triggering and failing my interface. Issue was whenever after first PO confirmation if user make change to PO header delivery date or cancellation date and reconfirm the PO, it wont trigger the respective Business event.
[Although there are different reason why business event is not considering this change, while if I make any financial changes like qty, line addition, tax etc, it works fine]
Ok lets see now how I can call a business event from code,
To trigger a business event in X++ code, you need to create a business event contract, an event handler class, and then trigger the event from your X++ code by using the BusinessEvent::newFromTable() and businessEvent.send() methods. (Also, configure the event in the business event catalog if its not configured)
1. Create a Business Event Contract:
Purpose: Defines the data that will be sent with the business event. For reference check CustFreeTextInvoicePostedBusinessEventContract.
2. Create an Event Handler Class:
Purpose: Handles the event when it's triggered, allowing you to perform actions based on the event.
For reference check CustTable_BusinessEvent_EventHandler
3. Trigger the Business Event from X++ Code:
Purpose: Use the BusinessEvent::newFromTable() and businessEvent.send() methods to trigger the event.
here is complete code sample,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ExtensionOf(classStr(CustFreeTextInvoicePostedBusinessEventContract))] | |
internal final class CustFreeTextInvoicePostedBusinessEventContractMyModel_Extension | |
{ | |
// contract extension state | |
private str customerClassification; | |
protected void initialize(CustInvoiceJour _custInvoiceJour) | |
{ | |
next initialize(_custInvoiceJour); | |
customerClassification = 'StandardCustomer'; | |
} | |
// contract extension data members | |
[DataMember('MyModelCustomerClassification')] | |
public str parmMyModelCustomerClassification(str _customerClassification = customerClassification) | |
{ | |
customerClassification = _customerClassification; | |
return customerClassification; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assuming you have a CustTable record named 'custTable' | |
BusinessEvent businessEvent = BusinessEvent::newFromTable( | |
tableStr(CustTable), | |
custTable | |
); | |
// Check if the business event is valid | |
if (businessEvent) | |
{ | |
// Send the business event | |
businessEvent.send(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class CustTable_BusinessEvent_EventHandler | |
{ | |
// Event handler method for the OnInserted event on the CustTable table | |
[Event(eventStr(OnInserted), tableStr(CustTable))] | |
public static void CustTable_OnInserted(CustTable _custTable) | |
{ | |
// Create a new business event instance | |
BusinessEvent businessEvent = BusinessEvent::newFromTable( | |
tableStr(CustTable), | |
_custTable | |
); | |
// Check if the business event is valid | |
if (businessEvent) | |
{ | |
// Send the business event | |
businessEvent.send(); | |
} | |
} | |
} |