3

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.

6
  • You can run JavaScript on a standard page layout through embedded VF pages. But can you describe your actual requirement?
    – AslamK
    Jul 15, 2014 at 5:12
  • 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.
    – Varun
    Jul 15, 2014 at 5:15
  • 1
    Well in that case I think the Trigger seems to be the best solution, unless you decide to rewrite the page in Visualforce, which I think is not worth it.
    – AslamK
    Jul 15, 2014 at 5:19
  • @AslamK You should post that as an answer, a before delete trigger would definitely be the way to go.
    – Matt Lacey
    Jul 15, 2014 at 5:23
  • They're really asking about how to make it look "pretty"... there's things you can do in just a few lines of code to make it look pretty.
    – sfdcfox
    Jul 15, 2014 at 5:31

2 Answers 2

9

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

1

Any message to be displayed before delete of a record (i.e. preventing delete based on some criteria) , on a standard page layout, can be done only through a before delete trigger.

Unless you decide to rewrite the page in Visualforce, which I think is not worth the trouble for your requirement.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .