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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Moving Order State Working using XRM but not by WebAPI

(0) ShareShare
ReportReport
Posted on by 5

Hello Guys 

We are now new to support Dynamics platform . To change order status in Dynamics , we used XRM tool box and it worked fine. But when trying to automate using WebAPI Call it throws error Object Reference not set to instance of object. 

Can you please help  me to resolve this issue??

Regards

Praveen

I have the same question (0)
  • Ankit Sabharwal Profile Picture
    on at
    RE: Moving Order State Working using XRM but not by WebAPI

    Hy Praveen,

    Hope this post finds you well. Can you please share the code and explain in detail what you are trying to achieve.

    Thanks,

    Ankit Sabharwal

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Moving Order State Working using XRM but not by WebAPI

    Hi,

    Use below C# web api code to update status of order. Make sure you pass correct Statecode and statuscode integer values. Combinations should be correct.

    using (HttpClient httpClient = new HttpClient())
    		    {
    		        httpClient.BaseAddress = new Uri(Resource);
    		        httpClient.Timeout = new TimeSpan(0, 2, 0);
    		        httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
    		        httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
    		        httpClient.DefaultRequestHeaders.Accept.Add(
    		            new MediaTypeWithQualityHeaderValue("application/json"));
    		        httpClient.DefaultRequestHeaders.Authorization =
    		            new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);
    		            
    		            //update order status
    		            JObject updateOrder = new JObject
    		        {
    		            {"statecode", 0},
    		            {"statuscode", 2}
    		        };
    		        
    		         HttpResponseMessage updateResponse =
    		            await httpClient.SendAsJsonAsync(new HttpMethod("PATCH"), "api/data/v8.2/salesorders("   salesorderid{replace with guid of order}   ")", updateorder);
    		            }

    Please find below link which has sample Web API c# code to perform basic operations in CRM.

    https://github.com/jlattimer/CrmWebApiCSharp/blob/master/CrmWebApiCSharp/Program.cs

    If found helpful, Please mark my answer verified.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans