[SalesForce] How to display a message when the trigger finishes

I have created a trigger that creates a new Contract object when an opportunity stage is equal to 'Closed/Won'. I need that once the trigger finishes, without error, it display some success message to the user (like the error message but in case success trigger execution). It is possible to do that? I do not need a pop-up. I am working with standard page layouts.

Best Answer

No, it is not possible to display any success message from a trigger directly. You will need to use a Visualforce page if you want to display any type of message.

There is a similar idea on the IdeaExchange, but you're looking for an after-operation message, so this is probably a new idea that needs to be submitted.

Most likely, the easiest way to display a visual alert would be to embed a Visualforce page on the opportunity layout that checks if the contract has been created, and if the user has been notified, and if not, display a message.

The page might look like this:

<apex:page standardController="Opportunity" action="{!markread}" extensions="OpportunityExtension">
<script>
if({!Opportunity.Contract__c != null && NOT(Opportunity.MessageShown__c)}) {
    alert("Contract was successfully created");
}
</script>
</apex:page>

With a given controller:

public with sharing class OpportunityExtension {
    ApexPages.StandardController controller;
    public OpportunityExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }
    public void markread() {
        Opportunity o = (Opportunity)controller.getRecord();
        o.MessageShown__c = true;
        update o;
    }
}

Drop this page onto the layout, and it will show an alert. MessageShown__c is a checkbox field, defaulted to false, and Contract__c is a lookup that should be populated by the trigger.

Related Topic