Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

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

(0) ShareShare
ReportReport
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 2024 Season 2 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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans