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);    

4 comments:

  1. I have Accounts and its associated entity Cases. I want to open individual case in new window from associated cases view of Accounts entity. From which event should I call OpenEntityForm function.

    ReplyDelete
  2. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    CRM Solutions



    ReplyDelete
  3. Nice post... It really too helpful for me.

    ReplyDelete