› Dynamics 365, Enterprise › Invoking Web Service/Rest API from D365 FOE

Invoking Web Service/Rest API from D365 FOE

 Sohagani Shah   Leave a comment

In this blog article, we will see how we can invoke web service call for a third-party application in Dynamics 365 for Finance and Operations, Enterprise Edition using X++.

In this blog we will Consider Service Order Entity as source passing Service Order values to a third party application on Service Order creation using web service endpoint url.

  • Create a SMAServiceOrderTable Table post Insert event.
    /// <summary>
    /// Post insert passing Service Order values and URL to invoke Web service
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [DataEventHandler(tableStr(SMAServiceOrderTable), DataEventType::Inserted)]
    public static void SMAServiceOrderTable_onInserted(Common sender, DataEventArgs e)
    {
            SMAServiceOrderTable serviceOrderTable;
            serviceOrdertable = sender as SMAServiceOrderTable;
            String15 SOstatus = enum2Str(serviceOrderTable.Progress);        
    
            //parmvalue stores Service Order values in container
            container parmvalue = ["'CustAccount':'"+serviceOrderTable.CustAccount+"'","'ServiceOrderId':'"+serviceOrderTable.ServiceOrderId+"'","'CurrencyCode':'US Dollar'","'SOStatus':'"+SOstatus+"'","'ProjId':'"+serviceOrderTable.ProjId+"'","'CFSCRMWorkOrderNo':'"+serviceOrderTable.CFSCRMWorkOrderNo+"'"];
    
            new WebService().sendrecord("<<webservice url>>",parmvalue);
    
    }
  • Create a class which calls the web service.
    Class WebService
    {
    public void sendrecord(String255 endpointurl,container arryI)
        {
            str                             url;
            str                             postData;
            str                             returnValue;
            System.Net.HttpWebRequest       request;
            System.Net.HttpWebResponse      response;
            System.Byte[]                   byteArray;
            System.IO.Stream                dataStream;
            System.IO.StreamReader          streamReader;
            System.Net.ServicePoint         servicePoint;
            System.Net.ServicePointManager  servicePointManager;
            CLRObject                       clrObj;
            System.Text.Encoding            utf8;
            Counter  countCounter;
            ;        
            //generate postdata in json format
            postData = "{";
            for(countCounter= 1;countCounter<= conlen(arryI);countCounter++)
            {
                postData = postData+conpeek(arryI,countCounter)+",";       
    
                info(strFmt("%1",postData));
            }
            postData = postData+"}";       
    
            new InteropPermission(InteropKind::ClrInterop).assert();
            url = endpointurl;
            clrObj = System.Net.WebRequest::Create(url);                     
            System.Net.ServicePointManager::set_Expect100Continue(false);
            request = clrObj;
            request.set_Method("POST");
            utf8 = System.Text.Encoding::get_UTF8();
            byteArray = utf8.GetBytes(postData);
            request.set_ContentType("application/json");
    
           request.set_ContentLength(byteArray.get_Length());
            dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.get_Length());
            dataStream.Close();
    }
    
    }

Let me know your reviews. I will soon come up with more articles, as I further explore D365 Operations.


Liked it? Share it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.