Forum Discussion

DanielRamos's avatar
DanielRamos
Frequent Visitor
3 years ago
Solved

Embedding Dashboard on Salesforce throws error when using RLS

Please, help. I'm using SalesforceAppOwnsDataEmbedding to display a Dashboard in Salesforce, everything works well, inclusively I'm able to send Report Level Filters. However, if Row Level Security...
  • DanielRamos's avatar
    3 years ago

    Finally achieved this embedding!!! ðŸŽ‰

    Sharing solution in case someone's struggling with something similar.

     

    My scenario was; I'm using a POST call to endpoint API GenerateTokenInGroup, my Dahsboard uses RLS, so it is required to include "identities" in the "Request Body" json, seems like I was not including all the required data on each section of the HttpRequest.

     

    Important!

    • Now I included Content-Type: application/json to the header of request
    • Request Body is now composed with accessLevel: View, and identities using Service Principal Id (Application Client Id) as the username.
    • I created a new Role with no filters and assigned to Service Principal
      • (This allows me to apply ReportLevelFilters for dynamically filtering from Salesforce)
    • Provided the required API permissions on App Registration (Service Principal) on Azure.
      • Delegated Permissions, Power Bi Service; Report.ReadWrite.All, Dataset.ReadWrite.All, Content.Create

    Full HttpRequest code

    // Call to Power BI Service API to get embed token for report        
            HttpRequest reqGetEmbedToken = new HttpRequest();
            reqGetEmbedToken.setMethod('POST');
            String urlGetEmbedToken = 'https://api.powerbi.com/v1.0/myorg/groups/' + WorkspaceId + '/reports/' + ReportId + '/GenerateToken';
            reqGetEmbedToken.setEndpoint(urlGetEmbedToken);
            System.debug('setEndpoint = '+urlGetEmbedToken);
            reqGetEmbedToken.setHeader('Authorization', 'Bearer ' + access_token);
            reqGetEmbedToken.setHeader('Content-Type', 'application/json');
            system.debug('setHeader = '+reqGetembedToken.getHeader('Authorization'));
            reqGetEmbedToken.setBody(
                '{'
                    +'"accessLevel":"View"'
                    +',"identities":['
                        +'{"username":"1eac8b84-11a3-4f67-b308-c9ab0a7b8da0","roles":["noRlsFilters"],"datasets":["'+powerBiReport.datasetId+'"]}'
                    +']'
                +'}');
            System.debug('setBody = '+reqGetEmbedToken.getBody());
        
            HttpResponse responseEmbedToken;
            try {                        
                responseEmbedToken = http.send(reqGetEmbedToken);
                System.debug('http send successfully');
                system.debug(responseEmbedToken);
            } catch (Exception error) {
                system.debug('Error Catched');
                System.debug(error);
            }

     

    Response code is now 200 and Token is successfully generated,

    Dashboard is displaying and filtering is correctly applying.

     

    Hooray!