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

Adding custom fields to bespoke code which creates Contracts from opportunities

ezdhanhussain created an amazing bit of code for me last week which automatically creates a Contract from an Opportunities, as soon as the stage is advanced to Closed Won.  For the benefit of others, as well as those kind people who might help me now, the code is below.


I now need to make some small improvements - specifically, to get the code to transfer over some custom fields from each opportunity.


The custom fields which we need to transfer over are these:


1. RESEARCH People (X1_RESEARCH_People__c)


3. RESEARCH site history (X3_RESEARCH_site_history__c)

4. RESEARCH likely issues (X4_RESEARCH_likely_issues__c)

5. RESEARCH Stakeholders (X5_RESEARCH_Stakeholders__c)

6. RESEARCH company other projects (X6_RESEARCH_company_other_projects__c)

Client Code (Client_Code__c)

Council where the scheme is (Council__c)

Date for 1st Public Exhibition (Date_for_1st_Public_Exhibition__c)

Date for 2nd Public Exhibition (Date_for_2nd_Public_Exhibition__c)

Date for Steve to Call (Date_to_Call__c)

Date for Submission (Date_for_Submission__c)

Details of Lead (Details_of_Lead__c)

First Meeting Report (First_Meeting_Report__c)

IPA Pitch Member 1 (IPA_Pitch_Member_1__c)

IPA Pitch Member 2 (IPA_Pitch_Member_2__c)

IPA Pitch Member 3 (IPA_Pitch_Member_3__c)

IPA Source of Lead (Indigo_Email__c)

Planning Committee Date Time (Planning_Committee_Date_Time__c)

Previous contact (Previous_contact__c)

Referer's main email address (Referersemail__c)

Region where the scheme is (Region__c)
Relevant Work (Relevant_Work__c)

Sales Notes (Sales_Notes__c)

Sector of the scheme (Sector__c)

URL where you found this lead (Is_there_a_URL_where_you_found_this_lead__c)


The code is:


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 :{
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 ;


Will greatly appreciate your advice and assistance.  I'm a newbie - didn't know what Salesforce was 10 weeks ago, but making good progress implementing it into the small ish company I'm working with.  Hopefully in a few months I'll be writing me own code and helping other people out on these boards.


Thanks in advance,




For each field you need mapped from the opportunity to the contract, you'll need to add a line of code such as the following:


con.X1_RESEARCH_People__c = opp.X1_RESEARCH_People__c

You'll want to place these lines of code above:


 conrtlist .add(con );


This of course assumes that you have set up the custom fields on the Contract object with the same API names used on Opportunity.


This is terrific - many thanks indeed!!