Saturday, December 13, 2014

Scripting form fields in MS-CRM 2011 , 2013 , 2015 with Java Script



Retrieving and Setting Value of  Fields  In MS-CRM  with JAVA SCRIPT



1 ) Working with text field :


function AlertTextField() 
{
    var MainPhone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
    alert(MainPhone);
}

2 ) Writing information back to the text field

    try
{
Xrm.Page.getAtribute("new_number").setValue("AAA");
}
catch(err)
{
alert("Error: " + err.message);
}

3 )  Working with Currency field :

function ReadCurrency()
{
var myCurrencyField;
myCurrencyField = Xrm.Page.getAttribute("new_
currencyfield").getValue();
alert("The value of this Currency field is: " +
myCurrencyField);
}
4 ) Writing information back to the Currency field

try
{
var myCurrencyValue = 6.25;
Xrm.Page.getAttribute("new_currencyfield").setValue(parseFloat(myCurrencyValue));
}
catch(err)
{
alert("Error: " + err.message);
}
5 ) Working with Date and Time field :
function ReadBirthday()
{
var myContactBirthday;
myContactBirthday = Xrm.Page.getAttribute("birthdate").getValue();
alert("Contact birthday is: " + myContactBirthday);
var year = myContactBirthday.getFullYear();
var month = myContactBirthday.getMonth(); // from 0 to 11
var day = myContactBirthday.getDate(); // from 1 to 31
month = month + 1;
alert("Year: " + year + ", Month: " + month + ", Day: " + day);
}
6 ) Writing information back to the Date and Time field
try
{
var currentDateTime = new Date();
Xrm.Page.getAttribute("new_myDate").setValue(currentDateTime);
}
catch(err)
{
alert("Error: " + err.message);
}
7 )  Working with Option Sets field :

function GetOSValue()
{
var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().text;
alert("Selected value: " + sval);
}
Alternate Method :
var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().value;

8 ) Writing information back to the Option set field
Assigning a value programmatically

function SetOSValue (osName, osLabel){
try
{
var options = Xrm.Page.getAttribute(osName).getOptions();
for(i = 0; i < options.length; i++)
{
if (options[i].text == osLabel)
Xrm.Page.getAttribute(osName).setValue(options[i].value);
}
}
catch(err)
{
alert("Error: " + err.message);
}
}
calling Jscript Function
function SetMe()
{
SetOSValue("new_optionset","Example C");
}
9 ) Working with LookUP field :

function ReadState()
{
var state = new Array();
state = Xrm.Page.getAttribute("new_state").getValue();
if(state != null)
{
var stateText = state[0].name;
var stateId = state[0].id;
var stateType = state[0].entityType;
alert("State is: " + stateText + ", ID: " + stateId + " of type: " + stateType);
}
}

10 ) Writing information back to the lookup field
Clearing a lookup 
function ClearState()
{
var state = Xrm.Page.getAttribute("new_state");
if (state != null)
{
Xrm.Page.getAttribute("new_state").setValue(null);
}
}
Changing a lookup selection
function SetStateToNY()
{
var state = new Array();
state[0] = new Object();
state[0].id = "{BA0762E4-64D2-E111-909E-00155D6C871A}";
state[0].name = "New York";
state[0].entityType = "new_state";
Xrm.Page.getAttribute("new_state").setValue(state);
}
function setOTBState()
{
var state = new Array();
state = Xrm.Page.getAttribute("new_state").getValue();
if(state != null)
{
var stateText = state[0].name;
if(stateText != "Other")
{
Xrm.Page.getAttribute("address1_stateorprovince").setValue(stateText);
Xrm.Page.ui.controls.get("address1_stateorprovince").setVisible(false);
}
else
{
Xrm.Page.getAttribute("address1_stateorprovince").setValue("");
Xrm.Page.ui.controls.get("address1_stateorprovince").setVisible(true);
}
}
}

2 comments:

  1. If you could please explain 8th program elaborately. specially in the line where it says
    " if (options[i].text == osLabel) "

    ReplyDelete