4

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.

5

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.

  • Hello sfdcfox, I have use some code similar to yours but when the trigger is executed it shown an error message telling that de MessagesShown__c field is read-only. I have checked the Field-Level security for this custom field and the read-only option is unchecked for all the profiles. Where is the root cause of the error?Thanks – Guillermo Jan 8 '14 at 9:18
  • @Guillermo Not sure how I missed your comment, but I think we'd need a look at your code for this. Maybe you should consider submitting a new question? I'd be glad to take a look at it for you. – sfdcfox Feb 6 '14 at 19:28
  • Hello sfdcfox, I have already solved this issue, but anyway, thanks for your help. – Guillermo Feb 10 '14 at 12:04

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.