locked
Notes and activities and JavaScript CRM 2011 RRS feed

  • Question

  • Hi,

    How to get newly added note on activity form and set activity attribute with this value. With JavaScript.

    Tuesday, February 4, 2014 4:42 PM

Answers

  • This would get the last Note attached to a Task and the update the Description field.

    var taskId = "EDBB2236-B57F-E311-BB6D-6C3BE5A881A4";
    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AnnotationSet?" + "$select=AnnotationId&$filter=ObjectId/Id eq guid'" + taskId + "'&$top=1&$orderby=CreatedOn desc"), true);
    req.setRequestHeader('Accept', 'application/json');
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var returned = JSON.parse(req.responseText).d;
                var results = returned.results;
                var AnnotationId = results[0].AnnotationId;
                var entity = {};
                entity.NoteText = 'test';
    
                req = new XMLHttpRequest();
                req.open('POST', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AnnotationSet(guid'" + AnnotationId + "')"), true);
                req.setRequestHeader('Accept', 'application/json');
                req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
                req.setRequestHeader('X-HTTP-Method', 'MERGE');
                req.onreadystatechange = function () {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 204 || this.status == 1223) {
                            alert('Updated'); //Success - Do Something
                        } else {
                            alert(this.statusText);
                        }
                    }
                };
                req.send(JSON.stringify(entity));
            } else {
                alert(this.statusText);
            }
        }
    };
    req.send();


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Tuesday, February 4, 2014 6:06 PM
    Moderator
  • There isn't a way to hook into that event in a supported way - you would need to trigger this code from a supported event like a field change on a form.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 1:28 AM
    Moderator

All replies

  • This would get the last Note attached to a Task and the update the Description field.

    var taskId = "EDBB2236-B57F-E311-BB6D-6C3BE5A881A4";
    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AnnotationSet?" + "$select=AnnotationId&$filter=ObjectId/Id eq guid'" + taskId + "'&$top=1&$orderby=CreatedOn desc"), true);
    req.setRequestHeader('Accept', 'application/json');
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var returned = JSON.parse(req.responseText).d;
                var results = returned.results;
                var AnnotationId = results[0].AnnotationId;
                var entity = {};
                entity.NoteText = 'test';
    
                req = new XMLHttpRequest();
                req.open('POST', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AnnotationSet(guid'" + AnnotationId + "')"), true);
                req.setRequestHeader('Accept', 'application/json');
                req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
                req.setRequestHeader('X-HTTP-Method', 'MERGE');
                req.onreadystatechange = function () {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 204 || this.status == 1223) {
                            alert('Updated'); //Success - Do Something
                        } else {
                            alert(this.statusText);
                        }
                    }
                };
                req.send(JSON.stringify(entity));
            } else {
                alert(this.statusText);
            }
        }
    };
    req.send();


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Tuesday, February 4, 2014 6:06 PM
    Moderator
  • Thank you but how can I use it on something like "AddNote" ?
    • Edited by rogus1 Tuesday, February 4, 2014 8:11 PM
    Tuesday, February 4, 2014 6:28 PM
  • There isn't a way to hook into that event in a supported way - you would need to trigger this code from a supported event like a field change on a form.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, February 5, 2014 1:28 AM
    Moderator