Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications

|

Passing Parameters Between Forms in CRM 2011

Post Author: Joe D365 |

Did you ever wish you could scoot some data from one form to another and then immediately start working on the new record? If so, then maybe passing parameters from one form to another in a query string is the answer for you!

In this example we will open a new email from the Account form with a function that will pass parameters to the form so we can use them on the email form.

Let's start on the Account form. Here is a small function that gets the GUID for the Account record, sets it as the parameter "accountid_0" and then uses the openEntityForm function to open the email form.

//Function to open another entity's form

function NewEmail()
{
 var parameters = {};
 var accountid = Xrm.Page.data.entity.getId();
  parameters["accountid_0"] = accountid;

Xrm.Utility.openEntityForm("email", null, parameters);
}

This small function can be added to a button or to an event on the form. In our example I will simply add it to the OnSave event.

passing parameters between forms in CRM 2011

On our Email form I am going to set up some events to read the query string, grab the value for "accountid_0" and then show it in a message box. I'll add the "formonload" function to our Form Load event.

//Function to read the QueryString

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[[]/,"[").replace(/[]]/,"]");
  var regex = new RegExp("[?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}
function formload()
{
if (getQuerystring('accountid_0') != "")
{
    var accountid = getQuerystring('accountid_0');
    accountid = accountid.substr(3,32);
    alert(accountid);
}
}


Now let's go over to an Account record, save some data, and we should get a new email message form and a message box with the Account GUID. Ready?

Oh no! What happened? We can see that we've got the email form here, the ribbon is looking good. But what is this error?

We can see this is a CRM Online Organization that has been updated to UR12 which gives us a clue*. According to this MSDN article we've missed a step. We need to revisit our Email form and set it up to accept our custom parameter.

*Interesting to note, the configuration works fine in an on-premise CRM organization running UR8.

You can set custom parameters in the Form Properties right on the Parameters tab. Since AccountID is a GUID, I am going to give it the type of UniqueID, but you can see there are a number of options here to work with.

We'll name the custom parameter "accountid_0" and set the Type to UniqueId. Save, Publish, and let's try it again.

Looks like we have a winner! Now that we have the account ID accessible from the email entity form, there are any number of things we could do.

Maybe you'd like to use an oData query to get some other data from records related to the Account and pre-populate the subject of the email or even parts of the body.

Maybe you'd like to pass more parameters in the query string and use them to set a few values.

Maybe you'd like a chocolate shake? Wait, I suppose you could use CRM to email someone to ask for one, but why not celebrate your victory with a trip to the ice cream shop? Sounds like a great idea to me.

Enjoy and happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

3 comments on “Passing Parameters Between Forms in CRM 2011”

  1. Hi I am facing some problem in getQuerystring() am getting NULL as result after execution of this line "qs = regex.exec(window.location.href)" .. The value which i am getting in the regex variable is "[?&]accountid_0=([^&#]*)"..Can anyone help me out why I am getting NULL value ?

PowerObjects Recommends