Development

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

I had a task today to open a new task form with the Regarding field prefilled. Based on the 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 looked for some explanation and found it in the SDK in the same article I mentioned before. The answer was that it is impossible to set values for partylists and regarding lookups.

I knew that this feature worked for the out of box buttons so, I rechecked the URL for activities created with OOB buttons and reworked the code to the 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);

13 Comments

  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…

  2. 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}]);}

  3. 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.

  4. Hey i am using mscrm 2015 where regardingobjectid shows all the entities. I want to set to a custom entity is ccs_corporatecustomer. how do i achieve ? i created a javascript code on form load but not working Please help??

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.