Thursday, January 10, 2013

MS CRM 2011: Open new activity form with ‘Regarding’ field prefilled

I had a task today to open new task form with Regarding field prefilled. Based on SDK description I tried to use following code to accomplish my task:

var params = {
    regardingobjectid: referenced record id,
    regardingobjectidtype: referenced record type name,
    regardingobjectidname: referenced record display name
};

Xrm.Utility.openEntityForm("task", null, params);

Unfortunately this code failed. I’ve started to look for some explanation and I found it in SDK at the same article I’ve mentioned before. Answer was that it is impossible to set values for partylists and regarding lookups.
I knew that this feature works for out of box buttons so I rechecked URL for activities that are created with OOB buttons and reworked code to following which worked without issues:

var params = {
    pId: referenced record id,
    pType: referenced record type code,
    pName: referenced record display name
};

Xrm.Utility.openEntityForm("task", null, params);

10 comments:

  1. Interesting note on the sometimes shady variations in the structure of CRM.
    I have written about another related issue concerning how the regarding-field can be populated.
    http://jonasrapp.cinteros.se/2012/08/setregarding.html

    Any thoughts on this?

    ReplyDelete
    Replies
    1. Haven't created activities in that way. I believe that one of 2 following statements is correct:
      1. This is a bug.
      2. This is a feature that was done with MS...

      Delete
    2. Completely agree with your "analysis" ! :)

      Delete
    3. Of course it was a joke =)
      Not sure why does Microsofties did this functionality in mentioned way...

      Delete
  2. Thank you, it is great shurtcut,
    In the past I had the same problem and I found this article
    http://www.crmnerd.com/customizations/using-xrm.utility.openentityform-with-custom-parameters/
    It worked great for me and I thought it is worth sharing.

    ReplyDelete
  3. there are multiple ways
    1. via form querystring:
    function OpenNewContact() {
    //Set the Parent Customer field value to “Contoso”.
    var extraqs = "parentcustomerid={F01F3F6D-896E-DF11-B414-00155DB1891A}";
    extraqs += "&parentcustomeridname=Contoso";
    extraqs += "&parentcustomeridtype=account";
    //Set the Address Type to “Primary”.
    extraqs += "&address1_addresstypecode=3";
    //Set text in the Description field.
    extraqs += "&description=Default values for this record were set programatically.";
    //Set Do not allow E-mails to "Do Not Allow".
    extraqs += "&donotemail=1";
    //Set features for how the window will appear.
    var features = "location=no,menubar=no,status=no,toolbar=no";
    // Open the window.
    window.open("/main.aspx?etn=contact&pagetype=entityrecord&extraqs=" +
    encodeURIComponent(extraqs), "_blank", features, false);
    }

    2. via parameters:
    function OpenNewContact() {
    var parameters = {};
    //Set the Parent Customer field value to “Contoso”.
    parameters["parentcustomerid"] = "2878282E-94D6-E111-9B1D-00155D9D700B";
    parameters["parentcustomeridname"] = "Contoso";
    parameters["parentcustomeridtype"] = "account";
    //Set the Address Type to “Primary”.
    parameters["address1_addresstypecode"] = "3";
    //Set text in the Description field.
    parameters["description"] = "Default values for this record were set programmatically.";
    //Set Do not allow E-mails to "Do Not Allow".
    parameters["donotemail"] = "1";

    // Open the window.
    Xrm.Utility.openEntityForm("contact", null, parameters);
    }


    3. if you see parameters don't work via above code, Here is the workaround:

    use this code:
    var params = {
    parameter_regardingobjectid: referenced record id,
    parameter_regardingobjectidtype: referenced record type name,
    parameter_regardingobjectidname: referenced record display name
    };

    Xrm.Utility.openEntityForm("task", null, params);

    On form customization go to properties and add three parameters as string:
    parameter_regardingobjectid,parameter_regardingobjectidtype,parameter_regardingobjectidname

    and on form onload set the regarding field to the following parameters using this code:
    var param=Xrm.Page.context.getQueryStringParameters();
    var regardingId=param["parameter_regardingid"];
    var regardingName=param["parameter_regardingname"];
    var regardingType=param["parameter_regardingtype"];

    //Populate the Regarding if there is one
    if (regardingId != undefined)
    {Xrm.Page.getAttribute("regardingobjectid").setValue([{id:regardingId, name:regardingName, entityType:regardingType}]);}

    ReplyDelete
    Replies
    1. Hello SHONI,
      1. I'm very appreciated for your message but I haven't asked how to solve an issue. I shared with solution I have already used in live project.

      2. I'm quite comfortable with SDK and I know how to work with prefilling of fields in new records via URL as well as how to work with Xrm.Utility class. Thanks for copypasting from http://msdn.microsoft.com/en-us/library/gg334375.aspx#BKMK_SetLookupFieldValues, I'm "really" appreciate it.

      3. Code you've provided in point 3 has been already mentioned with yairrose 2 posts before yours.

      Kind regards,
      Andrii.

      Delete
    2. I believe that it was a voice of Captain Obvious =)

      Delete