I'm new in CRM. I need just on the OnLoad event of the page, show a JavaScript alert message: "Wele 'Account Name'". This is my simple code:
function weleAlert()
{
var userName = Xrm.Page.getAttribute("name").getValue();
if(userName !== null)
{
alert("Wele " + userName + "!");
}
}
But I'm getting error message onLoad: TypeError: Cannot read property 'getValue' of null at weleAlert.
If my code looking like code below everything works fine.
function weleAlert()
{
alert("Wele ");
}
Somebody can help? Maybe the attribute name is not ok. But I don't know how to check it.
I'm new in CRM. I need just on the OnLoad event of the page, show a JavaScript alert message: "Wele 'Account Name'". This is my simple code:
function weleAlert()
{
var userName = Xrm.Page.getAttribute("name").getValue();
if(userName !== null)
{
alert("Wele " + userName + "!");
}
}
But I'm getting error message onLoad: TypeError: Cannot read property 'getValue' of null at weleAlert.
If my code looking like code below everything works fine.
function weleAlert()
{
alert("Wele ");
}
Somebody can help? Maybe the attribute name is not ok. But I don't know how to check it.
Share Improve this question asked Feb 21, 2019 at 20:46 Iliya GaponovIliya Gaponov 391 gold badge3 silver badges7 bronze badges 3- So getAttribute('name') is obviously not returning anything. Since I don't know what Xrm or Page are I can't help much more specifically. Open the debugger and put a break point at the var username line and hover over the objects to see what attributes they have. That may help. – Jason Pelletier Commented Feb 21, 2019 at 20:50
- Possible duplicate of Xrm.Page.getAttribute("").getValue() don't get actual value – Bonjov Commented Feb 21, 2019 at 20:50
- Does the "name" field exist in your Page? – swagrov Commented Feb 21, 2019 at 21:04
3 Answers
Reset to default 1- Verify the attribute
name
to make sure its in the form - If the field is a custom attribute then it will be having publisher name prefix ex.
new_name
- If the field is added in header/footer sections or BPF stages then it will be renamed like
header_name
- Check if it’s hidden or added multiple times in the form & use browser developer toolbar to inspect the DOM
- You can validate like
if(formContext.getAttribute("name") != null
before accessinggetValue()
Depending on your version of CRM, Xrm.Page
may be deprecated. Refer to deprecation page
The correct way of doing this is to use the executionContext
object which is a parameter that can be passed to your methods by CRM. Microsoft provides an an example of how to do that here but the steps are:
- Update your method to include a new parameter: e.g.
function weleAlert(executionContext)
- Use
executionContext
to retrieve theformContext
: e.g.var formContext = executionContext.getFormContext()
- Use
formContext
as a substitute forXrm.Page
: e.g.var userName = formContext.getAttribute("name").getValue();
- When you register this function in CRM, you need to be sure to check the Pass execution context as first parameter option otherwise CRM will not pass the executionContext to your function
Putting it all together, your method should look like this:
function weleAlert(executionContext)
{
var formContext = executionContext.getFormContext();
var userName = formContext.getAttribute("name").getValue();
if(userName !== null)
{
alert("Wele " + userName + "!");
}
}
I solved my problem!
I read this on docs.microsoft, so I thought that the attribute for Account Name is "name". var nameValue = Xrm.Page.getAttribute("name").getValue();
Assigns the value of the Account Name field to the nameValue variable.
The right attribute for Account Name is "parentcustomerid". This code works for me.
function weleAlert()
{
var userName = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].name;
if(userName !== null)
{
alert("Wele " + userName + "!");
}
}
Thank you all for response.