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
Vishnu7700Vishnu7700 

Opportunity Trigger

Hi,

I'm trying to trigger to 'Create a new contract once the opportunity reachs the Stagename = Closed/won'

Tried with logic as below but it throws complie errors as Initial term of field expression must be a concrete SObject: LIST<Contract> at line 7

 

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.old){
if(opp.StageName == 'Closed/Won'){
conrtlist.Account = 'accid.name';
conrtlist.Status = 'Draft';
conrtlist.StartDate = Today();
conrtlist.ContractTerm = '6';
opp.add(conrtlist);
}
update opp;
}
}

 

Can any one please correct me where i'm worng?

 

Thanks & Regards

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

In the page it is  showing. Make it insert and try.

 

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 != NUL){
con .AccountId = opp.AccountId;

}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}

}

if(conrtlist.size() > 0){
insert conrtlist ;

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

souvik9086souvik9086

Try like this

 

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 != NUL){
con .AccountId = opp.AccountId;

}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}
upsert conrtlist ;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Vishnu7700Vishnu7700

Hi Souvik9086,

Corrected my trigger as u said and try to compile.It is sucessfully complied but hope trigger funcationality was not working.

i.e When a opportunity stagename = closed won an contract should be created but as now contract was not createing when a opportunity stagename = closed won.

Can you help me in this?

 

Regards,

Vishnu7700

souvik9086souvik9086

Change like this

 

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 != NUL){
con .AccountId = opp.AccountId;

}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}

}

if(conrtlist.size() > 0){
upsert conrtlist ;

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Vishnu7700Vishnu7700

Hi,

While updateing opportunity system throws error as below

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreatecontractonOppstageclosewon caused an unexpected exception, contact your administrator: CreatecontractonOppstageclosewon: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.CreatecontractonOppstageclosewon: line 19, column 1

souvik9086souvik9086

In the page it is  showing. Make it insert and try.

 

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 != NUL){
con .AccountId = opp.AccountId;

}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = 6;
conrtlist .add(con );
}

}

if(conrtlist.size() > 0){
insert conrtlist ;

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
Vishnu7700Vishnu7700

Thanks alot Souvik9086....

My problem was resolved.