function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlexRitsonAlexRitson 

Code to convert an Opportunity into a Contract - difficult?

Hello,

 

Being a business that provides services, I need a third stage of the Salesforce process for when we have won the contract a the end of the Opportunity phase.  This will be for the period where we have won a contract for some business, and it would be great to have Salesforce workflow etc to ensure that we provide what we promised to provide to our customers.

 

It appears to me that Contracts would be the perfect tool, however there is apparently no way of transferring information such as quotations, sales notes, company names or contacts directly from a Salesforce Opportunity into a Salesforce Contract.

 

The people at Salesforce say this ought to be possible with some fairly simple Apex code, but I don't know where to start.  

 

This was clearly an issue back in 2010 - see http://boards.developerforce.com/t5/General-Development/New-to-Sales-force-Convert-Opportunity-to-Contract/td-p/163183 .Annoyingly, the link to the supposed "answer" no longer works.

 

Please help!

 

Best regards,

 

Alex.

Best Answer chosen by Admin (Salesforce Developers) 
ezdhanhussainezdhanhussain

Try this code.. If solved mark it as answer, so that it can be useful to others

trigger CreatecontractonOppstageclosewon on Opportunity (after insert , after update) {
Set<Id> accId = new Set<Id>();
List<Opportunity> opplist = new List<Opportunity>();
List<contract> conrtlist = new List<contract>();
for(Opportunity opp : Trigger.new){
if(opp.StageName == 'Closed Won'){
Contract con = new Contract();
if(opp.AccountId != NULL){
con .AccountId = opp.AccountId;
}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}
}
if(conrtlist.size() > 0){
insert conrtlist ;
}
}

 

All Answers

ryanjuptonryanjupton

Alex, from this and some of your earlier posts I would nicely recommend learning some basic Apex, SOQL and declarative development on our platform. A great place to start would be here http://www.salesforce.com/us/developer/docs/workbook/index.htm. I would also recommend reading these two blog posts: http://blogs.developerforce.com/developer-relations/2013/07/five-essentials-for-new-salesforce-platform-developers.html and http://blogs.developerforce.com/developer-relations/2013/06/advice-for-developers-new-to-the-salesforce-platform.html. Welcome to the platform, looking forward to seeing you around the community.

AlexRitsonAlexRitson

Dear Ryan,

 

Thanks very much for your post.  It's a great medium term solution and one which I shall certainly be following - I have made plans for this weekend involving the links you kindly provided.  My problem today is that there is no way I will learn enough by next week, when I need to have a solution in place. 

 

Other people have clearly had this problem before, so what I'm hoping is that someone will be able to share with me some code that I can adapt.  The Salesforce developers have promised to help me if I can give them something to start with.

 

Any offer of code that does something similar would be gratefully considered.  

 

Best regards,

 

Alex.

ezdhanhussainezdhanhussain

Try this code.. If solved mark it as answer, so that it can be useful to others

trigger CreatecontractonOppstageclosewon on Opportunity (after insert , after update) {
Set<Id> accId = new Set<Id>();
List<Opportunity> opplist = new List<Opportunity>();
List<contract> conrtlist = new List<contract>();
for(Opportunity opp : Trigger.new){
if(opp.StageName == 'Closed Won'){
Contract con = new Contract();
if(opp.AccountId != NULL){
con .AccountId = opp.AccountId;
}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}
}
if(conrtlist.size() > 0){
insert conrtlist ;
}
}

 

This was selected as the best answer
AlexRitsonDevAlexRitsonDev

Dear Hussein,

 

Thank you very much for this - will check out and come back to you shortly.

 

Best regards,

 

Alex.

AlexRitsonAlexRitson

Dear Hussein,

 

Thank you again for this code.  Absolutely first class!  I have also learned a lot in the process - maybe in a few months I'll be writing my first proper code.  

 

I'm going to post another topic requesting a slight modification - I need to be able to transfer the custom fields we have written in our opportunities overt to the contracts.  Hope very much you will be able to take a look.

 

Best regards,

 

Alex.

joe1.3950919854795088E12joe1.3950919854795088E12
How do I add this trigger to salesforce?
Pauline LabibPauline Labib
AlexRitson did you find a solution to do that? 
Amjad OmarAmjad Omar
I had the same need in my salesforce org; I created something similar but due to the sensitivity of the contracts I opted to create a salesforce flow that will gather the information from the opportunity and create the contract and the line items automatically through the flow. I prefered the flow over the trigger option because this allows for the user to always verify the contract information created.
mustapha L 1mustapha L 1
Hi Hussein,

I have used your code which is perfectly working, Many thanks.
Personaly i would go deeper and looking to link the fresh created Contract to the Opportunity who fired the trigger.
i have just add the following lines into you code, but seems to be not so easy :
if(conrtlist.size() > 0){
insert conrtlist ;

opp.ContractID=con .ID;
could you help please ?
thank you