3

I am trying to learn JavaScript. First day! I want to concatenate the First and Last names. This is what I have so far. It works fine, but if I don't enter one of the fields, it just shows up as null. How do I avoid that? Where would I put that clause in the following code? Thanks for your help in advance!

function ShowFullName()
{
    var varFirstName = Xrm.Page.getAttribute("lauren_firstname").getValue();
    var varLastName = Xrm.Page.getAttribute("lauren_lastname").getValue();
    Xrm.Page.getAttribute("lauren_name").setValue(varFirstName + " "  + varLastName);
};
4
  • 4
    Part of learning JScript (or anything) will be learning to use the documentation. Of course, you're welcome to ask questions here, but be aware that you are expected to show research effort. Commented Oct 24, 2012 at 19:44
  • By the way, what library are you using? Or, is "JScript" actually not a spelling error? Commented Oct 24, 2012 at 19:47
  • Any recommendations for a new person like me on where to start. I've been thrust into this world, and I'm reading online and trying to understand using JavaScript as best that I can. I need like a beginner's guide. Any suggestions? I currently am trying to use it within Microsoft Dynamics CRM 2011. Commented Oct 24, 2012 at 20:07
  • @Lauren: Google "Douglas Crockford" and watch his lectures on YouTube. Commented Oct 24, 2012 at 20:33

3 Answers 3

3

You can take advantage of the fact that all variables are truthy or falsy in JavaScript. In other words, every variable can be coerced (converted) into true or false.

null is falsy, and strings with content are truthy. That means:

var a = null;
var b = 'hello';

if (a) {
    // does not run
} else {
    // runs
}

if (b) {
    // runs
}

You can use other kinds of expressions too, like a ternary expression or the logical OR operator:

alert(a ? 'yes' : 'no'); // => no
alert(b ? 'yes' : 'no'); // => yes

alert(a || b); // => hello

In this case, logical OR is our most interesting choice. When you use the operator, the following happens:

  1. If the left side evaluates truthy, return that value.
  2. Otherwise, return the value that the right side evalutes to.

In the above example, a is null, so it is falsy. The expression returns the right side, the value of b. So we can take advantage of this behavior by writing varFirstName || '', which will either return the first name (if there is one), or if it's null, the right side gives us an empty string.

function ShowFullName() {
    var varFirstName = Xrm.Page.getAttribute("lauren_firstname").getValue();
    var varLastName = Xrm.Page.getAttribute("lauren_lastname").getValue();

    Xrm.Page.getAttribute("lauren_name").setValue(((varFirstName || '') + " " + (varLastName || '')).trim());
}

I added a call to trim to remove extra spaces from the result.

Sign up to request clarification or add additional context in comments.

Comments

1

You can check if something is null like:

if (obj == null) { 
  // do this 

}

so something like

if (varFirstName == null) { varFirstname = "";}

This will set firstName to an empty string

6 Comments

Hi Darren, I added what you suggested right below my two var statements. I added this: if (varFirstName == null) { varFirstname = "";} if (varLastName == null) { varLastname = "";}
Sorry, i hit enter too fast. I typed in First Name, Lauren, and then it still says "null" so in my full name box it says Lauren Null......What am i doing wrong?
Is it returning Null as in capital letter N ? If so, then it looks like a string "Null" is being returned as a null object is spelt all lowercase. Use this instead: if (varLastName == null || varLastName == "Null") { varLastName = "";} - the || means 'Or'
You could also try checking for undefined. Use: if (varLastName is undefined){varLastName = "";}
Darren, thank you for youe help. I took Jeff's advice above and that worked just fine, and now I see you suggested the same thing. THank you so much for your help I really appreciate it.
|
1

try this:

function ShowFullName()
    var varFirstName = Xrm.Page.getAttribute("lauren_firstname").getValue();
    var varLastName = Xrm.Page.getAttribute("lauren_lastname").getValue();

    if(varFirstName == null || varFirstName.ToLower() == "null") { 
        varFirstName = "";
    }

    if(varLastName == null || varLastName.ToLower() == "null") { 
        varLastName = "";
    }

    Xrm.Page.getAttribute("lauren_name").setValue(varFirstName + " "  + varLastName);
};

4 Comments

Jeff! Thank you so much for your help I really appreicate it. This worked great! Any recommendations for a new person like me on where to start. I've been thrust into this world, and I'm reading online and trying to understand as best that I can. I need like a beginner's guide. Any suggestions?
Sorry @Lauren I don't know much about JScript, it just happened that i know Xrm.Page.getAttribute will provides direct access to the Xrm.Page.data.entity.attributes.get method.
Are the .ToLower() bits really necessary? If it's not null, could it become null after .ToLower()? (I know nothing about xrm, but I do know js...)
yes it is necessary because it will return String 'Null'. and just to make it more safe, that is why I added .ToLower()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.