JavaScript Simple Code

In this post, I will describe simple commands when developing JavaScript code for CRM2011 extensions. Here is just a list of the simplest commands. You will find more complex Javascript functions like role checking or WS querying in future posts. I will keep updating this post with the new simple functions that i use.

GET A FIELD VALUE

Xrm.Page.getAttribute("name").getValue();

If the field is an EntityReference – guid:

Id: Xrm.Page.getAttribute("name").getValue()[0].id;
Name: Xrm.Page.getAttribute("name").getValue()[0].name;

if the field is OptionSet

var selectedOptionSet = Xrm.Page.getAttribute("fieldname");
selectedOptionSet.getText()); //Text
selectedOptionSet.getValue()); //Value

To set the Value
selectedOptionSet.setValue(1);

//Or
Xrm.Page.getAttribute("name").getSelectedOption().text;
Xrm.Page.getAttribute("name").getSelectedOption().value;

SET THE OPTION SET VALUE BASED ON OPTIONTEXT

function SetValue(optionsetAttribute, optionText)
{
	var options = Xrm.Page.getAttribute(optionsetAttribute).getOptions();
	for(i = 0; i < options.length; i++)
	{
		if (options[i].text == optionText)
			Xrm.Page.getAttribute(optionsetAttribute).setValue(options[i].value);
	}
}

SET THE FIELD VALUE

Xrm.Page.getAttribute("name").setValue();

If the field is an EntityReference – guid:

Xrm.Page.getAttribute("name").setValue([{id:idValue,name:textValue,entityType:typeValue}]);
  • idValue : id of the record
  • textValue: record name
  • typeValue: Entity name

If the field is an ActivityParty

var ActivityAsegurado = [{id: IdContact, name: Nombre, entityType:'contact'}];
Xrm.Page.getAttribute("to").setValue(ActivityAsegurado);

//OR

var partlistData = new Array();
partlistData[0] = new Object();
partlistData[0].id = <guid>;
partlistData[0].name = "Joe CRM";
partlistData[0].entityType = 2;
Xrm.Page.getAttribute("to").setValue(partlistData);

CLEAR A FIELD VALUE

Xrm.Page.getAttribute("name").setValue(null)

SHOW/HIDE (true/false) FIELDS

Xrm.Page.getControl("name").setVisible(false);

SHOW/HIDE (true/false) TABS

Xrm.Page.ui.tabs.get("tabname").setVisible(false);

SHOW/HIDE (true/false) SECTIONS

Xrm.Page.ui.tabs.get("tabname").sections.get("sectionname").setVisible(false);

ENABLE/DISABLE (true/false) FIELDS

Xrm.Page.ui.controls.get("controlname").setDisabled(false);

GET THE CURRENT ENTITY ID OF THE FORM

Xrm.Page.data.entity.getId();

FORCE ENTITY SAVE

Xrm.Page.data.entity.save(); // Only Save
Xrm.Page.data.entity.save("saveandclose"); // Save & Close
Xrm.Page.data.entity.save("saveandnew"); // Save & New

STOP FORM FROM SAVING

After roll up 12 it doesn’t work without passing the context (from the form properties) to the function and executing only the context.getEventArgs().preventDefault() . More info here.

//ON SAVE EVENT
   Xrm.Page.context.getEventArgs().preventDefault();

OPEN ENTITY FORM

Xrm.Utility.openEntityForm(entityName,recordId);

CHECK IF A FIELD HAS BEEN MODIFIED 

If(Xrm.Page.getAttribute("name").getIsDirty()==true)

SEND READ ONLY FIELD ON SAVE

        Xrm.Page.getAttribute("lema_submit").setSubmitMode("always");

CHECK IF A FORM IS OPEN IN CREATION OR UPDATE MODE

var  formType= Xrm.Page.ui.getFormType();
//If the return value is 1 --> Form context is Create
//If the return value is 2 --> Form context is Update
if (formType != null)
{
   if(formType == 1)
	{  //Creation form
	}
}

Mas cosillas aqui.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s