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
sk aleemsk aleem 

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

Khan AnasKhan Anas (Salesforce Developers) 
Hi Aleem,

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.
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.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
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);
    }
}

Handler Class:
public class AccountTrigger_Handler {
    public static void checkAccIndustryType(List<Account> accList) {
        //system.debug('accList------' + accList);
        Set<Id> accIdSet = new Set<Id>();
        List<Opportunity> oppList = new List<Opportunity>();
        try {
            for(Account ac : accList) {
                if(ac.Industry == 'Energy' && ac.Type == 'Customer - Direct') {
                    accIdSet.add(ac.Id);
                }
            }
            //system.debug('accIdSet------' + accIdSet);
            if(accIdSet.size() > 0) {
                for(Id i : accIdSet) {
                    Opportunity op = new Opportunity();
                    op.Name = 'Opp Name';
                    op.StageName = 'Prospecting';
                    op.CloseDate = system.today() + 5;
                    op.AccountId = i;
                    oppList.add(op);
                }
                //system.debug('oppList------' + oppList);
                if(oppList.size() > 0) {
                    insert oppList;
                }
            }
        }
        catch (Exception ex) {
            system.debug('Exception---ofLine--->' + ex.getLineNumber());
            system.debug('Exception---Message--->' + ex.getMessage());
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi