How to do it...

To create a data event handler, follow these steps:

  1. Create a new ConReportManager class.
  2. Open the table design for the table in question; in our case, double-click on the SalesConfirmHeaderTmp.ConReports table extension.
  3. Expand Events and locate onInserting.
The onInserted event is too late as we want to fill in a field before the record is written; if we subscribed to onInserted, the data will not be saved.
  1. Right-click on the event and choose Copy event handler method. This will create a code snippet, and place it in the paste buffer.
  2. Open the ConReportManager class, and paste in the code generated by step 4 into the class body, as shown here:
class ConReportManager 
{
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[DataEventHandler(tableStr(SalesConfirmHeaderTmp),
DataEventType::Inserting)]
public static void SalesConfirmHeaderTmp_onInserting(
Common sender, DataEventArgs e)
{
}
}
Look at the event declaration: it references the table, not the extension. This is important, as the extension is not a table, and the system correctly changes the subscription to the correct table.
  1. Next, we will need to write the code, and the obvious code to write may seem to be as follows:
SalesConfirmHeaderTmp header = sender; 
SalesTable salesTable;

select SalesPoolId
from salesTable
where SalesTable.SalesId == header.SalesId;

header.ConSalesPoolName = SalesPool::find(
SalesTable.SalesPoolId).Name;
This would be wrong, as we assume that the sales order record will never be deleted - and once invoiced, they can be deleted.
What we should do is add SalesPoolId to the CustConfirmJour table, which is the permanent record of that confirmation.
  1. Create an extension of CustConfirmJour, called CustConfirmJour.ConReports.
  2. Drag the SalesPoolId EDT from the Application Explorer, and rename it to ConSalesPoolId.
  3. Right-click on the onInserting event and choose Copy event handler method.
  4. Paste the method into the body of our ConReportManager class.
  5. Adjust the method so that it reads as follows:
/// <summary> 
/// Handles the inserting event of <c>CustConfirmJour</c>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[DataEventHandler(tableStr(CustConfirmJour),
DataEventType::Inserting)]
public static void CustConfirmJour_onInserting(
Common sender, DataEventArgs e)
{
CustConfirmJour jour = sender;
jour.ConSalesPoolId = jour.salesTable().SalesPoolId;
}
The record is passed by reference, and we must not call insert or update.
  1. Finally, adjust our SalesConfirmHeaderTmp handler so it reads as follows:
/// <summary> 
/// Handles inserting event <c>SalesConfirmHeaderTmp</c>
/// </summary>
/// <param name="sender">The calling record</param>
/// <param name="e"></param>
[DataEventHandler(tableStr(SalesConfirmHeaderTmp),
DataEventType::Inserting)]
public static void SalesConfirmHeaderTmp_onInserting(
Common sender, DataEventArgs e)
{
alesConfirmHeaderTmp header = sender;
CustConfirmJour jour;
select ConSalesPoolId
from jour
where jour.RecId == header.JournalRecId;
header.ConSalesPoolName =
SalesPool::find(jour.ConSalesPoolId).Name;
}
  1. Save and close the designer.
  2. To test this, perform a full build including database sync and create a sales confirmation. Then, check that the SalesPoolId field was populated in the CustConfirmJour table.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.105.181