• David_
  • NEWBIE
  • 40 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 4
    Replies
Hey there,

when writing Trigger Handler Classes and calling the respective methods from a Master Trigger there are different ways to achieve this. The keywords are Static and Non-Static .

In other words:
public void
vs.
public static void
I was wondering what's the best practice to write and call a method from a Master Trigger. In fact both options will work - but I once read that going for non-static was always the better choice. Why is that?

Given we have a method in our class that creates Assets from an Opportunity's line items (Opportunity Products). 

Static - Master Trigger call:
OpportunityTriggerHandler.createAssets(Trigger.new)
Question 1:

I saw this approach quite often in forums and blog articles - is this approach generally recommended and reasonable?

Non-Static - Master Trigger call:
OpportunityTriggerHandler oppHandler = new OpportunityTriggerHandler();
oppHandler.createAssets(Trigger.new);
Question 2

Can I consider non-static as best practice and if yes, why?

Looking forward to your feedback!
 
  • September 16, 2019
  • Like
  • 1
Hey there,

I'm new to APEX and curious about converting Apex Triggers to Apex Classes in order to be in line with the One-Trigger-per-Object pattern. 

Let's say I have the following, very simple Trigger: Whenever an Account gets updated, the number of related contacts gets populated in the custom field Number_of_Contacts__c. The Trigger code looks like this:
 
trigger relatedContacts on Account (before update) {  
   
   List<Contact> relatedContacts = [SELECT Id
                                    FROM   Contact
                                    WHERE  AccountId IN :Trigger.new];
        
   for (Account a : Trigger.new) {
        a.Number_of_Contacts__c = relatedContacts.size();
    }            
    
}
First question: How would the Apex Class for this Trigger look like?

Second question: How shall I reference the Apex Class in the Master Trigger for the Account object? At least I already know where to put it:
trigger MasterOpportunityTrigger on Opportunity (
  before insert, after insert, 
  before update, after update, 
  before delete, after delete) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    } 
    if (Trigger.isUpdate) {
      // MY TRIGGER WILL GO HERE ;)
    }
    if (Trigger.isDelete) {

    }
  }

  if (Trigger.isAfter) {
    if (Trigger.isInsert) {

    } 
    if (Trigger.isUpdate) {

    }
    if (Trigger.isDelete) {

    }
  }
}

I would highly appreciate if someone could teach me this best practice!

Best, 
David
  • September 11, 2019
  • Like
  • 0
Hey there, 

the System.FinalException: Record is read-only error is a well known error when talking about After Insert Triggers. I was wondering what's the most common workaround to resolve it.

Example scenario:

Let's say we want to write an After Insert Trigger that...

1) ... sets a field value in a newly created Lead record
2) ... creates a related record (e.g. Task) and therefore requires the Record Id of the new Lead (this part of the logic actually forces us to go for an After Trigger).

This code apparently will give us the notorious error message:
trigger leadTestTrigger on Lead (after insert) {
    for (Lead myLead : Trigger.new) {
        // We're inserting a value into a field of the initial record
        myLead.AnyField = 'Any Value';
        // Create a task related to the Lead
        Task myTask     = new Task();
        myTask.Subject  = 'Anything';
        myTask.WhoId    = myLead.Id;
        myTask.Priority = 'Normal';
        myTask.Status   = 'Not Started';
        insert myTask;       
    }
}

What's the most used technique to get out of this dilemma? How should the above code be amended?

Best,
David
Hey there,

I have a quick question regarding the usage of isLeapYear for a Date. 

Does isLeapYear always require a parameter within the brackets? This code apparently works...
Date myDate = Date.newInstance(2041, 11, 19);
Boolean dateIsLeapYear = Date.isLeapYear(myDate.year());
System.debug(dateIsLeapYear);
... but what about next one? I guess it's not possible to place first the variable and leave the brackets empty:
Date myDate = Date.newInstance(2041, 11, 19);
Boolean dateIsLeapYear = myDate.isLeapYear();
System.debug(dateIsLeapYear);
Just wanna make sure.

Thanks for your help!
Hi,

I have a short, general question regarding collection types for sObjects when it comes to inline SOQL queries. 

As we all know there are three collection types: Lists, Sets and Maps. I recently stumbled upon a Trailhead Project (https://trailhead.salesforce.com/content/learn/projects/quickstart-apex/quickstart-apex-2) where neither of these types where used in a method to retrieve a list of records using an SOQL query:
Account[] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
I'm talking about the Account[] part of the code. Can someone explain to me what makes the [] type  different from the List type? How does it behave and what are the particular use cases? When to use Account[] and when to use List<Account>?

I guess one could have also go for this option:
List<Account> oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
Any help appreciated! 

Best,
David
Hey there,

when writing Trigger Handler Classes and calling the respective methods from a Master Trigger there are different ways to achieve this. The keywords are Static and Non-Static .

In other words:
public void
vs.
public static void
I was wondering what's the best practice to write and call a method from a Master Trigger. In fact both options will work - but I once read that going for non-static was always the better choice. Why is that?

Given we have a method in our class that creates Assets from an Opportunity's line items (Opportunity Products). 

Static - Master Trigger call:
OpportunityTriggerHandler.createAssets(Trigger.new)
Question 1:

I saw this approach quite often in forums and blog articles - is this approach generally recommended and reasonable?

Non-Static - Master Trigger call:
OpportunityTriggerHandler oppHandler = new OpportunityTriggerHandler();
oppHandler.createAssets(Trigger.new);
Question 2

Can I consider non-static as best practice and if yes, why?

Looking forward to your feedback!
 
  • September 16, 2019
  • Like
  • 1
Hey there,

I'm new to APEX and curious about converting Apex Triggers to Apex Classes in order to be in line with the One-Trigger-per-Object pattern. 

Let's say I have the following, very simple Trigger: Whenever an Account gets updated, the number of related contacts gets populated in the custom field Number_of_Contacts__c. The Trigger code looks like this:
 
trigger relatedContacts on Account (before update) {  
   
   List<Contact> relatedContacts = [SELECT Id
                                    FROM   Contact
                                    WHERE  AccountId IN :Trigger.new];
        
   for (Account a : Trigger.new) {
        a.Number_of_Contacts__c = relatedContacts.size();
    }            
    
}
First question: How would the Apex Class for this Trigger look like?

Second question: How shall I reference the Apex Class in the Master Trigger for the Account object? At least I already know where to put it:
trigger MasterOpportunityTrigger on Opportunity (
  before insert, after insert, 
  before update, after update, 
  before delete, after delete) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    } 
    if (Trigger.isUpdate) {
      // MY TRIGGER WILL GO HERE ;)
    }
    if (Trigger.isDelete) {

    }
  }

  if (Trigger.isAfter) {
    if (Trigger.isInsert) {

    } 
    if (Trigger.isUpdate) {

    }
    if (Trigger.isDelete) {

    }
  }
}

I would highly appreciate if someone could teach me this best practice!

Best, 
David
  • September 11, 2019
  • Like
  • 0