function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AndyPandyAndyPandy 

Custom Button on Custom Object Not Working

Hi All,

 

I have created a Custom Object within my Org, and I would like to have a Custom Button which, depending on the User's Role, will take them to one of three Report Folders, to view a selection of reports related to this Object.

 

I have used the following foruma, which saves without a problem, and when you check the Syntax returns no errors:

 

if ( 
{!$UserRole.Name} = "Central TM" || "Eastern TM" || "Ireland TM" || "Northern TM" || "South Central TM" || "South East TM" || "South West TM", 

"https://emea.salesforce.com/00l20000001nVUx", 

if ( 

{!$UserRole.Name} = "Central RBM" || "Eastern RBM" || "Ireland RBM" || "Northern RBM" || "South Central RBM" || "South East RBM" || "South West RBM", 

"https://emea.salesforce.com/00l20000001nVUs", 

"https://emea.salesforce.com/00l20000001nVV2" 

))

However, whenever I test the code it does not work - it comes back with an error message:

 

"URL No Longer Exists"

 

Moreover, if I copy+paste any one of the 3 URLs I have used in the above formula, it takes me to exactly the right place, so I know that the URLs are fine.

 

Can anyone let me know what is going wrong please?

 

Additionally (just a by-the-by if anyone can help), I can only seem to show this custom button within one of the List Views (which has to be selected after a user has navigated to my Custom Object tab) - is there any way for the button to be visible as soon as a user clicks on the Custom Object tab, without them having to select a List View first?

 

Thank you for any help you can provide,

 

Andy

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force
var userRole='{!$UserRole.Name}';

var reportUrl='';

if(userRole.indexOf('Central TM') != -1 || userRole.indexOf('Eastern TM') != -1  || userRole.indexOf('Ireland TM') != -1 || userRole.indexOf('Northern TM') != -1 || userRole.indexOf('South Central TM') != -1 ||  userRole.indexOf('South East TM') != -1 || userRole.indexOf('South West TM') != -1)
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}else if(userRole.indexOf('Central RBM') != -1 || userRole.indexOf('Eastern RBM') != -1 || userRole.indexOf('Ireland RBM') != -1 || userRole.indexOf('Northern RBM') != -1 || userRole.indexOf('South Central RBM') != -1  || userRole.indexOf('South East RBM') != -1 || userRole.indexOf('South West RBM') != -1 )
{
reportUrl='https://emea.salesforce.com/00l20000001nVUx';
}else
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}
window.location.href=reportUrl;

 

All Answers

AndyPandyAndyPandy

Just to follow-up on this... when I say "When I test the code" what I mean is, "When I click on the button" to take me to a report folder, it does not work...

 

Apologies for the confusion.

 

Andy

AndyPandyAndyPandy

Just another update - after some more online research it seemed that using the Javascript method was the way forward - so I created the following:

 

var ManagersURL = 'https://emea.salesforce.com/00l20000001nVV2'; 
var RSMURL = 'https://emea.salesforce.com/00l20000001nVUs'; 
var TMURL = 'https://emea.salesforce.com/00l20000001nVUx'; 

var ReportURL; 

if('{!$UserRole.Name}' == "Central TM" || "Eastern TM" || "Ireland TM" || "Northern TM" || "South Central TM" || "South East TM" || "South West TM") 

{ReportUrl = TMURL;} 

else if('{!$UserRole.Name}' == "Central RBM" || "Eastern RBM" || "Ireland RBM" || "Northern RBM" || "South Central RBM" || "South East RBM" || "South West RBM") 

{ReportUrl = RSMURL;} 

else 

{ReportUrl = ManagersURL;} 

window.open(ReportURL);

However, this is coming up with the same error.

 

I'm completely new to JS, so this could all be wrong - any thought?

 

Thanks again,

 

Andy

AndyPandyAndyPandy

Just in case it helps, whenever I click the button at the moment, the link in the address bar of the browser window always shows "https://emea.salesforce.com/undefined" but I'm still not sure what is causing the error.

 

Thanks,

 

Andy

b-Forceb-Force

Correct javascript button code

 

 

var userRole='{!$UserRole.Name}';

var reportUrl='';

if(userRole.indexOf('Central TM') != -1 || userRole.indexOf('Eastern TM') != -1  || userRole.indexOf('Ireland TM') != -1 || userRole.indexOf('Northern TM') != -1 || userRole.indexOf('South Central TM') != -1  userRole.indexOf('South East TM') != -1 userRole.indexOf('South West TM') != -1)
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}else if(userRole.indexOf('Central RBM') != -1 || userRole.indexOf('Eastern RBM') != -1 || userRole.indexOf('Ireland RBM') != -1 || userRole.indexOf('Northern RBM') != -1 || userRole.indexOf('South Central RBM') != -1  || userRole.indexOf('South East RBM') != -1 || userRole.indexOf('South West RBM') != -1 )
{
reportUrl='https://emea.salesforce.com/00l20000001nVUx';
}else
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}
window.location.href=reportUrl;

 

 

Thanks,

bForce

AndyPandyAndyPandy

Hi b-Force,

 

Thank you very much indeed for helping me with this.

 

I have copy+pasted your code, and tried my button again, but am getting the following error:

 

A problem with the OnClick JavaScript for this button or link was encountered:

Expected ')'

Can you help me to identify where I need to add this parenthesis?

 

Thanks,

 

Andy

b-Forceb-Force
var userRole='{!$UserRole.Name}';

var reportUrl='';

if(userRole.indexOf('Central TM') != -1 || userRole.indexOf('Eastern TM') != -1  || userRole.indexOf('Ireland TM') != -1 || userRole.indexOf('Northern TM') != -1 || userRole.indexOf('South Central TM') != -1 ||  userRole.indexOf('South East TM') != -1 || userRole.indexOf('South West TM') != -1)
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}else if(userRole.indexOf('Central RBM') != -1 || userRole.indexOf('Eastern RBM') != -1 || userRole.indexOf('Ireland RBM') != -1 || userRole.indexOf('Northern RBM') != -1 || userRole.indexOf('South Central RBM') != -1  || userRole.indexOf('South East RBM') != -1 || userRole.indexOf('South West RBM') != -1 )
{
reportUrl='https://emea.salesforce.com/00l20000001nVUx';
}else
{
reportUrl='https://emea.salesforce.com/00l20000001nVV2';
}
window.location.href=reportUrl;

 

This was selected as the best answer
AndyPandyAndyPandy

This works perfectly!

 

Thank you b-Force, much appreciated.

b-Forceb-Force

Nice to heard that, It works for you.

 

Happy coding.

 

Thanks,

bForce