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
bhanu_prakashbhanu_prakash 

create new oppournity

Hi team,
How to create new oppournity . when leadsource is email automatically .
Help me to create trigger on this

Thanks for advance :)
Abhishek BansalAbhishek Bansal
Hi Bhanu,

Please find the required trigger code below:
trigger createOpp on Lead (after update){
	List<Opportunity> newOppList = new List<Opportunity>();
	
	for(Lead ld : trigger.new){
		if(ld.LeadSource != trigger.oldMap.get(ld.Id).LeadSource && LeadSource == 'email'){
			Opportunity opp = new Opportunity();
			opp.Name = 'New Opp';
			opp.StageName = 'Email Lead';
			opp.ClosedDate = Date.today();
			//Enter other fields here
			newOppList.add(opp);
		}
	}
	if(newOppList.size() > 0){
		insert newOppList;
	}
}

Please let me know if you need any further help or information on this.

Thanks,
Abhishek Bansal.
bhanu_prakashbhanu_prakash
Error: Compile Error: Variable does not exist: LeadSource at line 5 column 67

Actually LeadSource is standard field and it's in my org  :(  ?
Abhishek BansalAbhishek Bansal
Hi Bhanu,

Please use the updated code below:
trigger createOpp on Lead (after update){
	List<Opportunity> newOppList = new List<Opportunity>();
	
	for(Lead ld : trigger.new){
		if(ld.LeadSource != trigger.oldMap.get(ld.Id).LeadSource && ld.LeadSource == 'email'){
			Opportunity opp = new Opportunity();
			opp.Name = 'New Opp';
			opp.StageName = 'Email Lead';
			opp.ClosedDate = Date.today();
			//Enter other fields here
			newOppList.add(opp);
		}
	}
	if(newOppList.size() > 0){
		insert newOppList;
	}
}

Thanks,
Abhishek Bansal.​
bhanu_prakashbhanu_prakash
thank you so much for quick response

Error: Compile Error: Variable does not exist: ClosedDate at line 9 column 17
Abhishek BansalAbhishek Bansal
Please use CloseDate in place of ClosedDate.
Chetan KapaniaChetan Kapania
Hi Abhishek,

I tried the latest code mentioned by you, and I am getting Variable does not exist:LeadSource and Variable does not exist:Id error message. 

Kindly assist.

Regards
Chetan
Abhishek BansalAbhishek Bansal
Hi Chetan,

Please paste the latest code that are using here with the line number where you are facing the issue.

Thanks,
Abhishek Bansal.
Chetan KapaniaChetan Kapania

As requested, here is the screenshot of the code written and error message recieved. 

User-added image

Regards
Chetan