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)

Creating a Plugin to Retrieve Surname, Firstname, Middlename & Store in Custom Fields

(0) ShareShare
ReportReport
Posted on by

Please I am trying to develop the functionality of allowing users to search for contacts when typed in any order, for example the contact 'Martin Luther King' should return same result even when typed as 'Martin King Luther', 'Luther Martin King' etc

So I added two custom fields 'new_fullnameSFM' & 'new_fullnameSMF' to capture full name in Surname/Firstname/Middlename format & Surname/Middlename/Firstname format respectively and activated them as 'Search Columns'

The issue I have now is I created a plugin such that when a user creates a new 'contact' record, it retrieves the Surname, Firstname & middlename & store them accordingly in the right format into the two custom fields but below code does not achieve result

Thanks

        protected void ExecutePreContactCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            ITracingService tracingService = localContext.TracingService;

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

            ColumnSet cols = new ColumnSet(new String[] { "lastname", "firstname", "middlename", "new_fullnamesfm", "new_fullnamesmf" });

            var contact = service.Retrieve("contact", entity.Id, cols);

            if (contact != null)
            {
                string lastnameValue = entity.GetAttributeValue<string>("lastname");
                string firstnameValue = entity.GetAttributeValue<string>("firstname");
                string midddlenameValue = entity.GetAttributeValue<string>("middlename");

                entity.Attributes["new_fullnamesfm"] = lastnameValue + " " + firstnameValue + " " + midddlenameValue;
                entity.Attributes["new_fullnamesmf"] = lastnameValue + " " + midddlenameValue + " " + firstnameValue;

            }

            service.Create(entity);
        }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ChangeFrenzoId Profile Picture
    465 on at

    Hi,

    try below code:

    Entity entity = (Entity)context.InputParameters["Target"]; //#1
    ColumnSet cols = new ColumnSet(new String[] { "lastname", "firstname", "middlename", "new_fullnamesfm", "new_fullnamesmf" });
    Entity contact = service.Retrieve("contact", entity.Id, cols); //No need to retrive again, u already have this same info in entity object (#1)
    
    if (contact != null)
    {
        string lastnameValue = entity.GetAttributeValue<string>("lastname");
        string firstnameValue = entity.GetAttributeValue<string>("firstname");
        string midddlenameValue = entity.GetAttributeValue<string>("middlename");
    
        contact.Attributes["new_fullnamesfm"] = lastnameValue + " " + firstnameValue + " " + midddlenameValue;
        contact.Attributes["new_fullnamesmf"] = lastnameValue + " " + midddlenameValue + " " + firstnameValue;
        service.Update(contact);
    }
    


  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Hi,

    You can't retrieve the same record because it is not created yet, use below code for achieve your requirement

    //Plugin in Pre-operation

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

    if(entity.Contains("lastname") && entity.Contains("firstname") && entity.Contains("middlename")){

    entity["new_fullnamesfm"]=entity["lastname"].ToString()+" "+entity["firstname"].ToString() +" "+ entity["middlename"].ToString()

    entity["new_fullnamesmf"]=entity["lastname"].ToString()+" "+entity["middlename"].ToString() +" "+ entity["firstname"].ToString()

    }

    You don't need to create record explicitly in Pre-Operation.

    Thanks

  • Verified answer
    P. R. M Profile Picture
    739 on at

    Hi,

    Register the plug- in on Pre-Operation of create message so that you no need to retrieve and create the contact record explicitly instead just set the custom fields as below:

    protected void ExecutePreContactCreate(LocalPluginContext localContext)

           {

               if (localContext == null)

               {

                   throw new ArgumentNullException("localContext");

               }

               // TODO: Implement your custom Plug-in business logic.

               IPluginExecutionContext context = localContext.PluginExecutionContext;

               IOrganizationService service = localContext.OrganizationService;

               ITracingService tracingService = localContext.TracingService;

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

                if (contact != null)

               {

                   string lastnameValue = contact.Contains("lastname")?contact.GetAttributeValue<string>("lastname"):string.Empty;

                   string firstnameValue = contact.Contains("firstname")?contact.GetAttributeValue<string>("firstname"):string.Empty;

                   string midddlenameValue = contact.Contains("middlename")?contact.GetAttributeValue<string>("middlename"):string.Empty;

                   contact.Attributes["new_fullnamesfm"] = lastnameValue + " " + firstnameValue + " " + midddlenameValue;

                   contact.Attributes["new_fullnamesmf"] = lastnameValue + " " + midddlenameValue + " " + firstnameValue;

               }

           }

    Hope this helps you :)

  • Community Member Profile Picture
    on at

    In addition, I noticed whenever I update the fullname, the new changes do not propagate down to the custom fields because plugin fires only on 'create' message, please I would appreciate if you could help with the code to run on 'update' message on 'fullname' such that whenever I update the fullname, all respective custom fields are updated immediately

    Thanks

  • Aman Kothari Profile Picture
    on at

    Hi,

    You can also use this plugin on Update message , after checking it's MessageName.

    if (context.MessageName == "Create")
    {

    //Create code goes here

    }

    if (context.MessageName == "Update")
    {

    //Update code goes here

    string lastnameValue = contact.Contains("lastname") ? contact.GetAttributeValue<string>("lastname") : string.Empty;

    string firstnameValue = contact.Contains("firstname") ? contact.GetAttributeValue<string>("firstname") : string.Empty;

    string midddlenameValue = contact.Contains("middlename") ? contact.GetAttributeValue<string>("middlename") : string.Empty;

    contact.Attributes["new_fullnamesfm"] = lastnameValue + " " + firstnameValue + " " + midddlenameValue;

    contact.Attributes["new_fullnamesmf"] = lastnameValue + " " + midddlenameValue + " " + firstnameValue;

    }

    Thanks

  • Community Member Profile Picture
    on at

    Thanks very much but I noticed some little issues, for example whenever I update only 'one' or 'two' out of the 'three' fields, only the updated 'one' or 'two' fields appear in custom fields excluding the other non-updated fields, please how can I enforce the plugin to retrieve both the updated and non-updated fields such that it reflects correctly in the custom fields

  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Hi,

    Update plug-in target entity only contains the updated attributes. But sometime we need non updated field in Plugin update message so for that we make a image.

    Images contain snapshots of the primary entity's attributes before (pre) and after (post) the core platform operation. 

    For more info about images please go through below links

    https://community.dynamics.com/crm/b/crminogic/archive/2010/07/26/pre-image-38-post-image-explained-33

    http://crmbook.powerobjects.com/extending-crm/plug-in-development-and-workflow-extensions/plug-ins/plug-in-images-pre-vs-post/

    https://msdn.microsoft.com/en-us/library/hh237515%28v=crm.6%29.aspx?f=255&MSPPError=-2147217396#bkmk_RegImage

    After using Image , your code should be like this

    if (context.MessageName == "Update")
    {
    //Get attributes from plugin image
    Entity contactImage = (Entity)context.PreEntityImages["Your Image Name"];
    //Update code goes here

    string lastnameValue = contact.Contains("lastname") ? contact.GetAttributeValue<string>("lastname") : contactImage.GetAttributeValue<string>("lastname");

    string firstnameValue = contact.Contains("firstname") ? contact.GetAttributeValue<string>("firstname") : contactImage.GetAttributeValue<string>("firstname");

    string midddlenameValue = contact.Contains("middlename") ? contact.GetAttributeValue<string>("middlename") : contactImage.GetAttributeValue<string>("middlename")

    contact.Attributes["new_fullnamesfm"] = lastnameValue + " " + firstnameValue + " " + midddlenameValue;

    contact.Attributes["new_fullnamesmf"] = lastnameValue + " " + midddlenameValue + " " + firstnameValue;

    }

    Hope it'll help you.

    Thanks

    Aman Kothari

    Blog  LinkedIn 

  • Community Member Profile Picture
    on at

    Thanks

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