Solved

Get Data From Third Party API into sales order screen

  • 7 July 2023
  • 7 replies
  • 71 views

Userlevel 3
Badge

Hi,

I have a third party rest api to get bank details. 

When calling the API it response is json format.

I need to add response details into my sales order screen.In SOOrderEntry Graph extension I have created a action and calling the API below like that.

public PXAction<SOOrder> MyAction;
[PXUIField(DisplayName = "ADD ORDER", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXButton(CommitChanges = true)]
public virtual IEnumerable myAction(PXAdapter adapter)
{
var URL = "https://sandbox.corporate-api.hsbc.com/mock/v2/transactions";
var httpRequest = (HttpWebRequest)WebRequest.Create(URL);
httpRequest.Method = "POST";
httpRequest.ContentLength = 0;

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

SOOrder order = Base.Document.Current;
order.OrderDesc = httpResponse.StatusCode.ToString();
return adapter.Get();

}

When clicking the ADD ORDER button description field is updated with response code.Then I need to set the Reponse Body’s StatusDesc with my description field. How I do that?

 

icon

Best answer by jinin 11 July 2023, 16:01

View original

7 replies

Userlevel 7
Badge +11

Hi @jeewanishalika20 ,

Use the namespace RestSharp.
using RestSharp;

Userlevel 7
Badge +10

Hi @jeewanishalika20,

Did you try using the code below?

I believe it should work.

   order.OrderDesc = httpResponse.StatusDesc.ToString();

 

Regards,

Sweta

Userlevel 7
Badge +11

Hi @jeewanishalika20 

Please refer to the sample code provided below. This code will assist you in making an API call and retrieving a response from the third-party system. Feel free to make any necessary modifications based on your specific requirements.

 public static void WebAPICall(string url, string token, string password)
        {
            try
            {
              
                IRestResponse response = APIResponse(url, token, password);
                string content = response?.Content?.ToString();
                if (response?.StatusCode.ToString() != Constants.OK || (!string.IsNullOrEmpty(content) && !content.ToString().Contains(Constants.AuthentocationResponseCheck)))
                    throw new Exception(Messages.TestConnectionFailed);
                else    
                    {
                    // Read the response and assign to the respective field
                    
                     string response = response?.Content.ToString();
                    }
            }
            catch (WebException webEx)
            {
                throw new Exception(Messages.TestConnectionFailed + webEx.InnerException);
            }
            catch (Exception ex)
            {
                if (ex.Message == Constants.Ok )
                    throw new PXException(Messages.AuthenticationSuccess);
                else
                    throw new Exception(ex.Message);
            }
        }


  public static IRestResponse APIResponse(string url, string token, string password)
        {
            string responseCode = string.Empty;
            var client = new RestClient(url);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader(Constants.ContentTypeCred, Constants.ContentType);
            var body = @"{" + "\n" + @"    ""{0}"": ""{1}""," + "\n" + @"    ""{2}"": ""{3}""" + "\n" + @"}";
            body = body.Replace("{0}", Constants.Username.ToString().ToLower()).Replace("{1}", token);
            body = body.Replace("{2}", Constants.Password.ToString().ToLower()).Replace("{3}", password);
            request.AddParameter(Constants.ContentType, body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response;
}

Userlevel 3
Badge

Hi @jinin ,

Below error is shown 

The type or namespace name 'IRestResponse' could not be found (are you missing a using directive or an assembly reference?)

I’m using the acumatica customization project editor.

Userlevel 3
Badge

Hi ,

Below error is shown,

The type or namespace name 'IRestResponse' could not be found (are you missing a using directive or an assembly reference?)

I’m using customization project editor

Userlevel 3
Badge

Hi @sweta68 ,

Yes it shows error like below.

App_RuntimeCode\SOOrderEntry.cs(137): error CS1061: 'HttpWebResponse' does not contain a definition for 'StatusDesc' and no accessible extension method 'StatusDesc' accepting a first argument of type 'HttpWebResponse' could be found (are you missing a using directive or an assembly reference?)

 

 

Userlevel 7
Badge +10

@jeewanishalika20,

As per my understanding, HttpWebResponse does not contain the statusdesc field.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved