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 :
Microsoft Dynamics CRM (Archived)

Dynamics CRM 2015 Plugin - Update Sales Order through SDK

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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)

  • Verified answer
    Community Member Profile Picture
    on at

    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.

  • ashlega Profile Picture
    34,477 on at

    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"));

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Wayne Walton Profile Picture

Wayne Walton 2

#2
Good.Panos Profile Picture

Good.Panos 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans