1

Looking to add a trigger to prevent an "Approved" Quote from being deleted but at other stages you can delete the quote. The other stages are "Not Submitted", "Pending", "Rejected", and "Recalled". Field name is Approval Stage. Any suggestions much appreciated?

I tried this trigger below but does not work but an error message is displayed at any stage and prevents deleting of the record. Hoping for just when Approval Stage = Approved

What I have so far.

<apex:page standardController="Quote" action="{!IF(Quote.Approval_Stage__c == 'Approved', NULL, URLFOR($Action.Quote.Delete, $CurrentPage.parameters.id, [retURL='/home/home.jsp'], TRUE))}">
    <apex:pageMessage title="Unauthorized" summary="The Quote is an {!Quote.Approval_Stage__c } state and hence cannot be deleted!" severity="ERROR" strength="3">
    </apex:pageMessage>
</apex:page>
1
  • I presume this VF page is an override of the standard Delete button for Quote?
    – cropredy
    May 9, 2016 at 19:36

1 Answer 1

0

You can implement the following trigger. Using a Custom Label to contain error text makes it configurable, so you can change it without additional deployments.

Class

public with sharing class QuoteServices
{
    public static void validateNotApproved(List<Quote> records)
    {
        for (Quote record : trigger.new)
        {
            if (record.Approval_Stage__c == 'Approved')
                record.addError('Replace this text with a Custom Label');
        }
    }
}

Trigger

trigger Quote on Quote (before delete)
{
    if (trigger.isBefore)
    {
        if (trigger.isDelete)
        {
            validateNotApproved(trigger.new);
        }
    }
}
7
  • Apex Class has an error from line 8, column 33 at the word Label
    – Hutch
    May 9, 2016 at 18:55
  • Missed an = character in the comparison.
    – Adrian Larson
    May 9, 2016 at 18:56
  • = sign goes before ?
    – Hutch
    May 9, 2016 at 19:16
  • It's in the updated code.
    – Adrian Larson
    May 9, 2016 at 19:17
  • not working - don't see the updated code
    – Hutch
    May 9, 2016 at 19:20

You must log in to answer this question.

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