0

I am trying to figure out how to click a button on the D365 ribbon. The button will refresh the page and i am going this route because ultimately i want to refresh all of the elements on the page. I have tried accessing via query selector with no luck

document.querySelector("#rr_jobprofile\\|NoRelationship\\|Form\\|Mscrm\\.Modern\\.refreshCommand72 > button").click();

D365 Ribbon Button

html for the button

<button aria-label="Refresh" aria-hidden="true" title="Refresh" tabindex="-1" data-id="rr_jobprofile|NoRelationship|Form|Mscrm.Form.rr_jobprofile.RefreshModernButton" data-lp-id="Form:rr_jobprofile-rr_jobprofile|NoRelationship|Form|Mscrm.Form.rr_jobprofile.RefreshModernButton" type="button" class="pa-ak pa-kx pa-go pa-ep pa-aj pa-om pa-at pa-sx pa-sy flexbox"><span class="pa-az pa-ah pa-a pa-hh "><span class="pa-ho pa-hj pa-st pa-cd pa-bd pa-a pa-at "><img src="/uclient/resources/images/Refresh.svg?v=1.4.2043-2012.2" alt="" title="" class="pa-oh pa-cg pa-bd pa-cc "></span><span aria-label="Refresh" tabindex="-1" class="pa-hj pa-bd pa-st pa-v pa-e pa-cm pa-oz pa-cl ">Refresh</span></span></button>
1
  • 2
    There is already button provided by crm for refresh, user can manually click that. If you want to refresh your page using javascript, Microsoft provided method, you do not have to use dom object. Look at this docu Commented Feb 4, 2021 at 6:26

3 Answers 3

1

Really really really recommend against manipulating or navigating the DOM. There are methods in the formContext.Controls collection to refresh any control on the page that requires it, or the page itself. Refreshing an HTML web resource is a little less obvious, but I've had good success using the getSrc() and setSrc() functions of the control. This function (not mine I got it on some blog years ago and added it to my toolbox) works very well, and will work both on the form (e.g. on load or on change) and from the ribbon.

function refreshWebResource(executionContext, WebRrscName) {    
    var _crmForm = executionContext.getFormContext();
    var webResource = _crmForm.getControl(WebRrscName);
    if (webResource != null) {
        var webResourceSrc = webResource.getSrc();
        webResource.setSrc(null);
        webResource.setSrc(webResourceSrc);
    }
}

Code's original source: https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/232589/refreshing-the-web-resource-in-dynamics-365

Sign up to request clarification or add additional context in comments.

Comments

0

Try

document.querySelector('[data-id="rr_jobprofile|NoRelationship|Form|Mscrm.Form.rr_jobprofile.RefreshModernButton"]').click()

Selectors for data attributes use attribute selectors syntax instead of starting with a tag name or characters # or .. For example

  • button[data-id=xxxx] selects a button whose data-id attribute value is exactly xxxx.

  • button[data-id|=xxxx] selects a button whose data-id attribute value starts with xxxx`.

    `

2 Comments

Make sure you run it after page is loaded. window.onload=function () {document.querySelector('xxxyyyzzz').click()}. If it's still null, then you need to find out your a static attribute for the query selector to identify the element. Refresh your page a few times and find the common part of the data-id which shouldn't change after refreshing the page.
Don't navigate the DOM for CRM. That attribute selector you're referencing could change at any time without warning. There's a very good reason accessing the DOM is not supported.
0

Change button title name to whatever you want and also change the name of the check button or any button on which you want the action to happen. This will work in CRM as the button might not always appear in any resolution so the code makes the button work even if it's not visible in the form:

 function clickconnectsequence() {
    var buttons = window.parent.document.getElementsByTagName('button');
    console.log(buttons)
    var xdata="noluck"
    var addInfo = Xrm.Page.getAttribute("tickbox").getValue();

    if (addInfo && addInfo == 1) {
        for (let i = 0; i < buttons.length; i++) {
            let button = buttons[i];
            if (button.title == "Connect sequence"){
                xdata="Connect sequence";
                console.log(button.id);
                window.parent.document.getElementById(button.id).click();
            }  
        }

    if (xdata!="Connect sequence") {
        for (let i = 0; i < buttons.length; i++) {
            let button = buttons[i];
            if (button.title == "More commands for Lead") {
                console.log(button.id);
                window.parent.document.getElementById(button.id).click();
                var buttons = window.parent.document.getElementsByTagName('button');
                for (let i = 0; i < buttons.length; i++) {
                    let button = buttons[i];
                    if (button.title == "Connect sequence"){
                        xdata="Connect sequence";
                        console.log(button.id);
                        window.parent.document.getElementById(button.id).click();        
                    }
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.