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
stefano riviellostefano riviello 

Convert an opportunity to a contract

I would like to convert opportunities into contracts. once the opportuinità are closed won, I would like to convert with a key to opportunities in contracts have to write the code manually? if you like? many thanks in advance
salesforce mesalesforce me
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 ;

Stefano - You can do that without writing coding using Process builder.

User-added image

1. Select Opportunity object for the process builder
2. Set your bussiness citeria in the condition to enter to action [ If the record meets the criteria then it will enter into the process]
3. In the immediate action select 'create a record' action and choose 'contract' object
4. You can set the value for the contract object and also you can refer the opportunity value to contract object
5. Activate your process builder 

Now when you create/update opportunity record, if it meet the criteria then it will automatially create contract record. To  know more about the process builder, check this trailhead module which helps you to understand -
stefano riviellostefano riviello
I tried to write the code as written above it says url nonexistent.
instead as far as the building process, from which is activated because there is not in the settings, create. excuse my insistence I started yesterday I have yet to acclimatize well to Salesforce
stefano riviellostefano riviello
User-added image
salesforce me, that gives me the error with the code is this.
stefano riviellostefano riviello
sorry for the triple post , but this code or this building process is to create a button that allows me to convert ? sorry again