Quick Create Contact from the CRM Case Form

In a previous post I demonstrated a screen customisation that allows quick creation of a new Contact directly on the Phone Call form.   Today I needed the same thing on the Case form so thought I would share that code here as well.

Here’s what my screen looks like:


The “Quick Create New Citizen” fields are enabled so long as a “Citizen” has not yet been specified on the Case (note: I have renamed Contact to Citizen).   Once you key into one of the Quick Create fields the other Quick Create fields become mandatory.  Once they are all populated a new Citizen/Contact is created and the Case is updated to link to that record.

Here’s the java script:

function SetLookupValue(fieldName, id, name, entityType) { 
    if (fieldName != null) { 
        var lookupValue = new Array(); 
        lookupValue[0] = new Object(); 
        lookupValue[0].id = id; 
        lookupValue[0].name = name; 
        lookupValue[0].entityType = entityType; 

function DisableFields() { 

function MakeFieldsMandatory() { 

function MakeFieldsNonMandatory() { 

function OnLoad() { 
    if (Xrm.Page.ui.getFormType() == 1) { 
    else if (Xrm.Page.ui.getFormType() != 1) { 

function NewContact() { 
    if ( 
            Xrm.Page.getAttribute("new_firstname").getValue() == null && 
            Xrm.Page.getAttribute("new_lastname").getValue() == null && 
            Xrm.Page.getAttribute("new_phonenumber").getValue() == null) { 
    else if ( 
                Xrm.Page.getAttribute("new_firstname").getValue() != null && 
                Xrm.Page.getAttribute("new_lastname").getValue() != null && 
                Xrm.Page.getAttribute("new_phonenumber").getValue() != null && 
                Xrm.Page.data.entity.attributes.get("customerid").getValue() == null) { 
    else { 

function CreateContact() { 
    // Get the CRM URL 
    var serverUrl = Xrm.Page.context.getServerUrl(); 

    // Cater for URL differences between on premise and online 
    if (serverUrl.match(/\/$/)) { 
        serverUrl = serverUrl.substring(0, serverUrl.length - 1); 

    // Specify the ODATA end point (this is the same for all CRM 2011 implementations) 
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; 

    // Specify the ODATA entity collection 
    var ODATA_EntityCollection = "/ContactSet"; 

    // Define an object for the CRM record you want created 
    var CRMObject = new Object(); 

    // Define attribute values for the CRM object 
    CRMObject.FirstName = Xrm.Page.getAttribute("new_firstname").getValue(); 
    CRMObject.LastName = Xrm.Page.getAttribute("new_lastname").getValue(); 
    CRMObject.Telephone1 = Xrm.Page.getAttribute("new_phonenumber").getValue(); 

    //Parse the entity object into JSON 
    var jsonEntity = window.JSON.stringify(CRMObject); 

    //Asynchronous AJAX function to Create a CRM record using OData 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        datatype: "json", 
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection, 
        data: jsonEntity, 
        beforeSend: function (XMLHttpRequest) { 
            //Specifying this header ensures that the results will be returned as JSON.      
            XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
        success: function (data, textStatus, XmlHttpRequest) { 
            //This function will trigger asynchronously if the Retrieve was successful 
            //alert("ajax call successful"); 
            var NewCRMRecordCreated = data["d"]; 
            var FullName = Xrm.Page.getAttribute("new_firstname").getValue() + " " + Xrm.Page.getAttribute("new_lastname").getValue(); 
            SetLookupValue("customerid", NewCRMRecordCreated.ContactId, FullName, "contact"); 
        error: function (XmlHttpRequest, textStatus, errorThrown) { 
            //This function will trigger asynchronously if the Retrieve returned an error 
            alert("ajax call failed"); 

The new fields are called new_firstname, new_lastname and new_phonenumber.

You’ll need to add the following libraries and event handler to the Case form’s OnLoad event (I provide a download link to these at the end of this post):


And you will need this event handler added to each of the Quick Create field’s OnChange events:


The 4 web resources employed are available here.



7 thoughts on “Quick Create Contact from the CRM Case Form

  1. Patrick Honorez

    This post (and some others are invaluable examples for me. Thank you !
    Are there any tricks to know about debugging javascript in Dynamics CRM ? I am a bit in the dark on that side…

    1. Gareth Tucker Post author

      You’re welcome, glad to be able to help. To debug, add the following line into your java script to create a break point:


      Then make sure under “Internet Explorer –> Internet Options –> Advanced” you have “Disable Script Debugging (Internet Explorer)” unchecked.

      Update your web resource and then refresh you form and the debugger should catch. You’ll be asked what to do, tell the debugger to open a new instance Visual Studio and you will now be in debug mode. You can now press F10 to step through your code and you can right click on variables and select Quick Watch to see what values they hold.

      Hope this helps 🙂

  2. NAG

    Hi, I have a question here. When we are trying to add duplicate contact and the rule is enabled on phone number, will it check duplicate detection rule or it will ignore and add to the contact ?

  3. puneet joshi


    I have a business requirement where I have to copy record from Entity A to Entity B on Click of a Ribbon Button.

    On Entity A’s Form, i have created a Ribbon button called “Copy Record”, Because i was not able to trigger Plug-in from Ribbon button, i have used a hidden Boolean field (Yes/No) in the Entity A’s Form and on click on that Ribbon Button i am updating the Boolean field’s Value from NO to Yes. Then i am trying to trigger a plug-in which will create a record in the Update Message of Entity A. I Could not figure out where and why it is failing. I have a deadline so i was looking into other options, i visited your post and seems like it can help.

    I could have used WF’s to do it but the only challenge is I can copy the Option Sets using Work Flow. Other than that everything is getting copy over. I am sure you have see this post ages back. http://blogs.msdn.com/b/crm/archive/2008/06/13/use-workflow-to-configure-business-data-auditing-in-microsoft-dynamics-crm-4-0.aspx

    Please direct me how to leverage your above mentioned code to copy record from one entity to another. Entity B’s fields are same as Entity A (Similar DataType, Similar Structure).
    Also i have 100+ record in Entity A’s form to copy over to Entity B. Will it possible to copy so many fields.

    Your help will be much appreciated.



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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s