Reputation: 41
I have created a ribbon button on the opportunity entity in CRM 2013 and have used a custom enable rule to display the button only on one form from the two available (admin, sales).
I am able to see the button on web on the right form (admin), but it doesn't seem to be visible on Outlook client. I am using the following JavaScript to enable/disable the button and for some reason it looks like the JS is always returning false when called from Outlook.
I am using SetTimeout since Outlook seems to be taking time getting the objects and throwing an error if that's not used.
Is there a better way to enable the button on one form and not the other?
function EnableRibbonButton()
{
var client = Xrm.Page.context.client.getClient();
if(client == "Web")
{
var form = Xrm.Page.ui.formSelector;
if(form != null)
{
var formItem = form.getCurrentItem();
if(formItem != null)
{
var formId = formItem.getId();
if(formId == "ADMINFORMGUID")
{
return true;
}
else
{
return false;
}
}
}
}
else if(client == "Outlook")
{
var form = getForms();
var formItem = getFormItem(form);
var formId = getFormID(formItem);
if(formId == "ADMINFORMGUID")
{
return true;
}
else
{
return false;
}
}
}
function getForms()
{
var form = Xrm.Page.ui.formSelector;
if(form != null)
{
return form;
}
else
{
window.setTimeout("getForms()", 100);
}
}
function getFormItem(form)
{
var formItem = form.getCurrentItem();
if(formItem != null)
{
return formItem;
}
else
{
window.setTimeout("getFormItem("+form+")", 100);
}
}
function getFormID(formItem)
{
var formId = formItem.getId();
if(formId != null)
{
return formId;
}
else
{
window.setTimeout("getFormID("+formItem+")", 100);
}
}
Upvotes: 4
Views: 566
Reputation: 485
setTimeout
immediately returns (the timeoutID) and will execute the first argument at a later time, so you'll need some kind of callback mechanism to continue to the next step.
Furthermore, giving setTimeout
a string as first argument is a form of "implicit eval", which generally is undesirable.
Example of getForm
with callback:
function getFormSelector(callback) {
var formSelector = Xrm.Page.ui.formSelector;
if(typeof callback === 'function') {
if(!formSelector) {
window.setTimeout(
// Create closure for callback function.
function() {
getFormSelector(callback);
},
100 // milliseconds
);
return;
}
callback(formSelector);
return;
}
return formSelector;
}
Upvotes: 0