PaulNieuwelaar / notifyjs

Other
31 stars 9 forks source link

unable to call the function Notify.Add() #5

Closed sureshkumarsatti closed 6 years ago

sureshkumarsatti commented 6 years ago

Hello Paul,

I am a complete beginner with javascripts. I am trying use your notify.js on forms to display a notification and but it dosent seem to be working. my form properties is attached for your reference. Can you please help me understand if there is anything wrong in reference? I have tested my custom javascript and it works fine when i use the regular CRM notification function, but it fails when i use Notify.add(). Any kind of suggestion will be very helpful. Hoping to hear from you. Thank you. image

PaulNieuwelaar commented 6 years ago

Hi, your form properties looks fine. Can you post your function calling into notify?

sureshkumarsatti commented 6 years ago

Thank You Paul for your reply. The function is as mentioned below, i am making the call to Notify.add() method on line 54

function activityNotification() { debugger; countActivity("email"); countActivity("appointment"); }

function countActivity(activityType) {
var activityCount =0;
var activityFetchXML = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">"+ "<entity name=\""+activityType+"\">"+ "<attribute name=\"activityid\" />"+ "<filter type=\"and\">" + "<filter type=\"and\">" + "<condition attribute=\"statecode\" operator=\"in\">" + "0"+ "3" + ""+ "<condition attribute=\"createdon\" operator=\"last-x-weeks\" value=\"1\" />" + "<filter type=\"or\">" + "<condition attribute=\"new_notification1\" operator=\"eq-userid\" />" + "<condition attribute=\"new_notification2\" operator=\"eq-userid\" />" + "<condition attribute=\"new_notification3\" operator=\"eq-userid\" />" + "<condition attribute=\"new_notification4\" operator=\"eq-userid\" />" + "" + "" + "" + "" + "";

var encodedFetchXML = encodeURIComponent(activityFetchXML);

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + activityType + "s?fetchXml=" + encodedFetchXML, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {

            var results = JSON.parse(this.response);

            activityCount = results.value.length;
            if(activityCount > 0)
            {
                switch (activityType) {
                                          case "email":
                        \\Xrm.Page.ui.setFormNotification("You have " + activityCount + " " + activityType + "s in open status!", "INFO", "1");
                        Notify.add("You have 1 email pending", "QUESTION", "test",null,30);
                        break;
                    case "appointment":
                        Xrm.Page.ui.setFormNotification("You have " + activityCount + " " + activityType + "s in open status!", "INFO", "2");
                        break;

                }
            }

        }
    }

};

req.send();
return;

}

PaulNieuwelaar commented 6 years ago

Hi, I think some of your code got stripped out when you posted it. I can see the Notify.add call looks OK, and from what I can see, there's issues with the XML having unescaped quotation marks inside the strings.

E.g. "<condition attribute="new_notification1" operator="eq-userid" />" +

Should be "<condition attribute=\"new_notification1\" operator=\"eq-userid\" />" +

Or "<condition attribute='new_notification1' operator='eq-userid' />" +

Although if you post the whole code again, and use the "Insert Code" button of the github editor, it should allow me to see everything. Because the issues with the quotes should've broken everything, which doesn't explain why the OOTB notification works in the same example.

Paul

sureshkumarsatti commented 6 years ago

Thank you very much for looking into this. My bad, i had a syntax issue which was making my code fail. Fixed it and the notifications are working fantastic. Thank you very much again!