Reputation: 2599
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();
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>
Upvotes: 0
Views: 2966
Reputation: 1
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();
}
}
}
}
}
}
Upvotes: 0
Reputation: 3229
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
Upvotes: 1
Reputation: 490
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`.
`
Upvotes: 0