Wednesday, October 26, 2016

Xrm.Utility functions : openQuickCreate


There is another good function in Xrm.Utility object : openQuickCreate. This function is only available for CRM 2015 (update 1) online or later versions.
Using this function we can open quick create form for entity even there is not any direct relationship.
e.g. if you have any custom entity, and this custom entity is not has any relationship with contact but for some reason when this custom entity form opens, you want to create contact / want to open contact quick create, you can use this Xrm.Utility function.
With openQuickCreate function, you can set default values on form by passing parameters and/or by using query string parameters and/or using attribute mapping defined in relationship.
Common syntax for openQuickCreate is

Xrm.Utility.openQuickCreate(entityLogicalName,createFromEntity,parameters).then(successCallback, errorCallback);

To open any entity quick create form only entityLogicalName is required, other parameters are optional.

When wants to open just quick create form without setting any default values, then we can use

Xrm.Utility.openQuickCreate( "contact",);

This will open quick create form for contact entity.

For all details about parameters, please check


The complete example of openQuickCreateForm is

carScript = {

    onLoadEvent:function()
    {       
        var thisEntityRecord = {
            entityType: "new_car",
            id: Xrm.Page.data.entity.getId(),
            name: Xrm.Page.getAttribute('new_name').getValue()
        };
        var parameters = {};
        parameters["new_manufactureaddress"] = Xrm.Page.getAttribute('new_manufactureaddress').getValue(); 
  
        Xrm.Utility.openQuickCreate("new_manufacture", thisEntityRecord, parameters).then(carScript.setManfactureLookup,carScript.errorfun);

    },
    setManfactureLookup:function(manfObj)
    {
        var lookup = new Array();
        lookup.push(manfObj.savedEntityReference);
        Xrm.Page.getAttribute('new_manufactureid').setValue(lookup);
    },
    errorfun:function(error)
    {
        Xrm.Utility.alertDialog(error);
    },
};

This function is called on form load; once form is loaded, quick create form for Manufacture (custom entity) will be displayed.  Once quick create record is saved, setManfactureLookup is called, which will set Manufacture lookup in car entity.
 When quick create record saved, you will get saved record entity reference object in SuccessCallbackfunction.

Note: There is a limit of 10 nested quick create forms in the web application. If this limit is exceeded this function will open the full entity form rather than quick create form. 

Tuesday, October 25, 2016

Xrm.Utility functions : OpenEntityForm


OpenEnityForm is used to one entity form, with this we can open create record form or existing record. Also when opening for creating new record we can pass values which will be default set on form.

Xrm.Utility.openEntityForm(name,id,parameters,windowOptions)

This function has 4 different parameters, only name is required, and all other are optional parameters. Here name parameter is entity logical name.

name:
Suppose you want to create task when contact record is updated, then we can use OpenEntityForm without any optional parameters or with parameters.

Xrm.Utility.openEntityForm("task");

This will open create task form, no default values will be set.

Id:
When id value specified, existing record with id specified will be opened, when record with GUID provided is not exists in system, then error will be thrown by system.

Xrm.Utility.openEntityForm("task", “B2DF0F4F-737A-E611-80DB-C4346BAC4B78”);

Paramters:

If you want to set subject and description of task when new task is created from contact then you can pass additional parameter, and that value will be set on task form.

var parameters = {};
parameters["subject"] = "Followup task";
parameters["description"] = "Follow up task for contact .";
Xrm.Utility.openEntityForm("task", null, parameters); 

Note: When setting parameter for creates entity, that attribute must be on form and name of attribute should match, otherwise create form will not be opened.

Here are some more examples, for how to set / pass parameter values of different data types.
Option Set:
      var parameters = {};
        parameters["gendercode"] = "1"; //1 : Male, 2 : Female

String / Numbers:
var parameters = {};
      parameters["name"] = "John Smith";
      parameters["new_salary"] = "10000";

Datetime:
var parameters = {};      
      parameters["birthdate"] = "10/23/1985";

  or
     var startDate = new Date();
     startDate.setDate(new Date().getDate() + 3);
     parameters["actualstart"] = startDate.format("MM/DD/YYYY");

Note:  You cannot use javascript date directly in parameters like  parameters["actualstart"] =   new Date(); CRM will throw “Expected 'DateTime' data type for 'actualstart' parameter in 'Request.QueryString'.”

Lookup:
There are different lookups in MS CRM. Using parameters you CANNOT set value for partylist and regarding lookups.  Other lookups are simple lookup, customer lookup and owner lookup.
When setting customer or owner lookup you have to specify type.
When passing parameter you need to use “name” or “type” suffix in attribute name.

Simple lookup:
var parameters = {};
parameters["leadid"] = "2878282E-94D6-E111-9B1D-00155D9D700B";
parameters["leadidname"] = "ABC Inc";

Customer lookup:
var parameters = {};
parameters["parentcustomerid"]="2878282E-94D6-E111-9B1D-00155D9D700B";
parameters["parentcustomeridname"]="Contoso";
parameters["parentcustomeridtype"]="account";

If contact entity
var parameters = {};
parameters["parentcustomerid"]="2878282E-94D6-E111-9B1D-00155D9D700B";
parameters["parentcustomeridname"] = "Contoso";
parameters["parentcustomeridtype"] = "contact";

Owner Lookup:
var parameters = {};
parameters["ownerid"] = "B2DF0F4F-737A-E611-80DB-C4346BAC4B78";
parameters["owneridname"] = "Jamie Reding";
parameters["owneridtype"] = "systemuser";

        When team
var parameters = {};
parameters["ownerid"] = "B2DF0F4F-737A-E611-80DB-C4346BAC4B78";
parameters["owneridname"] = "service";
parameters["owneridtype"] = "team";

With default field values parameters you pass custom query string parameters also. For how to set query string parameters check

navbar parameter:
This parameter controls whether application navigation bar will be displayed or not.
Available options for this parameter are
On: this is default option; with this option navigation bar is displayed.
var parameters = {};
parameters["navbar"] = "on";
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters)



Off: application navigation bar will not be displayed; users can navigate using interface elements or back and forward buttons.

var parameters = {};
parameters["navbar"] = "off";
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters)



Entity: On an entity form, only the navigation options for related entities are available. After navigating to a related entity, a back button is displayed in the navigation bar to allow returning to the original record.

var parameters = {};
parameters["navbar"] = "entity";
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters)
 

cmdbar parameter:
 With this option we can control application command bar, if this parameter set to true (default), then command bar will be displayed, when it is set to false, then no command bar will be displayed.

True value:
var parameters = {};
parameters["cmdbar"] = true;
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters)



False value:
var parameters = {};
parameters["cmdbar"] = false;
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters)



windowsOptions:
This option controls whether form need to be opened in new window or in same window.
Default value for this parameter is false,  if wants to open in new window then we need to specify true value for this parameter.

var windowsOptions = {};
windowsOptions["openInNewWindow"] = true;
Xrm.Utility.openEntityForm("task", "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200", parameters, windowsOptions);

Here is complete example

var parameters = {};

/*String*/
parameters["firstname"] = "John";
parameters["lastname"] = "Smith";

/*Option set  1: Male, 2: female*/
     parameters["gendercode"] = "1";

/*Date time*/
     parameters["birthdate"] = "10/28/1985";

/*other option to set date time*/
// var startDate = new Date();
// startDate.setDate(new Date().getDate() + 3);
// parameters["actualstart"] = startDate.format("MM/DD/YYYY");

/*Number*/
     parameters["new_salary"] = "10000";
       
/* Lookup - customer type*/
    parameters["parentcustomerid"] = "2878282E-94D6-E111-9B1D-00155D9D700B";
    parameters["parentcustomeridname"] = "Contoso";
    parameters["parentcustomeridtype"] = "account";

/*Lookup - simple*/
    parameters["leadid"] = "B681AC0C-91DF-E311-B8E5-6C3BE5A8B200";
    parameters["leadname"] = "Contoso Inc";
       
/* lookup - owner type*/
    parameters["ownerid"] = "B2DF0F4F-737A-E611-80DB-C4346BAC4B78";
    parameters["owneridname"] = "marketing";
    parameters["owneridtype"] = "team";
       
/*set navigation option, available options are: on (default), off, entity*/
    parameters["navbar"] = "on";

/*set command bar option , available options are: true (default), false*/
    parameters["cmdbar"] = true;

/*set windows options, available options are: true, false (default)*/
   var windowsOptions = {};
   windowsOptions["openInNewWindow"] = true;
   Xrm.Utility.openEntityForm("contact", null, parameters, windowsOptions);    

Xrm.Utility functions : Dialogs

There are useful functions in Xrm.Utility object. These functions are not directly related to current page. 

Dialogs:
In older version of CRM, if you want to show alert or confirm dialog, then we have to JavaScript alerts / confirm dialog, but now there are two functions in Xrm.Utility which will give same functionality. 

When we want to show alert message on form or button click we can Xrm.Utility object. 

alertDialog
alertDialog we can use to show just message or when dialog closed, we can call any javascript function. 

Xrm.Utility.alertDialog(message,onCloseCallback)

Example:

contactScript =
{
    NoTaskCreated: function () {
        Xrm.Utility.alertDialog("No Task created.", contactScript.TaskDialogClose);
    },
    TaskDialogClose: function () {
        Xrm.Utility.alertDialog("Dialog Closed");
    },
};

Here in above example, in NoTaskCreated  function calling alertDialog with callback function(contactScript.TaskDialogClose) which will be called when user closes this alert dialog. And TaskDialogClose function shows alertDialog without callback function. 

confirmDialog
confirmDialog will show OK and Cancel buttons with message, and you can call functions when user clicks on OK and Cancel buttons.
Xrm.Utility.confirmDialog(message,yesCloseCallback,noCloseCallback)

Example:
contactScript =
{
    OnSaveContact: function () {
        Xrm.Utility.confirmDialog("Do you want to Create Followup Task?",
contactScript.CreateTask, contactScript.NoTaskCreated);
},
    NoTaskCreated: function () {
        Xrm.Utility.alertDialog("No Task created.", contactScript.TaskDialogClose);
    },
    TaskDialogClose: function () {
        Xrm.Utility.alertDialog("Dialog Closed");
    },
    TaskCreated: function () {
        Xrm.Utility.alertDialog("Task Created.");
    },
};

In this above example, when contact record is saved calling OnSaveContact function, which will ask user about creating follow-up task. This message will be shown using confirmDialog and based on user’s action either TaskCreated function will be called or TaskDialogClose function is called.