Development 101: working with parameters

Someone asked me to place an additional button on an account form. Pressing the button should popup a dialog in which notes could be taken about a meeting. he also asked me to prefill some fields in the popup dialog. While perfoming this task, a couple of questions arose:

How to display a popup?

How to pass parameters?

How to retrieve the parameters?

It turned out that passing parameters takes three steps

  1. On the calling form fill the parameters and send them out
  2. On the receiving form define parameters
  3. On the receiving form grab parameters and process them accordingly

The client side libraries for CRM are pretty good. I found a method that would open an entity form for me (Xrm.Utility.openEntityForm). This function can be called passing in form parameters. One of the fields on the receiving form was a lookup field. This field is a but different from other fields as you have to pass in three parameters to fill the field: the ID of the record to show, the display value and the entity to which it belongs.

In the code sample below, I show how to call a form passing in parameters.

function OpenMeetingNote() {
  var Guid = Xrm.Page.data.entity.getId();

  // construct the parameters object, which we pass to the receiving form
  // for a lookup field we need to specify the underlying ID, the display value and
  // the entity to which it belongs
  var parameters = {};
  parameters["customer_id"] = guid;
  parameters["customer_name"] = $("name").val();  // jQuery required
  parameters["customer_type"] = Xrm.Page.data.entity.getEntityName();

  // open a new form and pass the parameters
  Xrm.Utility.openEntityForm("new_meetingnote", null, parameters);
}

On the receiving form the parameters have to be specified ( I declared them as SafeString). This can be done in the form editor within CRM.

Parameters1

Once declared I added an additional script on the receiving form to fire on the form load event. That function takes the parameters passed in and positions the lookup field. If you omit to declare the incoming parameters on the form you cannot use the Xrm.Page.context.getQueryStringParameters() function to read the parameters.

function FillContact() {
  // get a reference to the customers lookup field and change
  // the default type, we need this to search properly (when pressing search button).
  var field = $("#customers"); //jQuery required
  if (field != null && field != undefined) {
    field.attr("defaulttype","2");
  }

  // read the querystring, and retrieve the fields passed in
  var xrmObject = Xrm.Page.context.getQueryStringParameters();
  var customer_id = xrmObject["customer_id"].toString();
  var customer_name = xrmObject["customer_name"].toString();
  var customer_type = xrmObject["customer_type"].toString();

  // do a null check
  if (customer_id != null && customer_id != undefined) {
    // in order to set the customer lookup, we need to pass in an array
    // containing the ID, name and entity type
    var lookupValue = new Array();
    lookupValue[0].id = customer_id;
    lookupValue[0].name = customer_name;
    lookupValue[0].entityType = customer_type;

    // fill the customer lookup with the value passed in
    Xrm.Page.getAttribute("customers").setValue(lookupValue);
  }
}

I found a really helpful blog by Gareth Tucker. On Gareth Tucker’s CRM blog. you will find an extensive list of 46 code snippets which are very handy when writing custom script. It helped me through the day.

Leave a Reply

Your email address will not be published. Required fields are marked *