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 from the technet really shows how Microsoft have split the functionality into logical sections, The picture from Technet shows the logical groups of functionality
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/
Here are some common Javascript functions you will need
Get the value from a field
Xrm.Page.getAttribute(“hosk_field”).getValue() ;
Set the value on a field
Xrm.Page.getAttribute(“hosk_field”).setValue(“new value”) ;
Setting requirement level for a field
Xrm.Page.getAttribute(“hosk_field”).setRequiredLevel(“none”);
There are three requirement levels “none”, “required”, “recommended”. Required means it must be filled in (red asterisk), recommended is the blue cross and none is obvious. It’s useful to be able to change the requirement settings dynamically because you can then make users fill in fields after they changed another field or make fields not required.
Hide/show tab
Xrm.Page.ui.tabs.get(“hosk_tab1”).sections.get(“Permanent”).setVisible(false);
hide = setVisable(false)
show = setVisable(true)
You can hide and show a section as well
Some interesting Javascript functions from this blog tidbits at this blogs:
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 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
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
LikeLike
updated, thanks for the comment. Syntax changes can be very frustrating
LikeLike
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?????
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
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
LikeLike
Thank you very much! I’ll try
LikeLike
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);
LikeLike
could help me with the code to hide and display sections?
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
// 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.
LikeLike
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().
LikeLike
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
LikeLike
Thanks for hint!
The issue was related to wrong type of variable… so simple:)
LikeLike
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?
LikeLike
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
LikeLike
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?
LikeLike
Excellent blog entry. The Javascript changes are great once you get used to them
LikeLike
How do I use the function uppercase () in crm11? Where can I find a library with functions for javascript crm11?
LikeLike
You should download the javascript cheat sheet which you can find here
LikeLike
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??
LikeLike
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.
LikeLike
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.
LikeLike
How can I dynamically add a checkbox to the ui.controls collection?
LikeLike
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”
LikeLike
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
LikeLike
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
LikeLike
Hi Could anyone tell how i debug the JavaScript for CRM 2011 and whether any tools are available for debugging JavaScript ?
Thanks 🙂
LikeLike
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
LikeLike
Thanks MR Hosk !!
LikeLike
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.
LikeLike
why don’t you try getting an attribute rather than a control
Xrm.Page.getAttribute(“name”).getValue(‘Hosk Name’);
LikeLike
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
LikeLike
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.
LikeLike
Okay. Im facing a error in script INVALID Character in the line, character 36 near “new”
var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue().
LikeLike
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.
LikeLike
if you want to just set a field value then just set the field value
Xrm.Page.getAttribute(“mycheckbox”).setValue(false);
LikeLike
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.
LikeLike
Hello Hosk!
I’m looking for SOAP/fetchxml code (CRM 2011) that do UPGRADE to specify field.
Thank you, advance!
LikeLike
Hello Hosk!
I’m looking for SOAP/fetchxml code (CRM 2011) that does UPGRADE to specific field.
Thank you, advance!
(fixed version SORRY!)
LikeLike
If you are trying to focus a SubGrid, please see my article here: http://prodynamicscrm.com/articles/using-javascript-set-focus-subgrid-control
LikeLike
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
LikeLike
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
LikeLike
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!
LikeLike
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’);
LikeLike
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??
LikeLike
Reblogged this on Shahzad Sarang gloB's.
LikeLike
How Can I map field for a custom field in order to transfer data from one source entity attribute to the target entity attribute in CRM.
LikeLike
By default i kept all tabs in collapse state. After page load i need to expand them, and i need the syntax code for all tabs, for not place in each and every tab. I kept for loop and looped it, but i need the count of all tabs by default. and if tabs are increased or decreased the code must not be changed. So please tell me how to get the count of tabs in a form.
LikeLike
Can you please tell me how can i align my fields of a tab on index change i want to hide or show some fields on form i done it but they are not properly allign…
LikeLike
My brother suggested I would possibly like this
web site. He was once totally right. This put up actually made my day.
You cann’t imagine just how much time I had spent for this information!
Thank you!
LikeLike
It took me a long time to figure out that which you were talking about.
LikeLike
hi! sir
What about if we want 2 fields hide on the selection of the picklist(option set) purticular value .
LikeLike
Can anyone tell me how to get the id of a primary entity in related entity. For eg how to get order ID in order Product ID.
LikeLike
How to set value to a field located in footer of the form……?
LikeLike
You can change the value like you would changing any other field. The only problem is the header and the footer won’t display the changed value until the form is refreshed
LikeLike
Hello Hosk,
Is there a way to auto populate fields when there is one lookup field in one entity and the second entity has a text field.
For Example: I have two entities A and B. Entity B has a lookup field on Entity A, and if the lookup field matches the field in Entity A then it should auto populate the rest of the fields in Entity B such as name, address etc..
LikeLike