web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Access async method from x++

(0) ShareShare
ReportReport
Posted on by 250

I have the follwoing code. Written in  a c# library refernce in my x project.

 public async Task FetchRef(PayloadModel model, string apiKey, string url)
        {
            try
            {
                string hashValue = "xxxxxx"
                var authorization = $"Bearer {hashValue}";

                var resps =await  url
                    .WithHeader("Authorization", authorization)
                    .PostJsonAsync(model)
                    .ReceiveString();
                var jsonResult = resps.Replace("jsonp (", "")
                     .Replace(")", "");
                var response = JsonConvert.DeserializeObject(jsonResult);
                if (response == null)
                    throw new Exception("Invalid Remita response");
                if (!string.IsNullOrEmpty(response.statusCode) && response.statusCode.Equals("025"))
                {
                    return response;
                }
                else
                {
                    throw new Exception(response.statusMessage);
                }
            }
            catch (Exception exception)
            {

                throw new Exception(exception.Message);
            }
        }

Calling this code form x returns an error that the object am accessing is a Task and not the object i was expecting.

How can i achieve this. Thanks

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    The problem is in your expectations - that the method returns a Task<T> instance is correct.

    Try something like this:

    var task = obj.FetchRef(...);
    ReferenceResponseModel result = task.Result;

    It would work in C#. I can't currently test if it behaves the same in X , but I think I used it before and it does work.

  • Suggested answer
    Deekshit Addepalli Profile Picture
    on at

    Based on the code you wrote, the method (FetchRef) will return a Task type object. You can use  the .Result property to get the value if that is what you are expecting.

  • Matthew Ireti Profile Picture
    250 on at

    Will i have to do this in C# or X++, because am getting an exception on the task.Result Line in x++

  • Deekshit Addepalli Profile Picture
    on at

    I havent tried in X++, but curious to know what the exception is?

  • Deekshit Addepalli Profile Picture
    on at

    If you debug in Visual Studio, what do these objects contain?

    var jsonResult = resps.Replace("jsonp (", "") .Replace(")", ""); -- This should have a proper value

    var response = JsonConvert.DeserializeObject<ReferenceResponseModel>(jsonResult); -- This should have a readable value as well

  • Matthew Ireti Profile Picture
    250 on at

    Microsoft.Dynamics.Ax.Xpp.InfoException: Error executing code: Wrong type of argument for conversion function. at Microsoft.Dynamics.Ax.MSIL.Interop.throwException(Int32 ExceptionValue, interpret* ip) at Microsoft.Dynamics.Ax.MSIL.Interop.ThrowCQLError(IL_CQL_ERR cqlErr) at Microsoft.Dynamics.Ax.Xpp.CastHelper.ObjectToString(Object _value) at Dynamics.AX.Application.RemitaConnector.`generateRRR(RemitaRefRequestContract model) in xppSource://Source/NCLEAS\AxClass_RemitaConnector.xpp:line 77 at Dynamics.AX.Application.RemitaConnector.generateRRR(RemitaRefRequestContract model) at Dynamics.AX.Application.NetworkCodeHelper.`main(Args args) in xppSource://Source/NCLEAS\AxClass_NetworkCodeHelper.xpp:line 9 at Dynamics.AX.Application.NetworkCodeHelper.main(Args args) at NetworkCodeHelper::Main(Object[] , Boolean& ) at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters) at Dynamics.AX.Application.SysClassRunnerHelper.`runMainMethod(SysClassRunnerState state) in xppSource://Source/ApplicationFoundation\AxClass_SysClassRunnerHelper.xpp:line 30 at Dynamics.AX.Application.SysClassRunnerHelper.runMainMethod(SysClassRunnerState state) at Dynamics.AX.Application.Forms.SysClassRunner.FormControl.timerControl.elapsed() in xppSource://Source/ApplicationFoundation\AxForm_SysClassRunner.xpp:line 52

    Although this was after i moved the code to c#

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    Do you still have the problem? Your last response suggests you have, but at the same time this thread already has a verified answer.

    If you have, please show us your current code and tell us which statement is throwing the error. It seems to be at line 77 of RemitaConnector class.

  • Matthew Ireti Profile Picture
    250 on at

    I no longer have the problem. The issue was the endpoint was throwing exception and it was not transmitted properly to X++.

    Though i have CLError exceptions handled.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans