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
Preethi SPreethi S 

Create Opportunity record

Hi all,

I have to create one Opportunity record.When I create that record the another record will be created automatically by using triggers.

This is my code. this is not working properly. It shows null pointer exception...Can u tell about this?

trigger OpprtunityInsertion on Opportunity (after insert) {
List<Opportunity> listOfOpportunities = new List<Opportunity>();
for(Opportunity op : Trigger.new)
{
if(op.StageName == 'Closed Won')
{
Opportunity o= new Opportunity();
o.Name = 'Cloned'+op.Name;
o.StageName=op.StageName+'Cloned';
o.CloseDate=op.CloseDate.addYears(1);
listOfOpportunities.add(o);
}
}
insert listOfOpportunities;
}
ShashForceShashForce
This same code works absolutely fine for me in my developer org. Can you tell me if you are doing anything else differently?
praveen murugesanpraveen murugesan
Hi Preethi,

Code you have posted will work fine. Please check your trigger whether you are using trigger.old

trigger OpprtunityInsertion on Opportunity (after insert) {
List<Opportunity> listOfOpportunities = new List<Opportunity>();
for(Opportunity op : Trigger.new)
{
if(op.StageName == 'Closed Won')
{
Opportunity o= new Opportunity();
o.Name = 'Cloned'+op.Name;
o.StageName=op.StageName+'Cloned';
o.CloseDate=op.CloseDate.addYears(1);
listOfOpportunities.add(o);
}
}
insert listOfOpportunities;
}

Mark this as solution if you got an answer

Thanks.

Praveen Murugesan
vanessa veronvanessa veron
Shashank_SFDC help me please!!!
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AW5B
Preethi SPreethi S
Thank u Shashank....
 yes I am doing some tasks in triggers...
Preethi SPreethi S
Thanks praveen. My code is working now .