Launching CRM Forms via Java Script in CRM 2011

Here’s a couple of examples demonstrating how to pop CRM forms via java script (from a CRM form event / ribbon button).

This first example shows how to pop an existing record, in this case a Case record.  In this scenario I have a field called Existing Case (new_existingcase) on the Phone Call form which is a lookup field to the Case entity.   This function reads the GUID and name value of that Case and then pops that record using a window.open command:

function PopCase() {
    //Get the value of the ExistingCase field
    var ExistingCase = Xrm.Page.data.entity.attributes.get("new_existingcase");
    if (ExistingCase.getValue() != null) {

        //Split out the GUID and Name from the ExistingCase field
        var ExistingCaseGUID = ExistingCase.getValue()[0].id;
        var ExistingCaseName = ExistingCase.getValue()[0].name;

        //Set features for how the Case form window will appear
        var features = "location=no,menubar=no,status=no,toolbar=no";

        //Pop the Case record
        window.open("/main.aspx?etn=incident&pagetype=entityrecord&id=" + encodeURIComponent(ExistingCaseGUID), "_blank", features, false);
    }
}

 

This second example shows how to create a new record, again let’s look at the Case entity.  In this scenario my code sits on the Phone Call form and want to create a new Case for the Customer specified on the Phone Call.  First I retrieve the details of the Customer from the CRM form (from the “from” field).  Then in a variable called extraqs I define the parameter values I want to pass across to the form so that some of the field values default, including the Customer field.   Note that to populate a Customer lookup field I have to provide 3 parameter values (for simpler lookup fields you supply just the first 2 values).  Then, to pop the form, it’s a simple window.open with the parameters variable included:

//Collect values from the existing CRM form that you want to default onto your new record
var CallerGUID = Xrm.Page.data.entity.attributes.get("from").getValue()[0].id;
var CallerName = Xrm.Page.data.entity.attributes.get("from").getValue()[0].name; 

//Set the parameter values
var extraqs = "&title=New Case";
extraqs += "&customerid=" + CallerGUID;
extraqs += "&customeridname=" + CallerName;
extraqs += "&customeridtype=contact"; 

//Set features for how the window will appear
var features = "location=no,menubar=no,status=no,toolbar=no"; 

//Pop the window
window.open("/main.aspx?etn=incident&pagetype=entityrecord&extraqs=" + encodeURIComponent(extraqs), "_blank", features, false);

 

Hope this is helpful.

Advertisements

One thought on “Launching CRM Forms via Java Script in CRM 2011

  1. Scotty

    Thanks for this! We’re seeing an issue when exercising the second example… The initial “new” record is fine, but if our users are pressing Save & New, the link to the original customer is lost.

    Thought?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s