[SalesForce] Popping Up Javascript Alert Box on Standard Page Layout

Is it possible to invoke javascript on a Standard Page Layout ?

I know we can write Javascript for Custom Button on a standard page layout but can we write javascript for object events (like delete etc) which will cause an pop up box to be displayed to the user on a standard page (non-VF).

We have a custom object "Transaction". Depending what Status this record is in we should have some sort of error mechanism thrown to the user when they try to delete it. I believe for "update" I can create a validation rule but I don't believe validation rule is an option since this is an "delete" event. Currently we are showing an error message via on delete trigger but I was wondering whether we can display a pop up alert box instead of the OOTB trigger error message.

Best Answer

Prior to Summer '14, you could use the sidebar loading trick to inject JavaScript, but with this feature being removed, you'll have to rely on another mechanism.

Personally, if you're really hung up on the delete button showing some sort of confirmation dialog, I would suggest you create a Visualforce override. This gives you control over what's shown. Such an override might look like this:

<apex:page standardController="Account">
<apex:form>
<apex:pageMessages />
<apex:pageMessage severity="confirm" summary="Are you sure?" detail="You are about to delete this account. Are you sure you want to do this?" />
<apex:commandButton action="{!delete}" value="Delete" />
<apex:commandButton action="{!cancel}" value="Cancel" />
</apex:form>
</apex:page>

Then, assign it to the Delete button as an override, and they'll see the screen and have to confirm their choice. You can show whatever you want here, as it is Visualforce.

Using a trigger would show an outright error, preventing deletion entirely, which may not be ideal, and overriding the entire detail page just to add a confirmation dialog is not desirable either, for a variety of reasons (mostly being a waste of time, and making maintenance more expensive in terms of time).

Response to Edit

Since you are trying to prevent deletes, use a Visualforce page that attempts to delete, catches any errors, and displays them in a pretty format if any errors occur. This will let you reuse what you've already written. Or, you could have the Visualforce page check the condition, and show the error instead of even attempting to delete, but this is duplicating code.

Edit

Code example that shows the design to use:

<apex:page standardController="Account" extensions="accountDelete" action="{!deleterecord}">
    <apex:pageMessages />
</apex:page>

And the extension:

public with sharing class accountDelete {
    apexpages.standardcontroller controller;
    public accountDelete(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    public pagereference deleteRecord() {
        try {
            delete controller.getRecord();
            return new pagereference('/home/home.jsp');
        } catch(exception e) {
            apexpages.addmessages(e);
        }
        return null;
    }
}

You can't call {!delete} directly because of the override, plus we're trying to catch the error, so you need a bit more code to support it.

Also, you'll want to spruce this up a bit, perhaps with a link back to the record, etc. Here's a screenshot of the code in action:

Can't Delete Record error in Visualforce

Related Topic