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
Create Apex Trigger β
Step 1:- Create Apex Controller : AccountTrigger.apxt
SFDX:Create Apex Trigger>> New >> AccountTrigger.apxt
AccountTrigger.apxt [Apex Trigger]
TRIGGER AccountTrigger ON Account (BEFORE INSERT, after INSERT) {
IF(TRIGGER.isBefore){
}ELSE IF(TRIGGER.isAfter && TRIGGER.isInsert){
accountTriggerHandler.relatedOppAcc(TRIGGER.new);
}
}
Create Trigger Handler Controller β
Step 2:- Create Apex Controller : accountTriggerHandler.apxc
SFDX:Create Apex Trigger>> New >> accountTriggerHandler.apxc
accountTriggerHandler.apxc [Trigger Handler]
public class accountTriggerHandler {
public static void relatedOppAcc(List<Account> accList){
List<opportunity> oppObjList = NEW List<opportunity>();
FOR(Account acc:accList){
Opportunity opp = NEW Opportunity();
opp.Name=acc.Name;
opp.StageName='Prospecting';
opp.CloseDate=system.today();
opp.AccountId=acc.Id;
oppObjList.add(opp);
}
IF(!oppObjList.isEmpty()){
INSERT oppObjList;
}
}
}
Further post that would you like to learn in Salesforce
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.
Related Topics | You May Also Like
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 |