Create an Task under Opportunity if AccountId is Null ?

on

|

views

and

comments

Table of Contents

Introduction to scenario

โ†’ Apex Trigger Scenario 7

Develop an Apex Trigger so that when an opportunity is created and the account is blank, create a task under Opportunity. Hint: – AccountId == null

    • Subject – Opportunity is created without Account
    • Description – Opportunity is created without Account, Please assign the Correct account to Opportunity.
    • Due Date – Todays Date + 7
    • Status – Not Started
    • Priority – High
    • Related To (What) – Opportunity Id
    • Assigned To (OwnerId) – Opportunity Owner Id

Hands on

				
					trigger OpportunityTrigger on Opportunity (after insert) {
    OpportunityTriggerDispatcher.dispatch(Trigger.OperationType);
}
				
			

Dispatcher Class

				
					public class OpportunityTriggerDispatcher {
    public Static void dispatch(System.TriggerOperation operationType){
        switch on operationType{
            WHEN AFTER_INSERT{
                OpportunityTriggerHandler.afterInsert((Map)trigger.newMap);
            }
        }
    }
}
				
			

Handler Class

				
					public with Sharing class OpportunityTriggerHandler {
  public Static void afterInsert(ListopportunityList){
    List taskInsertList = new List();
    for(Opportunity opp : opportunityList){
        if(opp.AccountId == Null){
            Task t = new Task();
            t.Subject = 'Opportunity is created without Account';
            t.Description = ' Opportunity is created without Account, Please assign the Correct account to Opportunity.';
            t.ActivityDate = System.today().addDays(7);
            t.Status = 'Not Started';
            t.Priority = 'High';
            t.WhatId = opp.Id;
            t.OwnerId = opp.OwnerId;
            taskInsertList.add(t);
        }
        
    }
    try{ // Catch the error in logger Class
        insert as user taskInsertList;
    }
    
    catch(System.DmlException ex){
        List seList = new List();
        System_Event__c se = LoggerClass.logError('OpportunityTriggerHandler',ex.getStackTraceString(),ex.getTypeName(),ex.getLineNumber());
        seList.add(se);
        insert seList;
    }
  }
}
				
			

Logger Class

				
					// Logger Class to catch the Error
public class LoggerClass {
    public Static System_Event__c logError(String className, String completeTracing, String exceptionType, Integer lineNumber){
        System_Event__c se = new System_Event__c();
        se.Class_Name__c = className;
        se.Complete_Trace__c = completeTracing;
        se.Exception_Type__c = exceptionType;
        se.Line_Number__c = lineNumber;
        se.User__c = UserInfo.getUserId();
        return se;
    }
}
				
			

Outcome

Resources

Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; itโ€™s the cornerstone of development on the Salesforce Platform. Itโ€™s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential