function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amit VaidyaAmit Vaidya 

Please implement a trigger on inserts of new records of custom object

We have to implement following things on the insert of new records :

 

  1. If Practice ID on the new record is populated:

Try to find an existing account where Context ID(Text field) on the account contains the value of Practice ID(Text) on the Custom Object.

If the value in Practice ID is less than 5 digits, add zeroes to the left of the Practice ID when comparing.

 

For example, if Practice ID is 123, try to find an account where Context ID contains ‘00123’. 

 

Note: The value in Practice ID on the Custom object shouldn’t be modified.

 

After: If we are getting any matching Accounts then create some records like create contact related to Account etc.

 

Can any one help me to achieve this functionality?

 

Thanks,

Amit

Best Answer chosen by Admin (Salesforce Developers) 
zachbarkleyzachbarkley

Hi Amit,

 

Please try this trigger + apex class. Please note that in order to keep this simple for demostration purposes, this is not "Blukified" 

 

Create Apex Class - Your Name | Setup | Develop | Apex Classes: New

 

public class MyCustomObject_HDL_AIAU {
	public void OnInsert(List<MyCustomObject__c> newRecords,List<MyCustomObject__c> oldRec){
        updateRecord(newRecords,oldRec); 
    }
    public void OnUpdate(List<MyCustomObject__c> upRec,List<MyCustomObject__c> oldRec){
        updateRecord(upRec,oldRec); 
    }
    private void updateRecord(List<MyCustomObject__c> upRec,List<MyCustomObject__c> oldRec) {
    	/******************************************************************
        DECLARE
        ********************************************************************/
        Boolean IsNew = false;
        List<Contact> ContactsToAdd = new List<Contact>{};
        
        /******************************************************************
        * IDENTIFY IF OLD RECORDS HAS VALUE
        ********************************************************************/ 
        Boolean NewRecords=false;
        if(oldRec==null){
            NewRecords=true;
        }

       	for(MyCustomObject__c upRec1:upRec){	
			if(upRec1.Practice_ID__c!=null){
				
				/******************************************************************
			    * FUNCTION FOR ONLY NEW RECORDS  
				********************************************************************/ 
				Boolean NewRecordForRow=true;
	        	if(NewRecords==false){
		        	for(MyCustomObject__c oldRec1:oldRec){
		        		if(upRec1.Id == oldRec1.Id){
		        			NewRecordForRow=false;
		        		}	
			        }  
		        }
				/******************************
				* If Practice ID on the new record is populated:
				*******************************/
				if(NewRecordForRow==true){
					/******************************
					If the value in Practice ID is less than 5 digits, add zeroes to the left of the Practice ID when comparing.
					For example, if Practice ID is 123, try to find an account where Context ID contains ‘00123’. 
					Note: The value in Practice ID on the Custom object shouldn’t be modified.
					*******************************/
					String PracticeIdToCompare = upRec1.Practice_ID__c.trim();
					Integer PracticeIdToCompareLength = PracticeIdToCompare.length();
					if(PracticeIdToCompareLength<6){
						for (Integer i = 0; i < 6-PracticeIdToCompareLength; i++) {
			    				'0'+PracticeIdToCompare;
						}
					}

					/******************************
					* Try to find an existing account where Context ID(Text field) on the account contains the value of Practice ID(Text) on the Custom Object.
					*******************************/
					List<Account> SearchAccount = [SELECT Id,Context_ID__c FROM Account WHERE Context_ID__c =:PracticeIdToCompare  AND IsDeleted=false];
					if(!SearchAccount.IsEmpty()){
						
						for(Account SearchAccount1:SearchAccount){
							/******************************
							* After: If we are getting any matching Accounts then create some records like create contact related to Account etc.
							*******************************/
							Contact ContactToAdd = new Contact(
	               				 FirstName= 'John'
	                			,LastName='Doe'
	                			,AccountId=SearchAccount1.Id
	            			);
			            	ContactsToAdd.Add(ContactToAdd);
						}
					}
				}
		
			}
			
		}
		
		if(!ContactsToAdd.IsEmpty()){
			INSERT ContactsToAdd;
		}
    }
}

 

Then goto Your Name | Setup | Create | Objects | MyCustomObject | Trigers | New

 

trigger MyCustomObject_AIAU on MyCustomObject__c (after insert, after update) {
    MyCustomObject_HDL_AIAU handler = new MyCustomObject_HDL_AIAU ();    
  	if(Trigger.isInsert && Trigger.isAfter) {
   		handler.OnInsert(Trigger.new,Trigger.old);
   	}else if(Trigger.isUpdate && Trigger.isAfter) { 
    		handler.OnUpdate(Trigger.new,Trigger.old);
   	}	
}

 Let me know how you go :-)

 

 

All Answers

zachbarkleyzachbarkley

Hi Amit,

 

Please try this trigger + apex class. Please note that in order to keep this simple for demostration purposes, this is not "Blukified" 

 

Create Apex Class - Your Name | Setup | Develop | Apex Classes: New

 

public class MyCustomObject_HDL_AIAU {
	public void OnInsert(List<MyCustomObject__c> newRecords,List<MyCustomObject__c> oldRec){
        updateRecord(newRecords,oldRec); 
    }
    public void OnUpdate(List<MyCustomObject__c> upRec,List<MyCustomObject__c> oldRec){
        updateRecord(upRec,oldRec); 
    }
    private void updateRecord(List<MyCustomObject__c> upRec,List<MyCustomObject__c> oldRec) {
    	/******************************************************************
        DECLARE
        ********************************************************************/
        Boolean IsNew = false;
        List<Contact> ContactsToAdd = new List<Contact>{};
        
        /******************************************************************
        * IDENTIFY IF OLD RECORDS HAS VALUE
        ********************************************************************/ 
        Boolean NewRecords=false;
        if(oldRec==null){
            NewRecords=true;
        }

       	for(MyCustomObject__c upRec1:upRec){	
			if(upRec1.Practice_ID__c!=null){
				
				/******************************************************************
			    * FUNCTION FOR ONLY NEW RECORDS  
				********************************************************************/ 
				Boolean NewRecordForRow=true;
	        	if(NewRecords==false){
		        	for(MyCustomObject__c oldRec1:oldRec){
		        		if(upRec1.Id == oldRec1.Id){
		        			NewRecordForRow=false;
		        		}	
			        }  
		        }
				/******************************
				* If Practice ID on the new record is populated:
				*******************************/
				if(NewRecordForRow==true){
					/******************************
					If the value in Practice ID is less than 5 digits, add zeroes to the left of the Practice ID when comparing.
					For example, if Practice ID is 123, try to find an account where Context ID contains ‘00123’. 
					Note: The value in Practice ID on the Custom object shouldn’t be modified.
					*******************************/
					String PracticeIdToCompare = upRec1.Practice_ID__c.trim();
					Integer PracticeIdToCompareLength = PracticeIdToCompare.length();
					if(PracticeIdToCompareLength<6){
						for (Integer i = 0; i < 6-PracticeIdToCompareLength; i++) {
			    				'0'+PracticeIdToCompare;
						}
					}

					/******************************
					* Try to find an existing account where Context ID(Text field) on the account contains the value of Practice ID(Text) on the Custom Object.
					*******************************/
					List<Account> SearchAccount = [SELECT Id,Context_ID__c FROM Account WHERE Context_ID__c =:PracticeIdToCompare  AND IsDeleted=false];
					if(!SearchAccount.IsEmpty()){
						
						for(Account SearchAccount1:SearchAccount){
							/******************************
							* After: If we are getting any matching Accounts then create some records like create contact related to Account etc.
							*******************************/
							Contact ContactToAdd = new Contact(
	               				 FirstName= 'John'
	                			,LastName='Doe'
	                			,AccountId=SearchAccount1.Id
	            			);
			            	ContactsToAdd.Add(ContactToAdd);
						}
					}
				}
		
			}
			
		}
		
		if(!ContactsToAdd.IsEmpty()){
			INSERT ContactsToAdd;
		}
    }
}

 

Then goto Your Name | Setup | Create | Objects | MyCustomObject | Trigers | New

 

trigger MyCustomObject_AIAU on MyCustomObject__c (after insert, after update) {
    MyCustomObject_HDL_AIAU handler = new MyCustomObject_HDL_AIAU ();    
  	if(Trigger.isInsert && Trigger.isAfter) {
   		handler.OnInsert(Trigger.new,Trigger.old);
   	}else if(Trigger.isUpdate && Trigger.isAfter) { 
    		handler.OnUpdate(Trigger.new,Trigger.old);
   	}	
}

 Let me know how you go :-)

 

 

This was selected as the best answer
Amit VaidyaAmit Vaidya

Hi zachbarkley

Can you please let me know?

 

Thanks,

Amit

zachbarkleyzachbarkley

Hi Amit,

 

My appologies, R.Id should be upRec1.Id. It's comparing the old vales to the new values to see if it did indeed had an old record. If it didn't have an old record, it then decides that it's a new record.