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)

Missing LocalPluginContext , LocalPluginContext not fou

(0) ShareShare
ReportReport
Posted on by 195

Hi i want to use LocalPluginContext, to get preimage entity, but i don't know why its missing, do i need to add any reference for this or it's already depecreated. so i need to get updated value from field and preupdated value from field and when im looking on the internet i found this usefull answer but i can't get LocalPluginContext 

protected void ExecuteYourPlugin(LocalPluginContext localContext)
{
    IPluginExecutionContext pluginContext = localContext.PluginExecutionContext;
    // Get Target Entity
    var updateEntity = (Entity)pluginContext.InputParameters["Target"];

    // Get PreImage Entity
    var preEntity = (Entity)localContext.PluginExecutionContext.PreEntityImages["PreImage"];

    // Is in Update
    if(localContext.PluginExecutionContext.MessageName.ToLower() == "update")
    {
        var fieldName = string.Empty;

            // If Contains, Extract value from Target Entity
            if(updateEntity.Contains("new_fieldname"))
            {
                // Cast the fields according their type
                fieldName = updateEntity["new_fieldname"].toString();
            }
            // Else extract the value from preImage
            else if(preEntity .Contains("new_fieldname"))
            {
                // Cast the fields according their type
                fieldName = preEntity["new_fieldname"].toString();
            }
    }

can someone help here thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi Keysersozeey,

    Read this article to understand the data context passed to a plug-in. Also make sure to check that you have registered the Pre-Image.

  • Verified answer
    Keysersozeey Profile Picture
    195 on at

    hi @AdrianBegovich

    thanks for your answer i'm already found the answer for that, i think the way for get preValue and postvalue that i found on the internet already depecreated or something, i can get the prevalue using preimage and postimage like this solution community.dynamics.com/.../pre-image-and-post-image-in-dynamics-crm

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    You don't need to add any reference, Can you share your plugin registration details?? just want to check as Target is not available in every event, if you have registered your plugin on update

    Make sure

    1. Your plugin firing.

    2. if you have used filtering attribute it means it will only fire on change of that specific field.

    Also you should check if it exists or not before trying to get value from it.

  • Keysersozeey Profile Picture
    195 on at

    yes i already the found the answer on the internet using Image, thanks for your answer

  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi Keysersozeey,

    You can use this as a template for your plug-in.

    using Microsoft.Xrm.Sdk;
    using System;
    
    namespace Namespace
    {
        public class Plugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); try { //Your code goes here } catch (Exception e) {
    tracer.Trace(e.ToString()); throw new InvalidPluginExecutionException(e.Message); } } } }
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    You should pass IServiceProvder as parameter which contains everything -

    Try with this -

            protected void ExecuteYourPlugin(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));         
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                // Get Target Entity
                var updateEntity = (Entity)context.InputParameters["Target"];
    
                // Get PreImage Entity
                var preEntity = (Entity)context.PreEntityImages["PreImage"];
    
                // Is in Update
                if (context.MessageName.ToLower() == "update")
                {
                    var fieldName = string.Empty;
    
                    // If Contains, Extract value from Target Entity
                    if (updateEntity.Contains("new_fieldname"))
                    {
                        // Cast the fields according their type
                        fieldName = updateEntity["new_fieldname"].ToString();
                    }
                    // Else extract the value from preImage
                    else if (preEntity.Contains("new_fieldname"))
                    {
                        // Cast the fields according their type
                        fieldName = preEntity["new_fieldname"].ToString();
                    }
                }
            }


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
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans