Create Note (Annotation) using Jscript in Microsoft Dynamics CRM 2011

Posted: October 7, 2012 in CRM 2011, Jscripts

Add Note using Jscript

In my example i’m going to create a note to account entity record in CRM.

To perform an ODATA call, we need to add jquery1.4.1.min and json2 files in your Form Properties. You can get those files from sdk.
path – \sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts.

function AddNotes() {
    var noteText = "First Note Added.";
    var accountId = Xrm.Page.data.entity.getId();
    var serverUrl = Xrm.Page.context.getServerUrl().toString();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var objAnnotation = new Object();
    var ODATA_EntityCollection = "/AnnotationSet";
    objAnnotation.NoteText = noteText;
    objAnnotation.Subject = "Note Subject";

    var refAccount = new Object();
    refAccount.LogicalName = "account";
    refAccount.Id = accountId;

    objAnnotation.ObjectId = refAccount;
    objAnnotation.ObjectTypeCode = refAccount.LogicalName;

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

    // Asynchronous AJAX function to Create a CRM record using OData 
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}

Advertisements

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