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.
[SalesForce] How to display a message when the trigger finishes
Related Solutions
Yes, you can absolutely accomplish this through an apex trigger.
If you dont have much Apex experience this link will really help to show you some of the best practices when writing triggers
http://wiki.developerforce.com/page/Apex_Code_Best_Practices
As far as the trigger you are looking for, something like this should work
trigger Opportunity_Trigger on Opportunity (after insert) {
set<Id> oppIds = new set<id>();
for(Opportunity o : trigger.new){
if(o.isWon && !trigger.oldMap.get(o.Id).isWon){
oppIds.add(o.Id);
}
}
list<Opportunity> myOpps = [Select Id, Name, StageName, OwnerId, AccountId, CloseDate, Amount
From Opportunity
Where Id In : oppIds];
list<Contract> newContracts = new list<Contract>();
for(Opportunity o : myOpps){
//Add whatever information from the opp to the contract that you want here
myContracts.add(new Contract(
AccountId = o.AccountId,
//add more contract attributes here
));
}
insert newContracts;
}
While this should be a fully functioning trigger, and should work fine. Once you get comfortable writing triggers in this fashion, you might want to look into using another besdt practice for writing triggers, which is keeping the logic of the trigger outside of the trigger itself. There are many frameworks out there for this. Here are just a few
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
http://www.tgerm.com/2012/01/salesforce-apex-trigger-template.html
http://wiki.developerforce.com/page/Apex_Enterprise_Patterns_-_Domain_Layer#Trigger_Handling
http://www.embracingthecloud.com/2013/09/06/SalesforceApexWrapperClass.aspx
These are great resources, but I would make sure you are more comfortable with Apex code before trying to jump into these.
I didn't see any checking of closed won in your code, so I added the check to the SOQL query for the opportunity below. The try catch around the SOQL query will prevent any contracts from being created if the opportunity is not closed won. Also, you weren't handling the attachment cloning in bulk, so I adjusted that as well.
public with sharing class CreateContract {
Opportunity opp;
public CreateContract(ApexPages.StandardController stdController) {
opp = (Opportunity)stdController.getRecord();
}
public PageReference Create() {
PageReference pageRef = null;
try{
//If opp was not closed won, then the query will throw a SOQL exception and no contracts are created.
Opportunity opp = [SELECT Id, AccountId, Amount, CloseDate, StageName FROM Opportunity WHERE Id=:opp.Id And IsClosed = true and IsWon = true];
//Create the contract
Contract c = new Contract(
AccountId = opp.AccountId
,ContractTerm = 12
,StartDate = opp.CloseDate
,OwnerExpirationNotice = '30
,Amount__c = opp.Amount
);
insert c;
//Activate - does this need to be a separate DML transaction?
c.Status = 'Active';
update c;
// Copy attachments
//Better to do this DML in bulk... so created a list of new attachments
List<Attachment> newAttachments = new List<Attachment>();
for (Attachment att: [SELECT Id, Name, Body FROM Attachment WHERE ParentId=:opp.id]) {
newAttachments.add(
new Attachment(
ParentId = c.Id
,Name = att.name
,Body = att.body
)
);
}
insert newAttachments;
pageRef = new PageReference ('/'+c.id);
pageRef.setRedirect(true);
}catch(QueryException e){
//Added example of how to represent an Apex Error in Visualforce using a CustomLabel (Setup->Create->Custom Label)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,Label.Could_Not_Find_Closed_Won_Opp));
}catch(DMLException e){
//Error handle not being able to insert Contract or its related attachments
}
return pageRef;
}
}
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:
With a given controller:
Drop this page onto the layout, and it will show an alert.
MessageShown__c
is a checkbox field, defaulted to false, andContract__c
is a lookup that should be populated by the trigger.