I have been looking at the CRM 2011 Javascript SDK, samples. Comparing it to CRM 4 code and getting very angry. It just wasn’t the same and I wasn’t understanding the SDK, it was written in an odd way and the examples were just much more complex than I needed to get started. I have also written a good blog entry about the comparison between Javascript CRM 2011 and CRM 4
I could hide sections and tabs ok, using the Page ui but setting variables was really starting to bug me. I could get it too work using crmForm but I wanted to move on and use the new code.
I think the problem is highlighted looking at the new Javascript layout
The UI bit makes sense but looking at this I couldn’t see how to get the actual fields!!!
there is also a big difference with the actual Javascript you write everything has to get something
Xrm.Page.getAttribute(“hosk_display2″)
Xrm.Page.ui.tabs.get(“hosk_tab1″)
once you get the variable you can then getValue or setValue
Xrm.Page.getAttribute(“hosk_display2″).setValue(“HOSK SET THIS CRM 2011 STYLE”);
Xrm.Page.ui.tabs.get(“hosk_tab1″).sections.get(“Permanent”).setVisible(false);
JavaScript CRM 4 to CRM 2011 converter tool:
http://crm2011scriptconvert.codeplex.com/
and looking at these tidbits at this blog
Here are common tidbits:
Get the value from a CRM field
var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ;
Set the value of a CRM field
Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);
Hide/Show a tab/section
Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true);
Call the onchange event of a field
Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();
Get the selected value of picklist
Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;
Set the requirement level
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”);Set the focus to a field
Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);
Stop an on save event
event.returnValue = false;Return array of strings of users security role GUIDs:
Xrm.Page.context.getUserRoles()
god bless the boys from Power Objects



Jon
March 16, 2011
Hi,
You might want to update your post to correct the casing here:
Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true);
Cheers
Hosk
March 16, 2011
updated, thanks for the comment. Syntax changes can be very frustrating
Val
April 13, 2011
Can anyone tell me how to setValue to a variable?
I’m trying to create Name as the person’s full name. I don’t know how to get lookupFirst + ‘ ‘ + lookupLast into the “name” attribute.
Here is what I have so far.
var lookupLast = Xrm.Page.getAttribute(“new_lastname”).getValue();
var lookupFirst = Xrm.Page.getAttribute(“new_firstname”).getValue();
Xrm.Page.getAttribute(“name”).SetValue?????
Hosk
April 13, 2011
there is an example below
Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);
but for your example
Xrm.Page.getAttribute(“name”).setValue(‘Hosk Name’);
Xrm.Page.getAttribute(“name”).setValue(lookupFirst + ‘ ‘ + lookupLast);
my advice would be press F12 and debug the javascript, this will show you if the syntax is correct and tell if you anything is failing with a syntax error
Alex
May 16, 2011
Many thanks for sharing this Hosk. In crm 4.0 you could change the display formatting of attributes e.g.
crmForm.all.new_copyaddress1button.style.textAlign = “center”;
crmForm.all.new_copyaddress1button.vAlign = “middle”;
crmForm.all.new_copyaddress1button.attachEvent(“onmousedown”, color1);
crmForm.all.new_copyaddress1button.attachEvent(“onmouseup”, color2);
like the example at http://crmstuff.blogspot.com/2008/02/create-button-on-your-form.html
Any idea how to do this in Crm 2011? Thanks in advance.
Houseedition
May 19, 2011
Hi Hosk,
Do you know how can I get the records in a subgrid?
Xrm.Page.ui.controls.get(‘subgrid) return null
I hope somebody can help me.
Hosk
May 19, 2011
I haven’t really done this myself yet but I did find a couple of interesting forum posts. You can see the sample code in this blog post
http://crmbusiness.wordpress.com/2011/05/19/crm-2011-javascript-and-subgrids-code-example/
Houseedition
May 19, 2011
Thank you very much! I’ll try
swazimodo
May 19, 2011
can you show and hide controls the same way you did with the tabs?
Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true);
Xrm.Page.getAttribute(fieldName).setVisible(false);
Xrm.Page.getAttribute(fieldName).setVisible(true);
mlp
July 19, 2011
could help me with the code to hide and display sections?
Bhanu Bharadwaj
June 1, 2011
Hi Every one,
I m new in CRM World, Can u Pls tell me that where can we write code in java script for a field
in CRM 2011.
Hosk
June 1, 2011
you need to add a jscript file to the web resources folder and then add that javascript file to the entity when editing the entity form
Jim Daly
June 6, 2011
I’m pretty sure you didn’t mean to actually add a file to the web resources folder. You should add a Jscript library as a JScript Web resource and then include that library in the form.
Also, be sure to check out the CRM SDK solution to get IntelliSense for JScript form libraries. This is summarized at JScript IntelliSense for Form Script Libraries.
This project provides IntelliSense and in-line documentation that should help you understand how to write scripts using Xrm.Page.
Batman
August 22, 2011
// If I have a control ‘myCTL’ that is null, I want to hide the section it’s in.
// (probably zero business value, but the example works)
var myCTL = Xrm.Page.getControl(“myCTL”);
var myATTR = myCTL.getAttribute();
var myAttrVal = myATTR.getValue();
if(myAttrVal==null)
{
myCTL.setVisible(false); //<— sets the control visibility to false
myCTL.getParent().setVisible(false); //<— sets the visibility of the control's PARENT (e.g., the section) to false
}
//hope this helps.
Martin
September 1, 2011
I try to get value from a field but everytime I get this error:
“Unable to get value of the property ‘getValue’: object is null or undefined”.
I am using Xrm.Page.getAttribute(“myfield”).getValue().
Hosk
September 1, 2011
You have to remember to use the actual name of the variable e.g. new_testvariable and not the display name.
here is an example
Xrm.Page.getAttribute(“meta_contacttype”).getValue();
Also has the field actually got a value, you should check to see if the variable is null before getting the value
one piece of advice is to press F12 on the form and then debug the script. you can then step through the code
if you assign it too a variable
var hosk = Xrm.Page.getAttribute(“meta_contacttype”).getValue();
you can then view what the variable is in the local variables display.
The only other thing I can think of if it’s maybe a type of variable where you wouldn’t use a getValue and you should look up the javascript cheat sheet
http://danielcai.blogspot.com/2011/04/microsoft-dynamics-crm-2011-javascript.html
Martin
September 2, 2011
Thanks for hint!
The issue was related to wrong type of variable… so simple:)
Bryce
September 12, 2011
I am having the same problem getting the value from a field in the header of a form. I have looked at the cheat sheet and even spit out all the attributes on the page to see if I have the name wrong but it doesn’t show up there either. Is there something special I have to do to get header values?
Hosk
September 12, 2011
I think the header is different from the form variables.
I think the header is set before the rest of the form variables.
I’m not sure how you set it, I think you will have to do some more googling to find that answer
D
September 15, 2011
I have seen the same blogpost running around on the crm circle about attaching a function to the onRefresh event of a Form sub-grid. but can the records from one Sub-grid(only related records) be fetched and displayed on another entities subgrid?
Metaphorix
September 21, 2011
Excellent blog entry. The Javascript changes are great once you get used to them
Saulo Ramon
September 30, 2011
How do I use the function uppercase () in crm11? Where can I find a library with functions for javascript crm11?
Hosk
September 30, 2011
You should download the javascript cheat sheet which you can find here
http://crmbusiness.wordpress.com/2011/04/11/crm-2011-javascript-cheat-sheet/
Saulo Ramon
October 3, 2011
I found lots of interesting things but nothing about uppercase (). This was the function that used in CRM 4.0 in the subject field:
var = oField event.srcElement;
oField.value oField.value.toUpperCase = ();
How do I do this in CRM 11??
Saulo Ramon
October 5, 2011
I managed to apply a function that resolved several outstanding issues that had in my customizations. Created a JScript library containing the function below:
var oField = event.srcElement;
oField.value = oField.value.toUpperCase();
Hook up the OnChange Event to the JScript Library Function.
eve
October 31, 2011
hi Saulo Ramon,
I tried this but eventually when I save the form, the uppercase is not saved into db, the page when reload returns to small case.
peter
October 5, 2011
How can I dynamically add a checkbox to the ui.controls collection?
Sandy
December 8, 2011
hi ,
kindly i need to know how to create a unique field that never has duplicate values in CRM 2011.
I’ve found the below answer on the net, can anyone give me the right code please.
“Develop validation plugin which will handle Create message in pre-mode and validate that those unique field is really unique”
Hosk
December 8, 2011
I’m not sure a page about Javascript basics is the right place for your question, you should try the CRM forums.
You could also try one of the autonumber projects on codeplex
http://crm2011autonumber.codeplex.com/
http://pinpoint.microsoft.com/en-gb/applications/auto-number-for-dynamics-crm-2011-12884915634
they will count up and so won’t put the same value in
Sandy
December 11, 2011
Thanks alot for your fast reply Hosk, my problem is that i dont need an autonumbering solution … i already have serial numbers on products , i just need to use them once in the system without being duplicated .. so i needed a javascript code to check the serial number before saving .
But realllly thankss alot for your help. and i’ll try searching more in CRM forums
athulmt
February 9, 2012
Hi Could anyone tell how i debug the JavaScript for CRM 2011 and whether any tools are available for debugging JavaScript ?
Thanks
Hosk
February 9, 2012
press F12 to get up the debugger.
go to the script tab
press start debugging
and then find the script you want to debug and put in a breakpoint
http://crmbusiness.wordpress.com/2011/02/18/crm-2011-how-to-debug-javascript-with-ie8/
athulmt
August 16, 2012
Thanks MR Hosk !!
Anni
February 22, 2012
Hi all.
I try to get value from a field, i try using below code but its not getting value of text field.
var phone = Xrm.Page.ui.controls.get(“TestMobile”).getvalue();
Here TestMobile is display name of the field.
Hosk
February 22, 2012
why don’t you try getting an attribute rather than a control
Xrm.Page.getAttribute(“name”).getValue(‘Hosk Name’);
Anni
February 22, 2012
Thanks for your response Hosk.. Im new bud in CRM.Actually as the input is given i need to change the field value. here is Javascript function is called by onchange Event
function validatePhone()
{
var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue();
{
phone.value=phone.value.substr(0, 3) + “-” + phone.value.substr(3,10);
}
}
new_testmobile is name of the field.Any suggestion or correct me if im wrong
Hosk
February 22, 2012
is the var phone variable set with a value, does the field have a value in it?
I don’t think you need to put phone.value, just phone would do.
why have you got extra brackets in there?
I would press F12 on the form and start debugging the javascript by putting a breakpoint on the line
var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue()
start with the basics, first try and assign the field value to a variable.
Also look at the CRM SDK for examples.
Anni
February 22, 2012
Okay. Im facing a error in script INVALID Character in the line, character 36 near “new”
var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue().
Edson
February 23, 2012
I uncheck the check of a checkbox that is inside a tab sections, I have something like this:
var tabGeneral=Xrm.Page.ui.tabs.get(“tab1″);
var seccionCheck=tabGeneral.sections.get(“section1″);
seccionCheck.controls.get(“mycheckbox”).setValue(false);
Xrm.Page.getAttribute(“mycheckbox”).setSubmitMode(“always”);
but i can’t. appreciate your comments.
Hosk
February 24, 2012
if you want to just set a field value then just set the field value
Xrm.Page.getAttribute(“mycheckbox”).setValue(false);
Edson
February 28, 2012
I can’t because my checkbox is in the section, for this i access to seccionCheck.controls.get, do you understand?
PD : im sorry, i don’t speak english so much.
Helecho
March 6, 2012
Hello Hosk!
I’m looking for SOAP/fetchxml code (CRM 2011) that do UPGRADE to specify field.
Thank you, advance!
Helecho
March 6, 2012
Hello Hosk!
I’m looking for SOAP/fetchxml code (CRM 2011) that does UPGRADE to specific field.
Thank you, advance!
(fixed version SORRY!)
Ryan Corrigal
March 9, 2012
If you are trying to focus a SubGrid, please see my article here: http://prodynamicscrm.com/articles/using-javascript-set-focus-subgrid-control
kamal
April 2, 2012
Bonjours,,
s’il vous plait je veux un peu d’aide sur comment intégrer bing maps dans crm2011 avec l’affichage d’un point sur la carte avec l’attitude et longitude dans le même temps faire dessiner un polygone en reliant ces points entre eux merci pour votre aide
kamal
April 3, 2012
Hi,,
Please I want some help on how to integrate bing in crm2011 maps with display of a point on the map with longitude and attitude at the same time to draw a polygon by connecting the dots between them thank you for your help
iambigred
April 26, 2012
Loads of the segments of JavaScript you have posted are incorrect! Your blogging platform (WordPress?) has changed any quotes to left and right angled quotes, which are invalid in JavaScript!
Anonymous
May 5, 2012
Hi i am new to crm 2011.
i have a problem. i have set a field value default during on load . but i want to change the value dynamic during ON SAVE event, can u send me java script code for it.
this is my java script
Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);
rama
September 21, 2012
How to get label of radio button options in javascript in DCRM 2011 and how to change the labels onload evrytime?? ex;- if i have created an optionset with 2 options yes,no. and i want yes label to be changed to some number onload of this form??
mshahzadg
November 25, 2012
Reblogged this on Shahzad Sarang gloB's.