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)

Sample Plugin

(0) ShareShare
ReportReport
Posted on by

Hi,

my logic  is through plugin i entered some fields in account form

i have wright some code but code working record is not created. below copy my code.

using System;
using Microsoft.Xrm.Sdk;
namespace SecondPlugin

{
public class Class1 : IPlugin
{

public void Execute(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);

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

if (entity.LogicalName != "account")
{
try
{
Entity followup = new Entity("account");
followup["name"] = "MS CRM";
followup["telephone1"] = "9036119324";
followup["emailaddress1"] = "Naresh@gmail.com";
service.Create(followup);
}


catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
}

regards,

Naresh

*This post is locked for comments

I have the same question (0)
  • Andreas Cieslik Profile Picture
    9,267 on at
    RE: Sample Plugin

    How have you registered that plugin? On which event? Post Operation of account creation/update?

  • ashlega Profile Picture
    34,477 on at
    RE: Sample Plugin

    Hi,

    did you register the plugin? Do you see any error messages?

  • Community Member Profile Picture
    on at
    RE: Sample Plugin

    Hi Alex,

    Thanks for response i was registered plugin successfully i didn't get any error.

    Thanks,

    Naresh

  • Community Member Profile Picture
    on at
    RE: Sample Plugin

    Hi,

    I am using create message and post operation.

    Thanks,

    Naresh

  • Suggested answer
    Haansi Profile Picture
    1,433 Moderator on at
    RE: Sample Plugin

    [quote user="naresh pabba"]if (entity.LogicalName != "account")[/quote]

    This line of code will execute if entity is not account. Do you want to do that ?

    Please share how you have configured plugin (which entity, attribute and message you have used)

  • ashlega Profile Picture
    34,477 on at
    RE: Sample Plugin

    Hi naresh,

     just to confirm.. you want that plugin to create ANOTHER account, right?

     If you wanted to update fields on the same account, do not create that follow up and use "entity" (which is the target) instead.. then remove service.Create(followup); and make sure the plugin is registered in the pre-create if that's what you want

  • Suggested answer
    Haansi Profile Picture
    1,433 Moderator on at
    RE: Sample Plugin

    is it on account entity ? if so your code is saying not to run if entity is account.

    Change 

    if (entity.LogicalName != "account")

    with 

    if (entity.LogicalName == "account")

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: Sample Plugin

    There error is this condition:

    if (entity.LogicalName != "account")

    as your plugin runs on post create of account it should be

    if (entity.LogicalName == "account")

    otherwise account create code will never be reached.

  • Suggested answer
    Haansi Profile Picture
    1,433 Moderator on at
    RE: Sample Plugin

    Hi Naresh,

    Hope you have found what was issue in your code.

    Before closing for today, I m leaving a few notes for you in case you still have issues.

    Are you entering a new record in plugin or adding values to record you saved before ? See following lines of your code.

    [quote user="naresh pabba"]

    if (entity.LogicalName != "account")
    {
    try
    {
    Entity followup = new Entity("account");
    followup["name"] = "MS CRM";
    followup["telephone1"] = "9036119324";
    followup["emailaddress1"] = "Naresh@gmail.com";
    service.Create(followup);
    }[/quote]

    1. if (entity.LogicalName != "account") is saying run this code if entity is not account.

    2. Another thing to look is Entity followup = new Entity("account"); By this line you are not updating record which you saved in form, rather you are creating a new account here. If you want to update existing record see this line you have done:

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

    Use entity and not followup.

  • Community Member Profile Picture
    on at
    RE: Sample Plugin

    Hi,

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using Microsoft.Xrm.Sdk;

    using System.Text;

    using System.Threading.Tasks;

    namespace SecondPlugin

    {

       public class Class1 : IPlugin

       {

           public void Execute(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);

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

               {

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

                   if (entity.LogicalName == "account")

                   {

                       try

                       {

                           Entity col = new Entity("account");

                           col["name"] = "MS CRM";

                           col["telephone1"] = "9036119324";

                           col["emailaddress1"] = "Naresh@gmail.com";

                           //service.Create(col);

                       }

                       catch (Exception ex)

                       {

                           tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

                           throw;

                       }

                   }

               }

           }

       }

    }

    i am using create message and preoperation code getting any error assembly should be registered successfully but fields not created

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans