Create an Apex Trigger to Update Industry Field?

0
470

Introduction to scenario

→ Apex Trigger 1
Develop an Apex Trigger so that every time any account is inserted then set the value of the Industry field to Education. Also, check if the Description is blank then set the value for the description field to “Account Description is blank

  1. Industry = Education
  2. Description = Account Description is blank if Description is null

Hands on

//logic less trigger
trigger AccountTrigger on Account (before insert) {
    AccountTriggerDispatcher.dispatch(Trigger.operationType);
}

Dispatcher Class

// dispatcher class
public class AccountTriggerDispatcher {
    public Static void dispatch(System.TriggerOperation operationType){
        switch on operationType{
            WHEN BEFORE_INSERT{
                AccountTriggerHandler.beforeInsert(trigger.new);
            }
        }
    }
 }

Handler Class

//Handler class
public with sharing class AccountTriggerHandler {
  public Static void beforeInsert(ListaccountList){
    for(Account acc : accountList){
        if(acc.Industry== Null){
            acc.Industry = 'Education';
        }
        if(acc.Description == Null){
            acc.Description = 'Account Description is blank';
        }
    }
  }
}

Outcome

Resources

LEAVE A REPLY

Please enter your comment!
Please enter your name here