Hey guys, today in this post we are going to learn about Write Apex Trigger Scenarios, Whenever new Contact is created, we will be updated correspondence Account field of “Status”, if Account Developer field Selected as “Salesforce Developer”, then status should be updating automatically “true” in Salesforce.
Final Output →
Other related post that would you like to learn in Salesforce
Create Apex Trigger →
Step 1:- Create Apex Controller : ContactTrigger.apxt
SFDX:Create Apex Trigger>> New >> ContactTrigger.apxt
ContactTrigger.apxt [Apex Trigger]
|
TRIGGER ContactTrigger ON Contact (BEFORE INSERT) {
IF(TRIGGER.isBefore && TRIGGER.isInsert){
ContactTriggerHandler.updateStatusHelper(TRIGGER.new);
}ELSE IF(TRIGGER.isAfter){
}
}
Create Trigger Handler Controller →
Step 2:- Create Apex Controller : ContactTriggerHandler.apxc
SFDX:Create Apex Trigger>> New >> ContactTriggerHandler.apxc
ContactTriggerHandler.apxc [Trigger Handler]
|
public class ContactTriggerHandler {
public static void updateStatusHelper(List<Contact> conList){
Set<Id> setId = NEW Set<Id>();
FOR(Contact con:conList){
setId.add(con.AccountId);
}
List<Account> accObjList = [SELECT Id, Name, Developer__c, Status__c FROM Account WHERE Id IN:setId];
List<Account> accItem = NEW List<Account>();
FOR(Account acc:accObjList){
IF(acc.Developer__c == 'Salesforce Developer'){
acc.Status__c = TRUE;
accItem.add(acc);
}
}
IF(!accItem.isEmpty()){
UPDATE accItem;
}
}
}
Further post that would you like to learn in Salesforce
What is the relationship between account and contacts in Salesforce?
Accounts and Contacts have a lookup relationship and have a property called cascade-delete, which is set to true. That is why the contact is deleted when the parent object is deleted. Refer below links for more details on relationships and cascade-delete on custom lookup relationships.
Can we create a contact without an account in Salesforce?
However, you can create them only programmatically, via APEX or API. You cannot create such a contact from a Standard UI and if you created a contact without an account via APEX, you will not be able to edit it from a Standard Salesforce UI unless specifying an Account on it.
What are two types of account contact relationships?
A direct relationship is the relationship we are used to seeing with a contact to an account. It is the account listed on the contact page when a contact is created in Salesforce. An indirect relationship is where things get interesting.
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 |