Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

Posted on by 40

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;

namespace Plugincode
{
    public class DelRecords : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is EntityReference))
            {
                if (context.MessageName == "Delete")
                {
                    Entity Contact = new Entity("contact");

                    service.Create(Contact);

                }
            }
        }
    }
}

  • Verified answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    My bad.

  • Ainee ahsan Profile Picture
    Ainee ahsan 40 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    Hey i did changed that particular line which u had told me to change but after deleting account record entity contact also associated with it got deleted will i have to create new fresh contact ? do i need to add anything in the code ?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    
    namespace Plugin1
    {
        public class DeletionRecord : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (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);
    
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
    
                    try
                    {
                        if (context.MessageName == "Delete")
                        {
                            
                            
                            {
                                Entity Contact = new Entity("contact");
                                Contact.Attributes["firstname"] = "Ainee";
                                Contact.Attributes["lastname"] = "Ahsan";
                                service.Create(Contact);
    
    
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());
                        throw;
                    }
    
    
    
                }
            }
        }
    }
    

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    when the message is Delete the Target is an EntityReference, that part is correct

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    Hi,

    In your code change the following line:

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

    To:

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

    If it still does not work, I would suggest:

    • Disable the plugin step.
    • Delete the Account and observe if you see any errors.
    • If it works, try creating a Contact from the UI (with the same user, you are registering plugin with). 
    • If there are no errors, please make sure you have followed the proper steps:
      • Your project should be a C# .Net Class Library.
      • You have installed the latest version of the SDK Assemblies

    If you are unsure of SDK Assemblies, re-create you project based on following steps:
    Tutorial: Write and register a plug-in (Microsoft Dataverse) - Power Apps | Microsoft Docs 

    Here is a similar issue:

    The Specified domain does not exist or cannot be contacted – BIDS Limited (bidynamics365.com)

  • Ainee ahsan Profile Picture
    Ainee ahsan 40 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    
    namespace Plugin1
    {
        public class DeletionRecord : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (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);
    
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is EntityReference)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
    
                    try
                    {
                        if (context.MessageName == "Delete")
                        {
                            
                            
                            {
                                Entity Contact = new Entity("contact");
                                Contact.Attributes["firstname"] = "Ainee";
                                Contact.Attributes["lastname"] = "Ahsan";
                                service.Create(Contact);
    
    
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());
                        throw;
                    }
    
    
    
                }
            }
        }
    }
    

    Can you plz see the code and tell me what is wrong here i need to delete account record on which auto creation of contact will take place i am unable to figure out what is it actually 

    it is throwing me the error when i am running it in crm 

    Screenshot-_2800_336_2900_.png

  • Ainee ahsan Profile Picture
    Ainee ahsan 40 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    
    namespace Plugin1
    {
        public class DeletionRecord : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (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);
    
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is EntityReference)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
    
                    try
                    {
                        if (context.MessageName == "Delete")
                        {
                            
                            
                            {
                                Entity Contact = new Entity("contact");
                                Contact.Attributes["firstname"] = "Ainee";
                                Contact.Attributes["lastname"] = "Ahsan";
                                service.Create(Contact);
    
    
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());
                        throw;
                    }
    
    
    
                }
            }
        }
    }
    

    can u plz see what mistake is deir after doing all necessary steps of plugins when i ran the crm it gave me this error and i am unable to figure out what actually  the problem is 

     Screenshot-_2800_337_2900_.png

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    Hi,

    Thank you for your query.

    Code is correct or not depends on the requirement. If you can give a little back-ground on the scenario, it would be great.

    However, I can see following problems right-away:

    • This plugin will only run on Delete (as per you code, if this is as per your requirement it is okay).
    • You are creating Contact record without setting any attributes (Like Name, Email etc.). At least set the Name.

    To se the name, you can set the attribute:

    Contact.Attributes["firstname"] = "John";

    Contact.Attributes["lastname"] = "Doe";

  • Suggested answer
    Akhil101 Profile Picture
    Akhil101 432 on at
    RE: is this code correct ? plugin code i have written to delete account entity record and create contact record automatically

    Sorry! Your code provided is incomplete. Is your requirement to delete and create record? if so, you need to first fire your plugin on an event basically. For creation, you need to map the field values before calling service.Create(Contact); like below.

    Entity contact = new Entity("contact");

    contact["firstname"] = "Test";

    contact["lastname"] = "Contact";

    service.Create(contact);

    You can always refer microsoft docs for your reference on how to write a plugin.

    docs.microsoft.com/.../tutorial-write-plug-in

    Hope this helps :)

    AKHIL

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