locked
Update Auto Phone Number Format using plugins RRS feed

  • Question

  • Hi ,

    can anyone help me to solve this issue.

    I am going to write a plugin for phone number filed. i need to change the format of the phone number like (XXX) XXX-XXXX.

    when user change the focus from that textbox. I am writing plugin for this.

    Need Help 


    Divija Vemulapalli

    Monday, April 7, 2014 7:51 AM

Answers

  • Hi , 

    i got this via Plugin Formatting of Phone numbers with Extension for multiple entities and multiple fields

           public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an account or contact.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName == "account" || entity.LogicalName == "contact")
                    {
                        try
                        {
                            if (entity.Attributes.Contains("fieldschemaname"))
                            {
                                string exDemo = entity["fieldschemaname"].ToString();
                                if (exDemo.Length == 10)
                                {
                                    //Update existing values in entity (Account or contact)
                                    entity.Attributes["fieldschemaname"] =string.Format("({0}) {1}-{2}", exDemo .Substring(0, 3), exDemo .Substring(3, 3), exDemo .Substring(6, 4));
                                }
                                else
                                {
                                    if (exDemo.Length > 10)
                                    {
                                        //Update existing values in entity (Account or Contact)
                                        entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2} Extn.{3}", exDemo.Substring(0, 3), exDemo.Substring(3, 3), exDemo.Substring(6, 4), exDemo.Substring(10));
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("Please Enter a Valid Phone Number");
                                    }
                                }
                            }
                            if (entity.Attributes.Contains("fieldschemaname"))
                            {
                                string xDemo = entity["fieldschemaname"].ToString();
                                if (xDemo.Length == 10)
                                {
                                    //Update existing values in entity (Account or Contact)
                                    entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2}", xDemo.Substring(0, 3), xDemo.Substring(3, 3), xDemo.Substring(6, 4));
                                }
                                else
                                {
                                    if (xDemo.Length > 10)
                                    {
                                        //Update existing values in entity (Account or Contact)
                                        entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2} Extn.{3}", xDemo.Substring(0, 3), xDemo.Substring(3, 3), xDemo.Substring(6, 4), xDemo.Substring(10));
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("Please Enter a Valid Phone Number");
                                    }
                                }
                            }
                           }
                        catch (Exception ex)
                        {
                            throw new InvalidPluginExecutionException(ex.Message + ", Stack trace : " + ex.StackTrace);
                        }
                    }


    Divija Vemulapalli


    • Marked as answer by Divija Tuesday, April 8, 2014 9:52 AM
    • Edited by Divija Thursday, April 10, 2014 7:54 AM Fixed Extension Issue
    Tuesday, April 8, 2014 9:51 AM

All replies

  • Hi,

    Why don't simply use JS do this and I hope in SDK there is a sample code for the same.

    Check: http://www.powerobjects.com/blog/2013/12/09/formatting-a-phone-field-in-crm-2013/


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, April 7, 2014 8:52 AM
    Moderator
  • ya we can use JS but my requirement is to do using plugin 

    Divija Vemulapalli

    Monday, April 7, 2014 8:56 AM
  • Hello,

    First there is no direct way of using plugin for this type of requirement, even if you are able to implement some workaround I don't think it will help you here, so better to use JS if you want to validate on onchange, otherwise let your plugin validate it in case of create/update.

    HTH


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, April 7, 2014 9:05 AM
    Moderator
  • Thanks for the help i will try doing this way 

    Divija Vemulapalli

    Monday, April 7, 2014 9:26 AM
  • Hi , 

    i got this via Plugin Formatting of Phone numbers with Extension for multiple entities and multiple fields

           public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an account or contact.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName == "account" || entity.LogicalName == "contact")
                    {
                        try
                        {
                            if (entity.Attributes.Contains("fieldschemaname"))
                            {
                                string exDemo = entity["fieldschemaname"].ToString();
                                if (exDemo.Length == 10)
                                {
                                    //Update existing values in entity (Account or contact)
                                    entity.Attributes["fieldschemaname"] =string.Format("({0}) {1}-{2}", exDemo .Substring(0, 3), exDemo .Substring(3, 3), exDemo .Substring(6, 4));
                                }
                                else
                                {
                                    if (exDemo.Length > 10)
                                    {
                                        //Update existing values in entity (Account or Contact)
                                        entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2} Extn.{3}", exDemo.Substring(0, 3), exDemo.Substring(3, 3), exDemo.Substring(6, 4), exDemo.Substring(10));
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("Please Enter a Valid Phone Number");
                                    }
                                }
                            }
                            if (entity.Attributes.Contains("fieldschemaname"))
                            {
                                string xDemo = entity["fieldschemaname"].ToString();
                                if (xDemo.Length == 10)
                                {
                                    //Update existing values in entity (Account or Contact)
                                    entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2}", xDemo.Substring(0, 3), xDemo.Substring(3, 3), xDemo.Substring(6, 4));
                                }
                                else
                                {
                                    if (xDemo.Length > 10)
                                    {
                                        //Update existing values in entity (Account or Contact)
                                        entity.Attributes["fieldschemaname"] = string.Format("({0}) {1}-{2} Extn.{3}", xDemo.Substring(0, 3), xDemo.Substring(3, 3), xDemo.Substring(6, 4), xDemo.Substring(10));
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("Please Enter a Valid Phone Number");
                                    }
                                }
                            }
                           }
                        catch (Exception ex)
                        {
                            throw new InvalidPluginExecutionException(ex.Message + ", Stack trace : " + ex.StackTrace);
                        }
                    }


    Divija Vemulapalli


    • Marked as answer by Divija Tuesday, April 8, 2014 9:52 AM
    • Edited by Divija Thursday, April 10, 2014 7:54 AM Fixed Extension Issue
    Tuesday, April 8, 2014 9:51 AM