Tuesday, July 9, 2019

How to get and set value for different datatypes in Dynamic 365 CRM


Let’s first see how many different data types we have in dynamic 365 CRM v9.0


Make sure that when we are defining a method, we should pass execution context as a first parameter.


Below is a complete method in JavaScript code.


function GettingAndSettingValues(context) {

var formContext = context.getFormContext();
// formContext will be used to access form attributes.

debugger;

// Retrieving Form Attribute is same for all Types using getAttribute()
// So here is a list of attribute name which we are trying to get and save value into.

var new_singlelineoftext = formContext.getAttribute("new_singlelineoftext");
var new_optionset = formContext.getAttribute("new_optionset");
var new_multiselectoptionset = formContext.getAttribute("new_multiselectoptionset");
var new_twooptions = formContext.getAttribute("new_twooptions");
var new_wholenumber = formContext.getAttribute("new_wholenumber");
var new_floatingpointnumber = formContext.getAttribute("new_floatingpointnumber");
var new_decimal = formContext.getAttribute("new_decimal");
var new_currency = formContext.getAttribute("new_currency");
var new_multiplelineoftext = formContext.getAttribute("new_multiplelineoftext");
var new_dateandtime = formContext.getAttribute("new_dateandtime");
var new_countryLookup = formContext.getAttribute("new_lookup");


*************************************************************

// Single Line Of Text
if (new_singlelineoftext != null) {
    var tempValue = new_singlelineoftext.getValue();
    if (tempValue != null)
        new_singlelineoftext.setValue(tempValue + " : Appending Text");
    else
        new_singlelineoftext.setValue("New Text");
}

// OptionSet or Drop Down List   
if (new_optionset != null) {
    var internalValue = new_optionset.getValue();

    if (internalValue != null) {
        var optionSelectedValue = new_optionset.getSelectedOption().value;
        var optionSelectedText = new_optionset.getSelectedOption().text;
    }
    else {
        alert("Nothing Selected - So Setting value to Item 2");
        new_optionset.setValue(100000001);
    }
}

// Multi-Select OptionSet
if (new_multiselectoptionset != null) {
    var selectedValue = new_multiselectoptionset.getValue();

    if (selectedValue != null) {
        var allSelectedOptions = new_multiselectoptionset.getSelectedOption();
        for (var i = 0; i < allSelectedOptions.length; i++) {
            var text = allSelectedOptions[i].text;
            var value = allSelectedOptions[i].value;
        }
    }
    else {
        // Setting some default Value for nothing selected
        new_multiselectoptionset.setValue([100000001, 100000002]);
    }
           
}

// Two OptionSet - Boolean
if (new_twooptions != null) {
    var selectedValue = new_optionset.getValue();

    if (selectedValue != null) {
        if (selectedValue == true)
            alert("TRUE");
        else
            alert("Its False");
    }
    new_twooptions.setValue(true);
}

// Whole-Number
if (new_wholenumber != null) {
    var inputNumber = new_wholenumber.getValue();
    if (inputNumber != null)
        new_wholenumber.setValue(inputNumber + 10);
}

// Floating Point Number
if (new_floatingpointnumber != null) {
    var inputNumber = new_floatingpointnumber.getValue();
    if (inputNumber != null)
        new_floatingpointnumber.setValue(inputNumber + 10);

}
// Decimal Number
if (new_decimal != null) {
    var inputNumber = new_decimal.getValue();
    if (inputNumber != null)
        new_decimal.setValue(inputNumber + 10);
}

// Currency Type
if (new_currency != null) {
    var currencyValue = new_currency.getValue();
    if (currencyValue != null)
        new_currency.setValue(currencyValue + 10);
}

// Multiple-Line-Of-Text
if (new_multiplelineoftext != null) {
    var tempValue1 = new_multiplelineoftext.getValue();
    if (tempValue1 != null)
        new_multiplelineoftext.setValue(tempValue1 + " : Appending Text");
    else
        new_multiplelineoftext.setValue("New Text");
}

// Date and Time
if (new_dateandtime != null) {
    var tempdate = new_dateandtime.getValue();
    if (tempdate != null) // if Not set to 1 day after current set day
        new_dateandtime.setValue(tempdate.setDate(tempdate.getDate() + 1));
    else
        new_dateandtime.setValue(new Date());
}

// Lookup
if (new_countryLookup != null) {
    countryLookupValue = new_countryLookup.getValue();
    if (countryLookupValue != null) {
        var entityTypeCode = countryLookupValue[0].type;
        var entityTypeName = countryLookupValue[0].entityType;
        var entityId = countryLookupValue[0].id.replace("{", "").replace("}", "");
        var entityText = countryLookupValue[0].name;
    }
}

        Xrm.Page.getAttribute("uomid").setValue([{ id: "69DAC10E-96BC-4F84-8BE9-29F84FF4401C", name: "Default Unit", entityType: "uomschedule" }]); // Unit Group

Xrm.Page.getAttribute("ownerid").setValue([{ id: createdUser[0].id, name: createdUser[0].name, entityType: "systemuser" }]);

// Its a good habit to explicity tell Dynamic CRM to save value, which are change via Javascript Code

new_singlelineoftext.setSubmitMode('always');
new_optionset.setSubmitMode('always');
new_multiselectoptionset.setSubmitMode('always');
new_twooptions.setSubmitMode('always');
new_wholenumber.setSubmitMode('always');
new_floatingpointnumber.setSubmitMode('always');
new_decimal.setSubmitMode('always');
new_currency.setSubmitMode('always');
new_multiplelineoftext.setSubmitMode('always');
new_dateandtime.setSubmitMode('always');
new_countryLookup.setSubmitMode('always');
}

No comments: