Skip to main content

Notifications

Announcements

No record found.

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

  • RE: Sample Plugin

    Please go through the below link and try to use proper traces so that you can check them in the plugin trace log.

    msdn.microsoft.com/.../gg327941.aspx

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

    Pre-operation should work just fine.. but you have to do it like this:

    if (entity.LogicalName == "account")

                  {

                      try

                      {

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

                          entity["telephone1"] = "9036119324";

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

                      }

                      catch (Exception ex)

                      {

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

                          throw;

                      }

                  }

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

    It is because your plugin is configured on "Create" event (message) and in your code you are doing service.create.

    It is causing infinite loop and for security CRM is throwing this error. What happening is On create on account you are creating another account and so on...

    1. Dont use   Entity col = new Entity("account");

    remove it.

    Do like this:

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

    2. Don't use service.create, use service.update on Post Operation. 

  • RE: Sample Plugin

    Hi,

    When i using post operation given below error i came.

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>38eebde0-184d-404f-a4e2-ffe85071f6bb</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.</Message>

     <Timestamp>2017-08-29T13:01:14.1346685Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText>

    [SecondPlugin: SecondPlugin.Class1]

    [6987da04-ba8c-e711-8122-c4346bdc0e01: SecondPlugin.Class1: Create of account]

    </TraceText>

    </OrganizationServiceFault>

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

    [quote user="naresh pabba"]i am using create message and preoperation code getting any error assembly should be registered successfully but fields not created[/quote]

    You can't use preoperation to create something. You will have to use post op. Pre operation means before create operation so you can't create here.

    Share error message if you are getting an error.

  • 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

  • Suggested answer
    Haansi Profile Picture
    Haansi 1,431 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.

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 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
    Haansi 1,431 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")

  • ashlega Profile Picture
    ashlega 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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans