Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Opening a report from a custom ribbon button (9.0 Dynamics CRM)

(0) ShareShare
ReportReport
Posted on by 130

Hi everyone,

I'm trying to update some of our JavaScript for the 9.0 API changes and I'm stuck on trying to run a report from a custom button on the ribbon. 

This code works fine:

function (primaryControl) {

var formContext = primaryControl;
var globalContext = Xrm.Utility.getGlobalContext();

var urlOptions = { height: 600, width: 400 };

var reportName = "<MyReport>";
var entityType = "<EntityType>";
var entityId = TrimBrackets(Xrm.Page.data.entity.getId());
var reportGuid = "<ReportGuid>";

var serverUrl = globalContext.getClientUrl();
var reportUrl = serverUrl + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + reportName + "&id=%7b + reportGuid + "%7d&records=%7b" + entityId + "%7d&recordstype=" + entityType;

Xrm.Navigation.openUrl(reportUrl, urlOptions)

}

But the moment I try to introduce the replacement for Xrm.Page.data.entity.getId(); I get problems:

var entityId = TrimBrackets(formContext.data.entity.getId());

The above doesn't appear to work for me - no errors. 

We're using Microsoft Dynamics 365 9.0 online.

Any ideas?

Kind regards,


Matt

*This post is locked for comments

  • Suggested answer
    MattTaylor Profile Picture
    MattTaylor 130 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    So, for anyone interested it turns out that there was something wrong with one of the ribbons I was working with and the parameter from the command hadn't been saved. Good shout Wei Jie Fun, as it turns out was just a problem with the ribbon. 

    Just to clarify I knew how to open a report using JavaScript but there was a need to update our code for the client APIs that are deprecated as of version 9.0 - details here.

    Here's a quick example of the function I used in the end: 

    // Primary Control is passed through from the Ribbon Command
    openReportExample (primaryControl) {
    
        var formContext = primaryControl;
        var globalContext = Xrm.Utility.getGlobalContext(); 
    
        var urlOptions = { height: 600, width: 400 };
    	
        var entityType = encodeURIComponent(<YourEntityTypeHere>);
        var entityId = encodeURIComponent(formContext.data.entity.getId());
        var reportName = encodeURIComponent(<YourReportNameHere>);
        var reportGuid = encodeURIComponent(<YourReportGuid>); // Include curly brackets 
    
        var serverUrl = globalContext.getClientUrl();
        var reportUrl = serverUrl + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + 
    		reportName + "&id=" + reportGuid + "&records=" + 
    		"" + entityId + "&recordstype=" + entityType;
    
        Xrm.Navigation.openUrl(reportUrl, urlOptions);
    }
    You need to send the Primary Control parameter from your command on the ribbon - use the Ribbon Workbench.
    I hope this helps someone else some day.
  • Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    Hi..

    try this..

    mahadeomatre.blogspot.com/.../run-report-from-custom-ribbon-button.html

  • Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    Hi,

    I would suggest you to debug the JS. if  formContext.data.entity.getId() return you null, perhaps you can try below suggestion.

    stackoverflow.com/.../how-to-get-formcontext-in-ribbon-command-of-dynamics-365-9-0

  • MattTaylor Profile Picture
    MattTaylor 130 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    Thanks for the suggestions,

    Perhaps I didn't explain well but this is about using a custom button on the ribbon to open a report, therefore we're not passing in the execution context from the form as the first parameter but instead from the ribbon customisation.

    Details on how to do that are here: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions#form-and-grid-context-in-ribbon-actions

    So, to answer your question Pranav, the formContext comes from the primaryControl parameter and not the executionContext. 

    Goutam, your function looks neater than mine but it doesn't actually change the bit I need to change - the function currently works but in order to avoid the deprecated API I need to replace:

    var entityId = Xrm.Page.data.entity.getId();

    with 

    var entityId = formContext.data.entity.getId();

    For some reason formContext.data.entity.getId(); doesn't work when trying to use the context passed through the ribbon command. On the link I posted above Microsoft state: 

    "With a form ribbon, you can use the data.entity.attributes collection and the ui.controls collection to retrieve and set values for known fields."

    Which doesn't mention data.entity.getId();

    Anyone else tried this yet?

    Cheers,


    Matt

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    Try with this  , you can try with removing red highlighted if its not work.

    function OpoenReport() {
        var action ="run";
        var reportName ="<MyReport>"; // Replace Report Name 
        var id =  "<ReportGuid>";  //Replace Report Id
        var entityId = Xrm.Page.data.entity.getId().replace("{","").replace("}","");  // Trim brackets
        var entityType = "<EntityType>"; // Replae entity name
    
    
        var globalContext = Xrm.Utility.getGlobalContext();
        var serverUrl = globalContext.getClientUrl();
    
        var reportUrl = serverUrl + 
         "/crmreports/viewer/viewer.aspx?action=" +
         encodeURIComponent(action) +
         "&helpID=" +
         encodeURIComponent(reportName) +
         "&id=%7b" +
         encodeURIComponent(id) +
         "%7d"
        +"&records=%7b" +
         encodeURIComponent(entityId) +
         "%7d"+
        "&recordstype=" +
         encodeURIComponent(entityType)
        ;   
    
        var urlOptions = { height: 600, width: 400 };
        Xrm.Navigation.openUrl(reportUrl, urlOptions)
    }


    Please refer below article for more inormation.

    https://community.dynamics.com/365/b/softchiefmicrosoftdynamics365/archive/2017/09/06/how-to-open-report-by-using-url

  • PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Opening a report from a custom ribbon button (9.0 Dynamics CRM)

    Hi Can you share how you are getting the formContext ?

    get ExecutionContext.getFormContext()  like this and you should be fine.

    For Example check this link: docs.microsoft.com/.../getformcontext

    For more info: docs.microsoft.com/.../formcontext-data-entity

    Regards,

    Pranav

    If found useful, please mark the answer as verified

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans