Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin doesn't work

Posted on by 2,397

Hi, I created a basic plugin that generates the account number when creating account, post operation, create.  I signed the assembly, and deployed successfully, and I can see the plugin registered in solution as well. But when I created a new account, the account number didn't show up. Please advise what else I missed.

        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                //</snippetAccountNumberPlugin2>

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName == "account")
                {
                    // An accountnumber attribute should not already exist because
                    // it is system generated.
                    if (entity.Attributes.Contains("accountnumber") == false)
                    {
                        // Create a new accountnumber attribute, set its value, and add
                        // the attribute to the entity's attribute collection.
                        Random rndgen = new Random();
                        entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
                    }
                    else
                    {
                        // Throw an error, because account numbers must be system generated.
                        // Throwing an InvalidPluginExecutionException will cause the error message
                        // to be displayed in a dialog of the Web application.
                        throw new InvalidPluginExecutionException("The account number can only be set by the system.");
                    }
                }
            }
        }


*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin doesn't work

    Thanks so much, Alex! We appreciate the help.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Plugin doesn't work

    It's not that pre-operation is before the creation.. It's pre-"database" and post-"database".. Pre-operation is before anything is stored in the database. Post-operation is after. You either need to set that attribute in the target before that attribute is stored in the database, or you have to call service.Update to update the record again if you do it in the post-operation. In other words, just follow Guido's advice:)

  • Suggested answer
    BadrinathB Profile Picture
    BadrinathB 970 on at
    RE: Plugin doesn't work

    change this line from

    entity.Attributes.Add("accountnumber", rndgen.Next().ToString());

    To

    localContext.Attributes.Add("accountnumber", rndgen.Next().ToString());

    You need to update/add the parameter to the context to get the value reflected.

    And you need to register the plugin on pre-operation for the create message of Account

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Plugin doesn't work

    Hi

    Yes. As Guido mentioned, you need to register pre-operation for the create message of Account.

    You need to set the values before creating (pre-operation) the new record.

  • sdnd2000 Profile Picture
    sdnd2000 2,397 on at
    RE: Plugin doesn't work

    Is it not something after the account creation? pre-operation is before the creation?

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Plugin doesn't work

    when you modify the target, you need to be on pre-operation, otherwise this row will not work:

    entity.Attributes.Add("accountnumber", rndgen.Next().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

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans