Alexey
Alexey

Reputation: 55

crm2011 plugin call js function

How can I run JavaScript function from appropriate web resource with a plugin in CRM2011.

I couldn't find any information on the Internet. Most of the resources describes how to trigger plugin from JS, but not opposite.

Here's JS code taht is copying the notes to the description field. When save is clicked. you can see that the data is correctly display in a description field. However if you press save and close and open form again the description field will be empty. I thought that the reason for that is that the JS executed after safe event but later tests descovered that it's false. Could someone point out an error in this JS code which cause that data is not saving? Or give a suggestion how's write a plugin which is retrieving data from related entity and writes it into field in the form. thanx

enter image description here

function copyNotes()
 {
// CLEAR DESCRIPTION FIELD
alert("JS");
    Xrm.Page.getAttribute("description").setValue('');
// GET ID OF THE CASE AND CLEAN IT AND GET URL for oData stuff
//THEN CALL RETRIEVE FUNCTION
    var caseID = Xrm.Page.data.entity.getId();
    caseID = caseID.replace('{', '').replace('}', '');
    var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
    ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
    retrieveRecord(caseID);
}
// CREATE AN HTTP REQUEST AND SEND IT
function retrieveRecord(Id) {

var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", ODataPath + "/AnnotationSet?$filter=ObjectId/Id" + " eq (guid'" + Id + "')", true);

retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function() {
// THIS HANDLES A CALLBACK 
        retrieveReqCallBack(this);
    };
    retrieveReq.send();
}

function retrieveReqCallBack(retrieveReq) {
    if (retrieveReq.readyState == 4 /* complete */  )
 {
        if (retrieveReq.status == 200) {
            //Success
            var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
// ITERATE THROUGH THE NOTES FOR THIS CASE
            for (var i = 0; i < retrieved.results.length; i++) {
// IF IS AN EMPTY FIELD ADD 'case details:'
                if (Xrm.Page.getAttribute("description").getValue() == null || Xrm.Page.getAttribute("description").getValue() == 'null') {
                    Xrm.Page.getAttribute("description").setValue('Case details:');
                }
                // BOF PARSE DATE
                var date = retrieved.results[i].CreatedOn;
                date = new Date(parseInt(date.replace("/Date(", "").replace(")/", ""), 10));
                // EOF PARSE DATE
                var newtext = "--------------------\r\n" + "Created by: " + retrieved.results[i].CreatedBy.Name + " - " + date + "\r\n" + retrieved.results[i].NoteText + "\r\n--------------------\r\n";
                var text = Xrm.Page.getAttribute("description").getValue() + "\r\n" + newtext;
                Xrm.Page.getAttribute("description").setValue(text);
            }
        }
    }

}

Upvotes: 2

Views: 2152

Answers (2)

Daryl
Daryl

Reputation: 18895

There is no supported way to call back to the client from the server from within the plugin. I'm also not aware of any unsupported way.

I don't think this question even makes sense. Plugin's only trigger when there has been a CRUD operation of some sort. Any CRUD operation triggered by the GUI will result in a refresh of the entity any way. You could perform an update via javascript and an Odata call, but then once the plugin has finished, you can run whatever javascript you'd like to run.

Upvotes: 3

Konrad Viltersten
Konrad Viltersten

Reputation: 39068

There's no (reasonable) way to do that.

The reason for that is the fact that plugin is a server-size executed thingy. It can't even assume there's a GUI. (Of course, we know there is but generally, a server-size code can't interact with the GUI directly).

JavaScript is client-side code and a client assumes a server. That's (roughly) why JS can call a plugin (although I wouldn't put it that way) but not the other way around.

I've never had a need of such an operation so I'm curious as to what your task is. Are you asking of pure, academic interest or is it a part of a design? Perhaps there's a better way to reach your goal?

Upvotes: 2

Related Questions