Posts

Showing posts with the label trigger

Salesforce Fact #916 | Adding validation on UserTerritory2Association

Image
Suppose we would like to add validations while adding users to the territory. For example, The user should be active and the user should not be assigned to multiple territories. We can write the logic in an after insert trigger on UserTerritory2Association object. Attached are the sample code snippets.

Salesforce Fact #915 | Issue with criteria based sharing rule in before trigger

Image
Do you know criteria based sharing rules work asynchronously, hence they don't work as expected in before insert triggers. In this example, we have two object Account(Parent) and Agent(Child) and there is lookup relationship present. Now, we have created a criteria based sharing rule on account such that it is in effect when the account number is 1234. While creating an agent record we are creating an account record and setting the account owner explicitly to test out the sharing rule. We encounter an error while creating a new agent record and the account lookup is populated with some value. However, it works fine with owner based sharing rules. Reference:  https://salesforce.stackexchange.com/questions/375247/when-does-sharing-rule-execute Attached are the screenshots. Note: The trigger code and sharing rules are only for demonstration purpose.

Salesforce Fact #901 | Few things to consider while using Data Import Wizard

Here are two things to keep in mind while importing records with Data Import Wizard: 1) The fields available for mapping is based on the layout of the selected record type and the field level security of the user. 2) While importing records we need to select a checkbox named 'Trigger workflow rules and processes for new and updated records' in order to trigger the automations in place for that object. Reference:  https://help.salesforce.com/s/articleView?id=000387837&language=en_US&type=1

Salesforce Fact #893 | OLI insert/update triggers Opp triggers and flows

When an Opportunity Product record is created or during update if certain fields like Sales price/Quantity is updated, it executes the before and after update triggers and flows on related opportunity. So, we need to be careful while creating any automation on Opportunity Product. Reference:  https://salesforce.stackexchange.com/questions/184659/opportunitylineitem-fires-opportunity-trigger

Salesforce Fact #848 | Trigger validation on locked records

Image
Suppose we want to put a validation that if an account record is locked, we cannot create any related record for example Contact. Attached is a sample trigger code:

Salesforce Fact #761 | Invalid email value in trigger

Image
What happens when an email field is evaluated to an incorrect value from a trigger. The below error is encountered: data changed by trigger for field <fieldName>: invalid email address: (value has been hidden) In this example, we have a dummy email field on contact which is populated with an invalid value from trigger and is eventually encountered as an error. Note: The code snippet is only for quick demonstration purpose. Attached are the screenshots.

Salesforce Fact #628 | Related account industry validation using trigger

Image
Suppose we have a scenario where we need to put a validation such that the industry of the related account should be the same on contact. If we update the related account on contact record, it needs to be of the same industry. This can be implemented using trigger logic. Attached is the screenshot. Note: The code snippet is only for demonstration purpose and coding best practices have not been covered.