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)

Reformatting text to proper case structure

(0) ShareShare
ReportReport
Posted on by

I have several field values that are in all caps and I want a function to change them to proper case structure. I found this:

function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}


but am not really sure what I need to change to get it to work, or if it is even workable. I've messed with this code as well:

function changeCase() {

    var attribute = Xrm.Page.getAttribute("pps_lastname");  

    if (attribute != null)

    var str = attribute.getValue();
    var pieces = str.split(" ");
    
    for (var i = 0; i < pieces.length; i++) {
        var j = pieces[i].charAt(0).toUpperCase();
        var i =
            pieces[i] = j + pieces[i].substr(1);
    }
    Xrm.Page.getAttribute("pps_lastname").setValue(pieces.join(' '));
}


But it only capitalizes the first letter, so if it's all caps, it's really of no help. As you might guess from the above, the field I'm trying to change is "pps_lastname".

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Hi,

    It looks like you just need to change the code to add the rest of the value in lowercase:

    for (var i = 0; i < pieces.length; i++) {

           var j = pieces[i].charAt(0).toUpperCase();

           pieces[i] = j + pieces[i].substr(1).toLowerCase();

       }

    Hope this helps,

    Scott

  • Community Member Profile Picture
    on at

    Gotcha! Works now! Not to be difficult, but is there a way to have it pass the value of the field I'm in as oppose to the field being set in the script so that I can use it on different fields in different entities?

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    For sure!

    If you mark your event has passing the event arguments, you can add a parameter to your onchange function and then use:

    function changeCase(executionContext)

    {

    var attribute = executionContext.getEventSource()

  • Community Member Profile Picture
    on at

    OK, I changed the code to match yours:

    function changeCase(executionContext) {
        var attribute = executionContext.getEventSource();
        
    
        if (attribute != null) 
            
        var str = attribute.getValue();
        var pieces = str.split(" ");
        
        for (var i = 0; i < pieces.length; i++) {
           var j = pieces[i].charAt(0).toUpperCase();
           pieces[i] = j + pieces[i].substr(1).toLowerCase();
       }
    
        Xrm.Page.getAttribute("pps_testlastname").setValue(pieces.join(' '));
    }

    added "pps_aosp_city" to the parameters under "Details" in my handler, have "Pass execution context as first parameter" unticked and I get the following error "Error: Object doesn't support property or method 'getEventSource'"

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Did you mean that you have that option ticked - not unticked? It should be ticked.

  • Community Member Profile Picture
    on at

    OK, I went back and ticked the option and get the following error:

    Error: Unable to get property 'setValue' of undefined or null reference

    Does the last line in the code:

    Xrm.Page.getAttribute("pps_testlastname").setValue(pieces.join(' '));

    need to be modified/removed?

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    You'll need to change it to:

    attribute.setValue(pieces.join(' '));

  • Community Member Profile Picture
    on at

    That did it! Thanks SO much!!!

  • Community Member Profile Picture
    on at

    Hello Nick,

    I am a new System Administrator for a company in San Francisco.  We have 3 fields that come into our CRM from a Foxpro application and they always come in as capital letters.   I see that the solution you and Scott created worked for you however I am not clear on how to implement it.    It looks like I would copy the code and create a web resource with the Java Script you provided.   Once that is done how do I call the script to update the Account Form in the CRM, and how do I change the field that the script works on?    I would greatly appreciate any guidance you can provide.

    Thank you very much!

    Gene

    genes@aaasolutions.com

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