Snippets Collections
public class TestClass {
    
    public Static Integer copies = 0;
    
    
    public TestClass(){
        this.add();
    }
    
       
    public void add(){
        copies++;
    } 
    
    public Static void subtract(){
        copies--;
        System.debug(copies);
    } 


}
trigger AccountTrigger on Account (before insert) {

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

        
        List<Account> myList = new List<Account>();

        for(Account acct : Trigger.new){
          if(acct.name == 'My Test Account'){
           acct.AnnualRevenue = 0;
           myList.add(acct);
          }
        }

        if(myList.size() >= 0){
          insert myList;
        }
      }   
}
(before insert, after insert,before update, after update, before delete, after delete, after undelete)
trigger ContactTrigger on Contact (before insert) { //Context and Event
   
    if(Trigger.isInsert && Trigger.isBefore) { // Condition
       System.debug(Trigger.new);	// Action
    } 
  
}
public with sharing class ContactTriggerHandler {
    //
    // custom trigger handler setup to handler all triggers received on Contact object
    //
    public Boolean isTriggerExecuted = true;
    private boolean myIsExecuting = false;
    private integer BatchSize = 0;

    public ContactTriggerHandler(boolean isExecuting, integer size) {
        myIsExecuting = isExecuting;
        BatchSize = size;
    }

    //
    // On Before Insert
    //
    public void OnBeforeInsert(List<Contact> contactList){
        List<String> emailList = new  List<String>();
        
        for(Contact contact : contactList){
              emailList.add(contact.email);
        }
        
		List<Contact> newContactList = new List<Contact>();
        List<Lead> leadList = [SELECT id,firstname,lastname, email,LeadSource,phone, MobilePhone,OwnerId,HasOptedOutOfEmail FROM Lead WHERE email IN :emailList];
       
        for(Contact cItem : contactList){
            //Merge
            for(Lead lItem: leadList){
            	cItem.HasOptedOutOfEmail = lItem.HasOptedOutOfEmail;
            	cItem.OwnerId = lItem.OwnerId;
            	cItem.LeadSource = lItem.LeadSource;
            	cItem.MobilePhone = lItem.MobilePhone;
				cItem.phone = lItem.phone;
            	cItem.LeadSource = lItem.LeadSource;
           	 	newContactList.add(cItem);
            }

        }
                

    }

    //
    // On After Insert
    //
    public void OnAfterInsert(List<Contact>  contactList) {
       
        List<String> emailList = new  List<String>(); 
        for(Contact contact : contactList){
              emailList.add(contact.email);
        }
        
        List<Lead> leadList = [SELECT id,firstname,lastname, email,LeadSource,phone, MobilePhone,OwnerId,HasOptedOutOfEmail FROM Lead WHERE email IN :emailList];
        List<Task> taskTransferList = [SELECT Id, Who.Id, Who.Type FROM Task WHERE Who.Type = 'Lead'];
        List<Task> taskUpdate = new List<Task>();
        
        for(Contact cItem : contactList){
            //Move Task to Contact
            for(Lead lItem: leadList){
                if(taskTransferList.size() > 0){
                    for(Task taskItem :taskTransferList){
                        taskItem.whoid = cItem.id;
                        taskUpdate.add(taskItem);
                    }
                }

        
            }
            upsert taskUpdate;
            delete leadList;

        }
  
}
star

Sat Jul 24 2021 14:14:46 GMT+0000 (UTC)

#apex #salesforce #trigger
star

Sat Jul 24 2021 09:42:42 GMT+0000 (UTC)

#apex #salesforce #trigger
star

Fri Jul 23 2021 00:18:27 GMT+0000 (UTC)

#apex #salesforce #trigger

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension