is there any option to convert an opportunity object into a new contract standard object (and copy some information from the opportunity into the contract)? Is that possible using apex triggers (i.e. when oportunity status is equal to Closed/Won –> Create new Contract object?
[SalesForce] Convert Opportunity into Contract
Related Solutions
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.
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
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
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://krishhari.wordpress.com/2013/07/22/an-architecture-framework-to-handle-triggers-in-the-force-com-platform/
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.