Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Object reference not set to an instance of an object plugin(Create message-post operation plugin) Error

Posted on by Microsoft Employee

Entity

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Object reference not set to an instance of an object plugin(Create message-post operation plugin) Error

    c

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Object reference not set to an instance of an object plugin(Create message-post operation plugin) Error

    Sorry for typo , i'll send the actual code

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User on at
    RE: Object reference not set to an instance of an object plugin(Create message-post operation plugin) Error

    Hi,

    As per comment f1 is lookup of Entity B but in code you have used f1 has money.

    Also, please below highlighted part as f3 declaration seems to be missing;

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;


    namespace Acc
    {
    public class Acc : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {

    #region srvc
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    #endregion

    tracingService.Trace("Message from tracing Plugin.. step 1");
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.Depth == 1)
    {
    Entity A = (Entity)context.InputParameters["Target"];
    if (A.LogicalName == "A_name")
    {


    try
    {
    if (A.Attributes.Contains("f1") && A.Attributes.Contains("f2"))
    {
    decimal f1 = (A.Attributes.Contains("f1")) ? A.GetAttributeValue<Money>("f1").Value : 0;
    Guid f2 = (A.Attributes.Contains("f2")) ? A.GetAttributeValue<EntityReference>("f2").Id : Guid.Empty;
    // condition 1
    if (f1 == 0 && f2 != null)
    {
    Entity C = service.Retrieve("c_name", f2, new ColumnSet("f5", "f3"));
    int f5 = (C.Attributes.Contains("f5")) ? C.GetAttributeValue<OptionSetValue>("f5").Value : 0;
    Guid custid = (C.Attributes.Contains("f3")) ? C.GetAttributeValue<EntityReference>("f3").Id : Guid.Empty;
    if (f3 == 804020006) //f3 declaration missing in code
    {
    Entity acc = service.Retrieve("account", custid, new ColumnSet("f4"));
    if (acc.Attributes.Contains("f4") && acc["f4"] != null)
    {
    acc["f4"] = true;
    service.Update(acc);
    }
    }
    }

    catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); }

    }


    }
    }
    }
    }

    Also, for better development experience please add multiple tracing inside code.

    Thanks,
    Pradeep

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans