Sunday, September 21, 2025

Business event [Purchase order confirmation] using x++ D365fo

 Business Event Class – POConfirmationBusinessEvent:


[BusinessEvents(classStr(POConfirmationContract),"POConfirmationBE",

    "POConfirmationBEDesc",ModuleAxapta::PurchaseOrder)]

public final class POConfirmationBusinessEvent extends BusinessEventsBase

{

    PurchTable purchTable;

    private PurchTable parmPurchTable(PurchTable _purchTable = purchTable)

    {

        purchTable = _purchTable;

        return purchTable;

    }

    private void new()

    {

    }

    public static POConfirmationBusinessEvent construct(PurchTable _purchTable)

    {

        var event = new POConfirmationBusinessEvent();

        event.parmPurchTable(_purchTable);

        return event;

    }

    public BusinessEventsContract buildContract()

    {

        return POConfirmationContract::newFromPurchTable(purchTable);

    }

}


Contract Class – POConfirmationContract:

[DataContract]

internal final class POConfirmationContract extends BusinessEventsContract

{

    str jsonFormat;

    public static POConfirmationContract newFromPurchTable(PurchTable _purchTable)

    {

        var contract = new POConfirmationContract();

        contract.initialize(_purchTable);

        return contract;

    }

    public void initialize(PurchTable _purchTable)

    {

        jsonFormat = this.buildJson(_purchTable); 

    }

    public str buildJson(PurchTable _purchTable)

{

    System.IO.StringWriter stringWriter = new System.IO.StringWriter();

    Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter);

    PurchLineAllVersions purchLineAllVersions;

    InventDistinctProductExpanded productInfo;

    VendParameters vendParams = VendParameters::find();

    jsonWriter.WriteStartObject();

    jsonWriter.WritePropertyName("documentID");

    jsonWriter.WriteValue(vendParams.DocumentId);


    jsonWriter.WritePropertyName("customerReference");

    jsonWriter.WriteStartObject();

    jsonWriter.WritePropertyName("documentID");

    jsonWriter.WriteValue(_purchTable.VendorRef);

    jsonWriter.WriteEndObject();


    jsonWriter.WritePropertyName("supplierID");

    jsonWriter.WriteValue(""); // Optional: static or configured value


    jsonWriter.WritePropertyName("buyerID");

    jsonWriter.WriteValue(vendParams.BuyerPartyId);


    jsonWriter.WritePropertyName("consigneeID");

    jsonWriter.WriteValue(_purchTable.DealerNumber);


    jsonWriter.WritePropertyName("orderLines");

    jsonWriter.WriteStartArray();


    while select LineNumber, ItemId, PurchQty from purchLineAllVersions

        join SearchName from productInfo

        where purchLineAllVersions.PurchId == _purchTable.PurchId &&

              purchLineAllVersions.ItemId == productInfo.ItemId

    {

        jsonWriter.WriteStartObject();


        jsonWriter.WritePropertyName("lineID");

        jsonWriter.WriteValue(any2Str(purchLineAllVersions.LineNumber));


        jsonWriter.WritePropertyName("itemID");

        jsonWriter.WriteValue(purchLineAllVersions.ItemId);


        jsonWriter.WritePropertyName("itemName");

        jsonWriter.WriteValue(productInfo.SearchName);


        jsonWriter.WritePropertyName("quantity");

        jsonWriter.WriteValue(any2Str(purchLineAllVersions.PurchQty));


        jsonWriter.WritePropertyName("deliveryDate");

        jsonWriter.WriteValue(date2Str(_purchTable.DeliveryDate, 321, 2, '-', 2, '-', 4));


        jsonWriter.WriteEndObject();

    }

    jsonWriter.WriteEndArray();

    jsonWriter.WriteEndObject();

    return stringWriter.ToString();

}

    [DataMember("JsonFormat"), BusinessEventsDataMember("JsonFormat")]

    public str parmJson(str _jsonFormat = jsonFormat)

    {

        jsonFormat = _jsonFormat;

        return jsonFormat;

    }

}



Call API using x++ D365FO

 System.Net.HttpWebRequest    request; System.Net.HttpWebResponse   response; System.IO.Stream             dataStream; System.IO.StreamReade...