In this webinar, our experts showcase a variety of demo use cases of how different components of the...
One of the noticeable changes between CRM 4.0 and CRM 2011 is the JavaScript object model. It has changed a bit. Below are some of the commonly used functions used to manipulate CRM forms. Let us know if you have a commonly used function that should be included in the list of CRM 2011 JavaScript tidbits below.
One of the tools we highly recommend is the JavaScript CRM 4 to CRM 2011 converter tool:
http://crm2011scriptconvert.codeplex.com/
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()
Hide/Show Tabs and Sections:
function setVisibleTabSection(tabname, sectionname, show) { var tab = Xrm.Page.ui.tabs.get(tabname); if (tab != null) { if (sectionname == null) tab.setVisible(show); else { var section = tab.sections.get(sectionname); if (section != null) { section.setVisible(show); if (show) tab.setVisible(show); } } } }
Xrm.Page.ui.tabs.get(5).SetVisible(false); doesn't work
Xrm.Page.ui.tabs.get(5).setVisible(false); it's working
it will work ....
crmonce.com
It will not going to work since java script is case-sensitive..!
// Hide or Show field based on selected option
function fnc_Hide_Show_Field()
{
var Option = Xrm.Page.getAttribute("CRMFieldSchemaName").getSelectedOption().text;
if (Option == "Option_xyz")
//example: based on the option "Individual" show field relevant to Individual
//or based on the option "Team" show field relevant to Team.
{
Xrm.Page.getControl("VisibleField_CRMFieldSchemaName").setVisible(true);
Xrm.Page.getControl("HiddenField_CRMFieldSchemaName").setVisible(false);
}
}
//Happy CRM 2011
you legends. I have been looking at the CRM 2011 Javascript SDK, samples. Comparing it to CRM 4 code and getting very angry.
I could hide stuff ok 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.
Finally I found you blog, awesome.
Also how can you subscribe to the blog on an RSS feed?
How do you hide a section? I have a section called Vision but I am unable to hide it. I can hide a tab with Xrm.Page.ui.tabs.get(tabnumber).setVisible(visible); but I cannot hide a section using this same syntax. Can anyone help?
Hi Mike,
Here's the function you need:
function setVisibleTabSection(tabname, sectionname, show) {
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null) {
if (sectionname == null)
tab.setVisible(show);
else {
var section = tab.sections.get(sectionname);
if (section != null) {
section.setVisible(show);
if (show)
tab.setVisible(show);
}
}
}
}
you can hide the section in one line through this
Xrm.Page.ui.tabs.get("tab_name").sections.get("section_name").setVisible(false);
You guys are awesome! Thank you so much.
I've been trying to hide a navigation submenu which gets renamed with
document.getElementById('_NA_MA')
However I've been unable to hide it completely.
The function being used to rename is as follows in case it helps anyone:
/** Renames Nearside Navigation Pane options. **/
cmnLeftNavBarRename = function(navBarEntry, newName) {
if (navBarEntry != null) {
var navBarEntryName=navBarEntry.getElementsByTagName("NOBR");
navBarEntryName[0].innerHTML = newName;
}
}
and it is called by
cmnLeftNavBarRename(document.getElementById('_NA_MA'),'My New Name');
I just found something that works for me, but now am wondering if there is a better way.
What I found is document.getElementById('_NA_MA').style.display = “none”;
Hi Guys,
This may seem like a really simple issue, but I'm struggling to use any of the above examples to grab the text value of a "Single Line of Text" field.
Basically, i need to grab the text value of "Topic" in an opportunity - then add the text value of "Potential Customer" to the end of it...
Tried to use...
var topicText = Xrm.Page.getAttribute(“Name”).getValue();
...to set my variable, but I get errors saying the system doesn;t understand the "getValue" part.
If anyone can point me in the right direction that'd be great.
Thanks
Tom
var originalValue = crmForm.all.contosa_name.defaultValue;
what is the equivalent value in CRM 2011
Try my CRM 4 to CRM 2011 JavaScript Converter tool on Codeplex, it might help you. http://bingsoft.wordpress.com/2010/09/09/crm-4-to-crm-2011-javascript-converter-tool/
Cheers,
Rhett
hi,
any one knows the equivalent of this in 2011
$("span.ms-crm-Form-Title").append(tittle);
How would you set a property on one form based on a related record field value. For example: Make a field Read-Only on the "Quote Product" from based on the value of the isSubmitted field on the related "Quote" record. Note: I cannot use "parent" to enable this as it goes through a view in between.
I'm having trouble getting a Telephone number field within the Case Entity to pull through the Telephone number from the Contact Entity based on the contact that has been selected.
Does anyone have any ideas?
Hi, i've 2 forms in an entity, and i want the form to auto change to another base on the value in one field. I've tried Xrm.Page.ui.formSelector.items.get(1).navigate(); , but the ribbon is diabled after the form load to another. Any idea?
how to hide attribute on particular date in crm 2011
Hi,
How to freeze "lookup for" picklist of lookup record window. I don't want to show other entities in that picklist.
Hi,
i am trying to hide some fields based on a picklist value. I get the following error
Xrm.Page.getAttribute(...) is null or not an object
i have added the complete script below for your reference
function form_onchange()
{
var facility = Xrm.Page.getControl("new_Facility");
var volume = Xrm.Page.getControl("new_Volume");
var rentalarea = Xrm.Page.getControl("new_RentalArea");
var areaunit = Xrm.Page.getControl("new_AreaUnit");
var commitment = Xrm.Page.getControl("new_Commitment");
var commitmentunit = Xrm.Page.getControl("new_CommitmentUnit");
var levelrate = Xrm.Page.getControl("new_LevelRate");
var linecode = Xrm.Page.getControl("new_LineCode");
var amount = Xrm.Page.getControl("new_Amount");
var amounttype = Xrm.Page.getControl("new_AmountType");
var acctype = Xrm.Page.getAttribute
("new_AgreementType").getSelectedOption().text;
if(acctype =="Lease Agreement")
{
volume.setVisible(false);
commitment.setVisible(false);
commitmentunit.setVisible(false);
levelrate.setVisible(false);
linecode.setVisible(false);
}
else
if(acctype == "General Cargo Agreement") { volume.setVisible(false); commitment.setVisible(false); commitmentunit.setVisible(false); levelrate.setVisible(false); linecode.setVisible(false); }
else
if(acctype =="Volume Agreement") { facility.setVisible(false); rentalarea.setVisible(false); areaunit.setVisible(false); amount.setVisible(false); amounttype.setVisible(false); } }
Thanks,
Surya N
you are not passing the attribute for the acctype,u try it.
var acctype = Xrm.Page.getAttribute("new_acctype");
Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);
I use this JavaScript.but this not worked for to set the focus on the desire field. any one have another solution????
Hi , Refer bellow link
http://jarrettexpertcrm.wordpress.com/2011/05/24/microsoft-crm-2011-disable-fields-from-picklist/
Hi I am trying to fill a custom field on one form from the custom field of the other. Do I need to try to use a SOAP envelope for this or is there an easier way? Thanks.
Hi Leanne - Yep - you would have to use soap to get the field form entity A and then set it on entity B. Once you do the first few of these, it is really straight foward code.
i am trying to get a value from (whole number & Currency Filed) and to set that value in text Field I have tried (i) parseInt , parseFloat ,.
But itz not working.. Can any one plz tel how to do this ....
Can you show how one would call upon the creation of a new activity from the underlying contact record an autopopulation of a field - say a phone number?
How to get the id of a primary entity in related entity. For eg how to get order ID in order Product ID.
Trying to hide a tab and show another if the value of OptionSelect is Vendor and then do the same if Distributor problem is it hides one and shows both tabs as if it is ignoring the if statements am i missing something obvious here?
function Information() {
var RelationshipType = Xrm.Page.getAttribute("customertypecode").getSelectedOption().text;
if (RelationshipType == "Vendor"); {
Xrm.Page.ui.tabs.get("CompanyInformation").setVisible(false);
Xrm.Page.ui.tabs.get("VendorInformation").setVisible(true);
}
if (RelationshipType == "Distributor"); {
Xrm.Page.ui.tabs.get("CompanyInformation").setVisible(false);
Xrm.Page.ui.tabs.get("DistributorInformation").setVisible(true);
}
}
Hi Chris - to help quickly debug, add an alert ( RelationshipType ); Right after you get it. this will show if your names are correct.
thanks Joe through some tinkering i found that the issue was the semi-colons after "Vendor") and "Distributor") removing them and it works as required.
Alright, but how to retrieve an option set (all items) via JavaScript in a form's OnLoad, that aren't related to the entity on the form?
Hi - If you want to retrieve all options sets from an unreleated option set.....something NOT on the form....then you will need to use the sdk and do a 'soap call'. However, if you need this, why not just add the option set to the form and keep it hidden?
Hi,
I have a problem about opening new form with default value of some fields in MS CRM 2011. I use this JavaScript and also i define that field in parameter section of second form, but that form opens with error message:
please help me about this problem.
Thanks,
Rey
function paymentrequest()
{
//get formno
var formno= Xrm.Page.data.entity.attributes.get("new_formno").getValue();
//define default values for new Incident record
var parameters = {};
parameters["new_purchasrequestno"] = "formno";
//pop paymentrequest form with default values
Xrm.Utility.openEntityForm("new_paymentrequest",null,parameters);
}
HI Rey - IN this line parameters["new_purchasrequestno"] = "formno"; you are setting your parameter to the text "formno". I'm assuming you want to set this to the actual value of your variable right? If so, then no need for the quotes. I would recommend starting with a simple js were you hard code the parameter. Then once that is working ok, get the parameter value from your field.
i want to Retrieve Solution (field) and offering (field) on the order item form when i select product (field) where the 3 fields are lookup fields
Hi,
I have a wizard with 2 steps and I want to make a field mandatory at first step so that I can't be able to pass to the second step until I have this field complete.
I tryied with an alert message but it's not enought. I click on the OK button from the message box and then I can pass to teh second step without filling in the mandatory field.
Thanks,
Anca
Thanks! Very useful page!
But I have an error while executing this --> Xrm.Page.ui.tabs.get(5).SetVisible(false);
Error message says that SetVisible is not a correct function.
The reason is function name is case sensitive and it needs not "SetVisible" but "setVisible".
Thanks for posting. We'll get it corrected.