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

Why and When will the "preimage" be null?

(0) ShareShare
ReportReport
Posted on by

I've read some materials and I know if a field has no value before update, the preimage's attribute is not loaded and will be null.

In my case, the preimage object is null.  I've written two update plugins and in my first experience I debugged the code, and couldn't resolve this issue. This time, I followed another post still got into same problem.

protected void ExecutePreOpportunityOwnershipUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            // TODO: Implement your custom Plug-in business logic.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity target = (Entity)context.InputParameters["Target"];

                //Est Revenue
                Money new_estrevenue = null;
                if(target.Attributes.Contains("new_estrevenue"))
                    new_estrevenue = (Money)target.Attributes["new_estrevenue"];
                else
                    new_estrevenue = (Money)preImageEntity.Attributes["new_estrevenue"];

                //Actual Revenue
                Money new_actualrevenue = null;
                if (target.Attributes.Contains("new_actualrevenue"))
                    new_actualrevenue = (Money)target.Attributes["new_actualrevenue"];
                else
                    new_actualrevenue = (Money)preImageEntity.Attributes["new_actualrevenue"];

                //Revenue Percentage
                Decimal new_revenuepercentage = 0;
                if (target.Attributes.Contains("new_revenuepercentage"))
                    new_revenuepercentage = Convert.ToDecimal(target.Attributes["new_revenuepercentage"]);
                else
                    new_revenuepercentage = Convert.ToDecimal(preImageEntity.Attributes["new_revenuepercentage"]);


                //Est Revenue by Owner
                Money new_estrevenueforowner = new Money(new_revenuepercentage * new_estrevenue.Value);
                //Actual Revenue by Owner
                Money new_actualrevenuebyowner = new Money(new_revenuepercentage * new_actualrevenue.Value);

                target.Attributes.Add("new_estrevenueforowner", new_estrevenueforowner);
                target.Attributes.Add("new_actualrevenuebyowner", new_actualrevenuebyowner);
            }
        }
    }


 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Why and When will the "preimage" be null?

    Hi All,

    I have registered a Preimage in the update plugin step post operation but i am not able to read image information it is passing null into preimages property in the context. Same thing working fine in the CRM 2011 server but not on the CRM2015 server. Can any one help me on this what was the exact problem

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at
    RE: Why and When will the "preimage" be null?

    hi..

    My bad.. Pre-Image is not available only in pre create operation.

    Here are details

    crmbook.powerobjects.com/.../plug-in-images-pre-vs-post

    are you using

    this.preImageAlias = "OpportunityOwnershipPreUpdateImage"

    might be need to check spelling or case of PreImage Alias.. you need exactly same name you defined in plugin registration and code.. 

    Also you can try debugging your plugin code by using Plugin registration tool and see how many images are in context.PreEntityImages. 

    Here are details of how to debug plugin.. 

    http://nishantrana.me/2014/07/02/debugging-online-plugin-in-crm-2013/

  • Community Member Profile Picture
    on at
    RE: Why and When will the "preimage" be null?

    Mahadeo Matre ,

    I was folllowing crmbook.powerobjects.com/.../developing-a-plug-in example 2.

    I think he's creating a pre-operation plugin.

  • Community Member Profile Picture
    on at
    RE: Why and When will the "preimage" be null?

  • Community Member Profile Picture
    on at
    RE: Why and When will the "preimage" be null?

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at
    RE: Why and When will the "preimage" be null?

    hi..

    preImage  is available only on Post Operations, and You need to register preImage with plugin step.

    preimage1.png

    Also In code you need to use same PreImage name/ Entity Alias which you gave in Image registration

    If you are missing any of the option, then preImage will be null.

  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: Why and When will the "preimage" be null?

    Are you sure you have Pre-Image step registered in the Plugin step?  Just double check the plugin step you may forgot to add pre-image step.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans