taarskog / crm-powerbi-viewer

Embed tiles and reports from Power BI into Dynamics CRM Forms and Dashboards.
http://crm-powerbi-viewer.heiigjen.com/
MIT License
27 stars 10 forks source link

Unhandled Promise rejection: Failed to execute 'postMessage' on 'Window' #34

Closed LemonGeu closed 6 years ago

LemonGeu commented 6 years ago

Hello,

I'm using your powerbi viewer but since a couple of days I get this error message. I'm use a javascript to filter out a specific account. I can still see the report but it doesn't get filtered anymore.

Any solutions? filtercustomer

Kind regards,

Lemon

christinaa7 commented 6 years ago

I have the same issue.

Regards, Christina

taarskog commented 6 years ago

Hi Lemon and Christina,

Can you tell me:

  1. Is it D365CE on-premises or Online
  2. What version of D365CE...
  3. and crm-powerbi-viewer are you on?
  4. Did it start happening after a change to the javascript and/or did you modify the form where it is embedded?
christinaa7 commented 6 years ago

Hello taarskog,

thank you for your quick response.

  1. D365 Online
  2. 9.0.2.1470
  3. first we had the version 0.5 and I updated to version 1.1.0
  4. I don't know when it starts, but we changed nothing neither JavaScript nor a form.
taarskog commented 6 years ago

Thanks Christina,

I am able to reproduce the issue. Will investigate and hopefully have an update later today or sometime this Weekend.

-Trond

LemonGeu commented 6 years ago

Hello Trond,

First case

  1. D365 Online
  2. 9.0.2.1470
  3. version 1.0.0
  4. Didn't change anything.

Seconde case

  1. D365 Online
  2. 9.0.2.1468
  3. version 1.1.0
  4. Didn't change anything.

Looking forward to your response. Thanks alot!

Lemon

jamesrus1986 commented 6 years ago

Hi Taarskog,

Here to report the same issue.

  1. On-premise.
  2. 8.2.2
  3. V1.1.0
  4. No changes to implementation of solution config, this started today.

Issue reproduceavle in Chrome > latest , but not Firefox.

Error is the same as above, except this element “Cannot read property: ‘isDeprecated’ of undefined could not be cloned”.

Error occurs at the setting filters step.

Many thanks, James

taarskog commented 6 years ago

Looks like the Power BI team may have introduced a bug (or "feature" ;-))

In my tests I initially tried to set a filter on the event pagechanged. As you I also got the promise rejection.

The fix that worked for me was to add an empty call to setFilters on the event loaded. The call does not need to contain an actual filter but it seems to initialize objects that are required when setting filters on pageChanged.

Appreciate if you can help verify by adding the following to your code (adjust names etc. to match your code):

Samples.Filters.filterOnUser = function(report) {
   report.on("loaded", function(event) {
      report.setFilters([]);

      ... Original code triggering on pageChanged  ...
   });
}

UPDATE (after posting): Added empty array on call to setFilters. Se details a few comments down

LemonGeu commented 6 years ago

Hello Trond,

I tried to setFilters on loaded event. But for the v1.0 and the v1.1.0 version I get same errors. Also if I change a page afterwards I get the first error message again since the page is already loaded at that point.

setfilter filter

What should I change in the script?

Lemon

taarskog commented 6 years ago

Hi Lemon,

Did you clear the cache and increase the script file version in the config file (e.g. ?ver=2)? Suggest you also add a console.log right after setting the empty filter to verify that you are executing the most recent code changes.

-Trond

LemonGeu commented 6 years ago

Hi Trond,

I did clear cache + tried incognito browser. Also from what I can see the '?ver=x' isn't needed at this moment. But I did use it now with still no succes. I get "empty filter" in the console log written by console.log.

Lemon

taarskog commented 6 years ago

Hi Lemon,

Thank you for testing. Unfortunately I made a mistake Yesterday when I provided the code above. The empty call to setFilters require an empty array: report.setFilters([]);.

Though I believe using report.removeFilters(); may be a better function to use. Both approaches are working in my environments.

image

-Trond

LemonGeu commented 6 years ago

Hi Trond,

removeFilters() did indeed fix the errors. The scripts loads and triggers the pageChanged.

At this moment it doesn't filter my first page (that is not changed) before it did it. I didn't manage to get it working on page load yet. Do you have any idea how to trigger to get my first loaded page filtered aswell?

Anyways alot of thanks for the quick support!

Lemon

christinaa7 commented 6 years ago

Hello Trond,

thank you for your prompt solution. It works by changing the report page, but not on initial load. Before this bug/feature (we will never know) it worked fine. Maybe is there antoherway to trigger it?

Or is there an error in my code?

Samples.Filters.filterOnAccount = function (report) {
    report.on("loaded", function(loadedEvent) {
        console.log("Report loaded");

        console.log("Remove filters on report");
        report.removeFilters();

        report.on("pageChanged", function(event) {
            let page = event.detail.newPage;
            console.log(`Page changed to '${page.displayName}' (${page.name})`);

            var firma = window.parent.Xrm.Page.data.entity.getId();
            firma = firma.replace('{', '').replace('}', '');

            console.log("Firma - " + firma);        

            if (page.displayName === "Produktauswertung" || page.displayName === "Jahresauswertung") {
                const filter = {
                    $schema: "http://powerbi.com/product/schema#basic",
                    target: {
                        table: "accounts",
                        column: "accountid"
                    },
                    operator: "In",
                    values: [firma]
                };

                page.setFilters([filter]);

            }
        });

    });
}

Thank you so much for your help.

Christina

taarskog commented 6 years ago

Lemon and Christina,

Thanks for providing feedback. Glad to hear it now works.

Regarding setting the initial filter the following seems to work (not sure if it is the right approach though). I'm closing this issue now, but feel free to open another on initial load if my suggestion below does not work.

    let filteredSet = false
    report.on('rendered', () => {
        if (filteredSet) return;
        filteredSet = true;
        report.getPages()
        .then(function (pages) {
          // Retrieve active page.
          var activePage = pages.find(function(page) {
            return page.isActive
          });

          activePage.setFilters([filter])
            .then(function () {
                console.log("Page filter was set.");
            })
            .catch(function (errors) {
                console.error(errors);
            });
        })
        .catch(function (errors) {
            console.error(errors);
        });
    });
christinaa7 commented 6 years ago

Hello Trond,

thanks a lot for you code. Everything is working fine now. It's fantastic to get such a great support of an amazing product.

Christina

SergioRota commented 6 years ago

Hi Trond and Christina,

I have the same issue (filtering on initial load).

Where have I to insert the code:

let filteredSet = false
    report.on('rendered', () => {
        if (filteredSet) return;
        filteredSet = true;
        report.getPages()
        .then(function (pages) {
          // Retrieve active page.
          var activePage = pages.find(function(page) {
            return page.isActive
          });

          activePage.setFilters([filter])
            .then(function () {
                console.log("Page filter was set.");
            })
            .catch(function (errors) {
                console.error(errors);
            });
        })
        .catch(function (errors) {
            console.error(errors);
        });
    });

in my JS:

window.Samples = window.Samples || {};
Samples.Filters = Samples.Filters || {};

Samples.Filters.filterOnAccount = function (report) {

    report.on("loaded", function (loadedEvent) {
        report.removeFilters(); // For some reason this needs to be done before setting page filters (August 2018)

        report.on("pageChanged", function(event) {
            let page = event.detail.newPage;
            console.log(`Page changed to '${page.displayName}' (${page.name})`);

            {
var accId = window.parent.Xrm.Page.data.entity.getId();
accId = accId.replace('{','').replace('}','');               
const filter = {
                    $schema: "http://powerbi.com/product/schema#basic",
                    target: {
                        table: "accounts",
                        column: "accountid"
                    },
                    operator: "In",
                    values: [accId]
                };

                page.setFilters([filter]);
            }
        });
    });
}

Thanks Sergio