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)

Problem with plugin using assign message

(0) ShareShare
ReportReport
Posted on by

Hi! I'm creating a plugin using assign message.

I would like to prevent assigning an account record and throw a message by checking two option sets on account entity and verify if one or the two fields don't contain any value. When I assigned a record, even if one or the two fields I had mentioned, assignation is done. So my plugin doesn't work. I used Assign message and Pre-Validation Pipeline stage

This is the code I used. 

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

                Entity _entity = (Entity)context.InputParameters["Target"];

                if (_entity.LogicalName == "account")
                {
                    string ErrorMsg = "Please unquire fields \"Statut de la localité\" et \"Type de route\" ";
                    bool ContainingTargetFields = !_entity.Attributes.Contains("asc_statutlocalite") && !_entity.Attributes.Contains("asc_typederoute");
                    
                    OptionSetValue StatutLocalite = (OptionSetValue)_entity.Attributes["asc_statutlocalite"];
                    int? StatutLocaliteValue = StatutLocalite.Value;

                    OptionSetValue TypeRoute = (OptionSetValue)_entity.Attributes["asc_statutlocalite"];
                    int? TypeRouteValue = TypeRoute.Value;

                    bool StatutLocalite_Or_TypeRoute_AreEmptyFields = StatutLocaliteValue == null || TypeRouteValue == null;
                    try
                    {
                        if (ContainingTargetFields)
                        {
                            if (StatutLocaliteValue == null || TypeRouteValue == null)
                            {
                                throw new InvalidPluginExecutionException(ErrorMsg);
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("Erreur", ex);
                    }
                }
            }

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Will,

    The assign message means that only change the owner field and only have something like Target and Assignee in the Input Parameter, so I don't think that line after this code will be executed:

    if (ContainingTargetFields)

    Because if you see:

    bool ContainingTargetFields = !_entity.Attributes.Contains("asc_statutlocalite") && !_entity.Attributes.Contains("asc_typederoute");

    I don't think that the entity contains this "asc_statutlocalite" field..

    Because you don't update any field on it.

    So, I will suggest you to retrieve the Account record first then get the field.

    And in the first if,

    I am not sure that this: if (_entity.LogicalName == "account") will work.

    I suggest you to use this method:

    // Verify the message context

               if (context.InputParameters.Properties.Contains("Target") ||

                   context.InputParameters.Properties["Target"] is Moniker == false ||

                   context.PrimaryEntityName != entityName)

               {

                   return;

                 }

                //continue your logic here

    See this:

    danielcai.blogspot.com/.../use-crm-assign-message.html

    So, you need to get the current Account Id using this:

    Moniker moniker = (Moniker)context.InputParameters.Properties["Target"];

    Guid accountid = moniker.Id;

    Then after that you can use to retrieve the Account using service.Retrieve() function like usual.

    After that, you will get the entity.

    So you need to do something like:

    *Was

    Entity _entity = (Entity)context.InputParameters["Target"];

    *Change to

    Entity _entity = service.Retrieve("account", accountid, new ColumnSet(true));

    Then you can continue to retrieve the fields, your option set checking.

    But remember to change your if using the link method.

    Then just continue.

    Sorry for typo error, I am not using any compiler editor.

    Hope this helps.

    Thanks.

  • Community Member Profile Picture
    on at

    Hi Aileen,

    Thx for your reply.

    I reorganized my code but, I couldn't test my plugin because it didn't find assembly for Moniker class and definition for Properties when I was running compilation.

    Moniker moniker = (Moniker)context.InputParameters.Properties["Target"];

    I downloaded CRM SDK 2015 but it don't contain DeveloperToolkit folder containing installer. So what can I do to resolve this problem?

    I'm using VS 2012 Premium.

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Will,

    It seems Moniker class has been replaced with EntityReference in CRM 2015.

    Reference:

    msdn.microsoft.com/.../microsoft.xrm.sdk.entityreference.aspx

    SO, instead of using Moniker, please use EntityReference, ok.

    Try to use:

    // Obtain the target entity from the input parameters.

       EntityReference entity = (EntityReference)context.InputParameters["Target"];

    You wont need .InputParameters.Properties anymore.

    The CRM 2015 SDK doesn't have Developer Toolkit, so don't use it, Will.

    Hope this helps.

    Thanks.

  • Community Member Profile Picture
    on at

    Thx! I'll try and show you the result. :)

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Will,

    I hope it works :)

    Thanks.

  • Community Member Profile Picture
    on at

    Hi Aileen,

    I did everything you told me and It's work now! However I had have some problem on retrieving optionset fields (or value), but I found the solution. I mark that lines. Would you please my english becaus it's not good. My mother tongue is french.

    This is the final code.  Thx a lot !!! :) :)

    IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;

                const string entityName = "account";
                string ErrorMsg = "Veuillez renseigner les champs : \n \"STATUT DE LA LOCALITE\" ET \"TYPE DE ROUTE\"";

                bool messageContextTest = (context.InputParameters["Target"] is EntityReference == false ||
                                           context.PrimaryEntityName != entityName);
               
                if (messageContextTest)
                {
                    return;
                }
               
                    EntityReference moniker = (EntityReference)context.InputParameters["Target"];
                    Guid accountid = moniker.Id;
                    Entity _entity = service.Retrieve(entityName, accountid, new ColumnSet(true));

                    OptionSetValue StatutLocalite = _entity.Attributes.Contains("asc_statutlocalite") ? _entity["asc_statutlocalite"] as OptionSetValue : null;
                    OptionSetValue TypeRoute = _entity.Attributes.Contains("asc_typederoute") ? _entity["asc_typederoute"] as OptionSetValue : null;

                    try
                    {

                        if (StatutLocalite == null || TypeRoute == null)
                        {
                            throw new InvalidPluginExecutionException(ErrorMsg);
                        }

                    }

                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("Erreur", ex);
                    }

  • Community Member Profile Picture
    on at

    Hi Aileen,

    I'd like to deploy my plugin on other organization. So, could you show me the way to do that?

    I'd like to have an explicit manner, very detailed step by step. From creating the package, adding files, etc.

    Thanks!

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Will,

    You want to like copy the plugin from A to B, right?

    Okay..

    1. Create a new solution, so-called Plug-in only

    2. Add the plugin assembly + SDK Message as the components (just plugin you want to add)

    3. Export it out, up to you, unmanaged or managed solution

    4. Import to Org B, do not forget to check the Activate SDK Message during import

    Other way, is using Plugin Registration Tool

    1. Download first the SDK (I guess you've done it)

    2. Go to Plugin Reg tool

    3. Register your plugin to the Org B.

    Nowadays, I recommend you to use first method (solution)

    But, remember to change your user context (Run as User context in plugin reg tool) if you use specific user registered.

    Hope this helps.

    Thanks.

  • Community Member Profile Picture
    on at

    Hi Aileen,

    Thanks for your replies! I did it and it works! :)

  • Community Member Profile Picture
    on at

    Hi Alieen,

    I developed a PlugIn which runs on the Prevalidation using assign message. If this validation fails, the user has to be notified with custome message.

    The display of my error messages is working fine also getting second alert as received "ISV Code aborted the operation"

    please suggest me.

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