x++ code to send messages from dynamics 365 to azure service queue

 step1 : Create a Runnable Class (Job)

class BAS_SendMessageToAzure

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    { 

        //Azure connection string

        str connectionString = 'Endpoint=sb://basofaservicebus.servicebus.windows.net/;SharedAccessKeyName=Manage;SharedAccessKey=YRLWqKSymqC/ghx1e9U7q/5ZVhd5t7Uqc+ASbCPeIl0='; 

        // queue name

        str queueName = 'test-queue'; 


        System.IO.StringWriter          stringWriter; // used to manipulate and generate text data.

        Newtonsoft.Json.JsonTextWriter  jsonWriter; // used to write data in json format

        CustTable                       custTable;

        stringWriter       = new System.IO.StringWriter();

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

 

        select AccountNum, Currency from custTable

            where custTable.AccountNum == "004009";

        // Start writing the JSON object

        jsonWriter.WriteStartObject();

        // Write properties and values

        jsonWriter.WritePropertyName("AccountNum");

        jsonWriter.WriteValue(custTable.AccountNum);

        jsonWriter.WritePropertyName("Name");

        jsonWriter.WriteValue(custTable.nameAlias());

        jsonWriter.WritePropertyName("Currency");

        jsonWriter.WriteValue(custTable.Currency);

        // End writing the JSON object

        jsonWriter.WriteEndObject();     

   // it is a class that was part of microsoft azure service bus messaging library.

  // QueueClient is a class in the Azure Service Bus messaging library that allowed developers to interact with Azure Service Bus queues.

  // With QueueClient, we could send messages to a queue, receive messages from a queue

 Microsoft.ServiceBus.Messaging.QueueClient queueClient = Microsoft.ServiceBus.Messaging.QueueClient::CreateFromConnectionString(connectionString,queueName);

                        //System.IO.Stream messageStream;

                        System.Byte[] reportBytes = new System.Byte[0]();

                        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

                        reportBytes = enc.GetBytes(stringWriter.ToString());

                        System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);

    //BrokeredMessage class represents a message that could be sent to or received from a queue in Azure Service Bus.                      

  Microsoft.ServiceBus.Messaging.BrokeredMessage brokeredMessage = new Microsoft.ServiceBus.Messaging.BrokeredMessage(stream);

                        // send method call is used to send a BrokeredMessage object to an Azure Service Bus queue.

                        queueClient.Send(brokeredMessage);

       

    }


}

Comments

Popular posts from this blog

How to Create a wizard in x++ d365

how to post trade agreement journals automatically using x++ code

x++ code to submit and approve and reject the invent movement workflow in d365 F&O