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
rakesh kondarakesh konda 

Unable to create opportunity

Hello ,

I am trying to create an opportunity using triggers when an account is created .

i have executed  following code but i was unable to create the account
CODE:
trigger createopportunity on Account (after insert) {
list<opportunity> opp=new list <opportunity>();
for( account acc :trigger.new){
opportunity oppt=new opportunity();
oppt.Name = acc .Name;
oppt.Accountid = acc .id;
oppt.StageName= 'proposal';
 oppt.CloseDate = System.today() + 30;
 opp.add(oppt);
}
 if (opp.isEmpty() == false) {
 Database.update(opp);
        }
        }

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

please help me with your suggestions
Best Answer chosen by rakesh konda
Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

You need to Insert not Update the Opportunity list

Try this code :
 
trigger createopportunity on Account (after insert) {
list<opportunity> opp=new list <opportunity>();
for( account acc :trigger.new){
opportunity oppt=new opportunity();
oppt.Name = acc .Name;
oppt.Accountid = acc .id;
oppt.StageName= 'proposal';
 oppt.CloseDate = System.today() + 30;
 opp.add(oppt);
}
 if (opp<>null && opp.size()>0) {
 Database.insert(opp);
        }
        }

Please mark this question as Solved if this answers your question so that others can view it as a proper solution.

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

You need to Insert not Update the Opportunity list

Try this code :
 
trigger createopportunity on Account (after insert) {
list<opportunity> opp=new list <opportunity>();
for( account acc :trigger.new){
opportunity oppt=new opportunity();
oppt.Name = acc .Name;
oppt.Accountid = acc .id;
oppt.StageName= 'proposal';
 oppt.CloseDate = System.today() + 30;
 opp.add(oppt);
}
 if (opp<>null && opp.size()>0) {
 Database.insert(opp);
        }
        }

Please mark this question as Solved if this answers your question so that others can view it as a proper solution.

Thanks,
Apoorv
This was selected as the best answer
rakesh kondarakesh konda
Thank you Apoorv saxena