Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Dynamics CRM 2015 Plugin - Update Sales Order through SDK

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello,

I have a plugin registered on the Sales Order Entity with Message 'Create'  on Stage 'PostOutsideTransaction' and in mode 'Synchronous'.

In the execute method that is fired upon the creation of the Sales Order Entity, I am trying to perform a very basic update.

Below is an example of the code.

When this plugin triggers, I can step through the code and it reaches the line of service.Update(mySalesOrder).

Upon the execution of this line, I am gifted an error message stating 'Object reference not set to an instance of an object'.

Any help with this would be greatly appreciated.

For what it's worth too... I do have code that can add sales order details to this sales order and it works just fine... so I am not sure why I cannot perform a simple update on the sales order itself.

protected void ExecutePostOrderCreate(LocalPluginContext localContext)

{

if (localContext == null)

{

throw new ArgumentNullException("localContext");

}

IServiceProvider _service = localContext.ServiceProvider;

OrganizationServiceProxy _org = (OrganizationServiceProxy)localContext.OrganizationService;

IPluginExecutionContext _context = localContext.PluginExecutionContext;

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)_service.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(_context.UserId);

if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] is Entity && _context.Depth < 2)

{

var targetEntity = (Entity)_context.InputParameters["Target"];

if (targetEntity.LogicalName == "salesorder")

{

SalesOrder mySalesOrder = (SalesOrder)service.Retrieve(SalesOrder.EntityLogicalName, targetEntity.Id, new ColumnSet(true));

mySalesOrder.Name += " Test";

service.Update(mySalesOrder);

}

}

}

*This post is locked for comments

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Dynamics CRM 2015 Plugin - Update Sales Order through SDK

    No problem. Also, you might want to try reducing the list of columns in that retrieve.. Even though you are updating Name only, what you are sending back in the update is all the attribute you have retrieved. So if there are plugins/workflows registered on those other attributes, they will trigger. In other words, this may also help:

    SalesOrder mySalesOrder = (SalesOrder)service.Retrieve(SalesOrder.EntityLogicalName, targetEntity.Id, new ColumnSet("name", "salesorderid"));

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2015 Plugin - Update Sales Order through SDK

    Alex,

    Thank you for the reply and pointing me in the right direction.

    There is a lot of custom workflows, javascripts, and other plugins that are running in the environment.

    I"ll see if I can narrow it down.

    Thank you.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Dynamics CRM 2015 Plugin - Update Sales Order through SDK

    Hi,

     Is it the only plugin you have on the sales order entity? If there are other plugins triggering on update, they will run, too (once you call service.Update). The error might be coming from one of those other plugins (or, maybe, realtime workflow.. if there are custom workflow activities)

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,820 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans