Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Capitalize first letters of contact first name and last name

Posted on by Microsoft Employee

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

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 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

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans