When an Account record is created then Create a related Opportunity automatically in Salesforce

175 views


Hey guys, today in this post we are going to learn about the Trigger Scenario:- When an Account record is created then Create a related Opportunity automatically in Salesforce..

 

Final Output →

 

Other related post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

Create Apex Trigger →

Step 1:- Create Apex Controller : AccountTrigger.apxt

SFDX:Create Apex Trigger>> New >> AccountTrigger.apxt

AccountTrigger.apxt [Apex Trigger]

  1.   TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.  
  5.     }ELSE IF(TRIGGER.isAfter && TRIGGER.isInsert){
  6.  
  7.      accountTriggerHandler.relatedOppAcc(TRIGGER.new);
  8.     }
  9.  
  10.  
  11. }

 

Create Trigger Handler Controller →

Step 2:- Create Apex Controller : accountTriggerHandler.apxc

SFDX:Create Apex Trigger>> New >> accountTriggerHandler.apxc

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

accountTriggerHandler.apxc [Trigger Handler]

  1.  public class accountTriggerHandler {
  2.  
  3.     public static void relatedOppAcc(List<Account> accList){
  4.          List<opportunity> oppObjList = NEW List<opportunity>(); 
  5.         FOR(Account acc:accList){           
  6.             Opportunity opp = NEW Opportunity();
  7.             opp.Name=acc.Name;
  8.             opp.StageName='Prospecting';
  9.             opp.CloseDate=system.today();
  10.             opp.AccountId=acc.Id;
  11.             oppObjList.add(opp);
  12.         }
  13.         IF(!oppObjList.isEmpty()){
  14.             INSERT oppObjList;
  15.         }
  16.  
  17.     }
  18.  
  19. }

 

Further post that would you like to learn in Salesforce

 

FAQ (Frequently Asked Questions)

How do I create an opportunity in Salesforce using Apex?

Navigate to the Opportunities tab, and click New. Enter the Opportunity Name, Account Name, Close Date, and Stage. Enter additional information as required. Click Save.

How do I trigger an opportunity in Salesforce?

The trigger is defined on the Opportunity object and set to execute after an update. The trigger iterates over the updated Opportunities and checks if their stage has changed to 'Closed Won' from a previous stage that is not 'Closed Won'. For each qualifying Opportunity, a new Order is created.

How do I add a custom field of opportunity?

Create Custom Fields: Navigate to 'Custom Fields' under settings. Click on 'Add Field' or 'Create Field'. Define Your Fields: Choose 'Opportunity' as your object and then define your custom field type, such as single-line text, number, dropdown, etc.

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net