Sample Triggers with different scenarios | Techila
2857
single,single-post,postid-2857,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Sample Triggers with different scenarios

SAMPLE TRIGGERS WITH DIFFERENT SCENARIOS

Sample Triggers with different scenarios

Sample Trigger Scenarios of Salesforce

 

Trigger Scenario 1:

Create “Top X Designation” custom object which is related list to Opportunity (Look up Relationship). In the Top X Designation object, create the fields

  • Type (Picklist),
  • Document Attached (Checkbox)

Create one field Handoff Attached with picklist type with values are Yes, No on Opportunity Object.

Logic :-  If Type (Top X Designation) = “Contract Flow Down/Handoff”, and “Document Attached” = True then “Handoff Attached” = True, otherwise false.
Code:-
trigger UpdateHandoffAttached on Top_X_Designation__c(after insert, after update,
 after delete) {
 List<Opportunity> listoppUpdate=new List<Opportunity>();
 List<Opportunity> listopp=new List<Opportunity>();
 Set<Id> setOppId=new Set<Id>();
 Set<Id> setOppDelete=new Set<Id>();
 map<id,id> mapDocAttchTrue = new map<id,id>();
 map<id,id> mapDocAttchFalse = new map<id,id>();
 map<id,id> mapDelete = new map<id,id>();
 if(Trigger.isInsert || Trigger.isUpdate){
 for(Top_X_Designation__c ada:Trigger.New){
 if(ada.Document_Attached__c==True && ada.Item_Type__c=='Contract Flow
 Down/Handoff'){
 mapDocAttchTrue.put(ada.Opportunity,ada.id);
 setOppId.add(ada.Opportunity);
 }
 else
 mapDocAttchFalse.put(ada.Opportunity,ada.id);
 setOppId.add(ada.Opportunity);
 }
 }
 if(Trigger.isDelete){
 for(Top_X_Designation__c ada:Trigger.old){
 mapDelete.put(ada.Opportunity,ada.id);
setOppId.add(ada.Opportunity);
 setOppDelete.add(ada.Opportunity);
 }
 }
listopp = “select id,Handoff_Attached__c from Opportunity where id in: setOppId”;
 if(listopp.size()>0 && listopp !=null){
 for(Opportunity opp:listopp){
 if(mapDocAttchTrue.containskey(opp.id)){
 opp.Handoff_Attached__c='Yes';
 }
 if(mapDocAttchFalse.containskey(opp.id)){
 opp.Handoff_Attached__c='No';
 }
 if(setOppDelete.contains(opp.id)){
 opp.Handoff_Attached__c='';
 }
 listoppUpdate.add(opp);
 }
 }
 if(listoppUpdate.size()>0 && listoppUpdate!=null){
 update listoppUpdate;
 system.debug('LLLLLLLLLLLLLLL'+listoppUpdate);
 }
 }

Trigger Scenario 2:-

The following Trigger will fires when we try to create the account with same name i.e Preventing the users to create Duplicate Accounts

trigger AccountDuplicateTrigger on Account (before insert, before update)
{
 for(Account a:Trigger.new)
 {
 List<Account> acc=“Select id from Account where Name=:a.Name and Rating=:a.Rating
 “;
 if(acc.size()>0)
 {
 acc.Name.addError('You Cannot Create the Duplicate Account');
 }
 }
 }

Trigger Scenario 3:-

The following trigger updates the field called “Hello” by the value “World”whenever we are creating an account or updating an account record. Create the field called “Hello” on the Account Object (Data Type = Text)

trigger HelloWorld on Account (before insert, before update)
{
 List<Account> accs = Trigger.new;
 MyHelloWorld my= new MyHelloWorld(); //creating instance of apex class
 my.addHelloWorld(accs); // calling method from the apex class
 }

Class:
public class MyHelloWorld
 {
 public void addHelloWorld(List<Account> accs)
 {
 for (Account a:accs)
 {
 if (a.Hello__c != 'World')
 {
 a.Hello__c = 'World';
 }
 }
 }
 }

Trigger Scenario 4:-

The following trigger describes about when the leads are inserted into the data base it would add Doctor prefixed for all lead names. This is applicable for both inserting and updating the lead records.

trigger PrefixDoctor on Lead (before insert,before update)
{
 List<Lead> leadList = trigger.new;
 for(Lead l: leadList)
 {
 l.firstname = 'Dr.'+ l.firstname;
 }
 }

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment