how to write a trigger When ever new Account is inserted with industry as Energy and Type as Customer-Direct then create new Opportunity with same account data
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger CreateOpp on Account (after insert) {
List<Opportunity> optyList = new List<Opportunity>();
for(Account a:Trigger.new) {
if(a.Industry=='Energy' && a.Type='Customer - Direct'){
Opportunity opp = new Opportunity();
opp.AccountId=a.Id;
opp.Name='Created from account';
opp.StageName='Prospecting';
opp.CloseDate=Date.Today();
opp.Industry__c=a.Industry;
// Add fields according to your requirement
optyList.add(opp);
}
}
if(optyList.size()>0){
INSERT optyList;
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Hi Sk Aleem, Try this trigger and it's handler class: Trigger: trigger AccountTrigger on Account (after insert) { if(trigger.IsInsert && trigger.IsAfter) { AccountTrigger_Handler.checkAccIndustryType(trigger.new); } }
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Try this trigger and it's handler class:
Trigger:
trigger AccountTrigger on Account (after insert) {
if(trigger.IsInsert && trigger.IsAfter) {
AccountTrigger_Handler.checkAccIndustryType(trigger.new);
}
}
Handler Class:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi