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)

Capitalize first letters of contact first name and last name

(0) ShareShare
ReportReport
Posted on by

Hi I'm trying to capitalize 1st and last name letters for the existing contacts using c# and for the new contacts javascript. 

I need a help in fixing them please. 

Thanks 

c#:
private void button1_Click(object sender, EventArgs e)
        {
            _service = GetService();

            var Criteria = new FilterExpression();
            _log.Info("Capitalize function has started");
            QueryExpression query = new QueryExpression("contact");
            query.EntityName = "contact";
            query.ColumnSet = new ColumnSet();
            query.ColumnSet.AddColumns("firstname", "lastname", "contactid");

            FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.Or);
            childFilter.AddCondition("firstname", ConditionOperator.NotNull);
            childFilter.AddCondition("lastname", ConditionOperator.NotNull);

            EntityCollection results = _service.RetrieveMultiple(query);

            foreach (var item in results.Entities)
            {
                var contactid = item.GetAttributeValue<Guid>("contactid");

                ColumnSet cols = new ColumnSet(new[] {"firstname", "lastname"});

                Entity contact = _service.Retrieve("contact", contactid, cols);

                if (item.GetAttributeValue<string>("firstname") != null)
                {
                    {
                        item["firstname"]= ToTitleCase(item.GetAttributeValue<string>("firstname"));

                    }

                    if (item.GetAttributeValue<string>("lastname") != null)
                    {
                       // item["lastname"] = item.GetAttributeValue<string>("lastname").ToUpper();

                        item["lastname"] = ToTitleCase(item.GetAttributeValue<string>("lastname"));

                    }

                    _service.Update(contact);
                }
            }

        }
public string ToTitleCase(string str)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
JavaScript:

//================Start: CapitalLetter=================================================
    function CapitalizeFirstLetters(FieldName)
          {

            var firstnamelowercase = Xrm.Page.getAttribute("firstname").getValue();
            
            if(firstnamelowercase!= null)
            
            { var firstName = firstToUpperCase(firstnamelowercase); 
            
            Xrm.Page.getAttribute("firstname").setValue(firstName); }


            var lastnamelowercase  = Xrm.Page.getAttribute("lastname").getValue(); 

            if(lastnamelowercase!= null)
            
            { var firstName = firstToUpperCase(lastnamelowercase); 
            
            Xrm.Page.getAttribute("lastname").setValue(lastname); }
             
          }
//====================End:CapitalLetter=================================================

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: Capitalize first letters of contact first name and last name

    You can use the following function to capitalize the first letter of a string:

    function firstToUpperCase( str ) {

       return str.substr(0, 1).toUpperCase() + str.substr(1);

    }

    You can then call it:

    var firstnamelowercase = Xrm.Page.getAttribute("firstname").getValue();

    var firstName = firstToUpperCase(firstnamelowercase);

    Xrm.Page.getAttribute("firstname").setValue(firstName);

    In your code you have some mismatch in the attribute Names, so try and follow logic above.

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

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans