1

I have a java script button. It alerts the user with some message ‘Invalid operation please contact system admin’.

New requirement, I need to display admin name as link on pop window after the message. When they click the link, it should take them to System Admin chatter page on a new tab. New message something like this : ‘Invalid operation, please contact system admin: Razu’

When they click Razu, it must open up a chatter page of razu. System admin name is dynamic value.

I just gave shot with below code. It is not working. Please help.

var URL = "/" + "{!User.Id}" ; window.alert(Invalid operation please contact system admin:’ + URL);

Changed the code based on input as below. It is not going to user page when I click OK on popup window..I tried the window.location.href also.. it is not working..

if (window.confirm('Invalid operation, please contact system admin. \nPress Yes to navigate to Admin Chatter Page.' + "{!$User.Id}")) {

window.location = '/' + "{!$User.Id}";

} }

1

You can use a confirm dialog with 'OK/Cancel' buttons instead of URL Link:

if (window.confirm('Invalid operation, please contact system admin. \nPress OK to navigate to Admin Chatter Page.'))
{
    //User clicked 'OK'
    window.location = '/' + '{!User.Id}';
}else{
    //User clicked 'Cancel'
}

It is not possible to add links inside an alert dialog. If you really want to place a clickable URL inside a dialog I would suggest to use a more advanced dialog available with jQuery - http://jqueryui.com/dialog/

EDIT

If you also do not know how to find your System Administrator user then look at the code below on how to define a controller for your VisualForce page that will get you that user:

public class AdminController {

    public User AdminUser {get; set;}

    public AdminController()
    {
        List<User> adminUsers = [SELECT Id, FirstName
                     FROM User 
                     WHERE User.Profile.Name = 'System Administrator' AND User.isActive = true];

        if(adminUsers.size()>0)        
            AdminUser = adminUsers[0];
    }

}

And then in your VisualForce page you assign that controller either as <apex:page extensions='AdminController'> if you already have assigned controller for that page, or if you don't have assigned controller you assign it as <apex:page controller='AdminController'> and then your JavaScript code would look like this:

if (window.confirm('Invalid operation, please contact system admin {!AdminUser.FirstName}. nPress Yes to navigate to Admin Chatter Page.'))
            {
                //User clicked 'OK'
                window.location = '/' + '{!AdminUser.Id}';
            }else{
                //User clicked 'Cancel'
            }

Note that in adminUsers query inside AdminController you might not want to get the first admin that query returns. You may want to add additional query filter to only return specific admin, etc.

| improve this answer | |
  • Thanks. It doesn;t diplay the button 'Yes' or 'No' on Pop up window. – Razu Nov 4 '15 at 16:46
  • Yes, my bad, it displays Ok and Cancel instead. I edited my answer few minutes ago. Does that work for you? Also, see my edit for other ways to add url to dialogs. – smukov Nov 4 '15 at 16:54
  • Thank you. Please look at my code.. it is not going to my chatter pager at all.. rather it staying at current page.. if (window.confirm('Invalid operation, please contact system admin. \nPress Yes to navigate to Admin Chatter Page.' + "{!$User.Id}")) { window.location = '/' + "{!$User.Id}"; } } – Razu Nov 4 '15 at 18:30
  • Does {!$User.Id} returns correct Admin ID value? Try hardcoding this line like: window.location = '/' + '00524000001at6Y'; (just use some user Id from your org) and see if it will navigate to it when you click OK on dialog. Or try with window.location = 'http://www.google.com';, and if it navigates to google.com after OK is pressed then your `{!User.Id}' is not evaluating correctly. – smukov Nov 4 '15 at 18:52
  • Also, add a screenshot of your current popup dialog to your question, that will help with debugging this issue. And finally, {$User.Id} will return your ID, not the admin ID. That expression always returns the ID of the currently logged in user that is browsing the page. – smukov Nov 4 '15 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.