The requirement is if the current user same as the owner of Appointment record, display the /Close Appointment/ ribbon button, other wise hide it.
// Hide/display Appointment ribbon close button with condition
function hideAppointmentCloseBtn(primaryControl) {
debugger;
var executionContext = primaryControl;
var formContext = executionContext.getFormContext();
var userSettings = Xrm.Utility.getGlobalContext().userSettings; // userSettings is an object with user information.
var currentUserName = userSettings.userName; // The user's name
var appointmentId = formContext.data.entity.getId().replace(/{/, //).replace(/}/, //);
Xrm.WebApi.retrieveRecord(/appointment/, /appointmentId/, /?$expand=owninguser($select=fullname/).then(
function success(result) {
const owner = result.owninguser.fullname
if (owner === currentUserName) {
return true;
} else {
return false;
}
},
function(error) {
console.log(error.message);
// handle error conditions
}
);
}
Tried million of time, It was not working. Am I missing something? Could you please advice?