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
Ruchi Gupta KeshriRuchi Gupta Keshri 

how to write trigger for this condition can any one help me. Check if contact exists for the email id and if not then create a contact record respective to the email id  and populate the id to the A object contact field. 

Create fields on Object A :- ObjectA

Request Status (Picklist) :-  New, Approved, Duplicate, Not Matched, Rejected, Completed, Match Found. 

Is Auto Approved (Checkbox) 

Email  

Roles (Multi-Select Picklist) :- Admin, Developer, Manager, CEO, Executive. 

When a record is created for the first time request status is new.  Also Check roles and email existence, 
if already exist then request status is Duplicate. 

Check if contact exists for the email id and if not then create a contact record respective to the email id 
and populate the id to the A object contact field. 

If the Request Status is Completed for the record created , then for another new record (with same email) 
the Request Status should be Approved and Checkbox true. 

i write trigger for above 1st condition.....can any one tell how to write for 2nd and 3rd point. with best practice....

trigger ObjectATrigger on ObjectA__c (before insert,after update) {
    list<String> email= new List<string>();
    list<string> roles= new List<string>();
    list<ObjectA_c> objData= [Select id,Emailc,Rolesc,Request_Statusc from ObjectA_c limit 10];
    if(trigger.isbefore && trigger.isinsert){
    for(ObjectA__c obj:objData){
        email.add(obj.Email__c);
        roles.add(obj.Roles__c);
    }
    for(ObjectA__c objcheck: trigger.new){
        if(email.contains(objcheck.Email_c) || roles.contains(objcheck.Roles_c)){
            objcheck.Request_Status__c='Duplicate';
        }else
           objcheck.Request_Status__c='New'; 
    }
    }
    if(trigger.isafter && trigger.isupdate){
        list<ObjectA_c> objNewRecord= new List<ObjectA_c>();
        for(ObjectA__c objupdate: trigger.old){
            system.debug('objupdate'+objupdate);
            if(objupdate.Request_Status__c == 'Completed'){
                ObjectA_c newobj= new ObjectA_c();
                newobj.Request_Status__c='Approved';
                newobj.Name=objupdate.Name;
                newobj.Is_Auto_Approved__c=true;
                newobj.Email_c= objupdate.Email_c;
                objNewRecord.add(newobj);
                system.debug('objNewRecord'+objNewRecord);
            }
            insert objNewRecord;
         }
    }
}
Best Answer chosen by Ruchi Gupta Keshri
AnkaiahAnkaiah (Salesforce Developers) 
Hi Ruchi,

try with below code.
trigger ObjectATrigger on ObjectA__c (before insert,after update) {
    set<String> email= new set<string>();
    set<string> roles= new set<string>();
    if(trigger.isbefore && trigger.isinsert){
    for(ObjectA__c obj:trigger.new){
	if(obj.Email__c!=Null){
        email.add(obj.Email__c);
		}
	if(obj.Roles__c!=Null){	
        roles.add(obj.Roles__c);
		}
    }
	
	List<ObjectA__c> objAlist = [select id,Email__c,Roles__c from ObjectA__c where Email__c =:email OR Roles__c=:roles ];

    for(ObjectA__c objcheck: trigger.new){
	
        if(objAlist.size()>0){
            objcheck.Request_Status__c='Duplicate';
        }else
           objcheck.Request_Status__c='New'; 
    }
    }
	 
	Map<string,id> emailrelatedcontactsmap = new <string,id>();
	
	List<contact> coninsert = new List<contact>();
	List<ObjectA__c> objlistwithoutcontacts = new List<ObjectA__c>();
	
	for(contact con:[select id, email from contact where email=:email]){
	emailrelatedcontactsmap.put(con.email,con.id);
	}
	for(ObjectA__c obj:trigger.new){
	if(emailrelatedcontactsmap.containskey(obj.email__c)){
	obj.contact__c = emailrelatedcontactsmap.get(obj.email__C);
	}else if(!emailrelatedcontactsmap.containskey(obj.email__c)){
   objlistwithoutcontacts.add(obj);
	}	
	}
	
	if(objlistwithoutcontacts.size()>0){
	 List<Contact> createNewContact = new List<Contact>();
                Map<String,ObjectA__c> conNameKeys = new Map<String,ObjectA__c>();
                
                for (ObjectA__c objA : objlistwithoutcontacts)   
                {  
                    String objemailName = objA.Email__c ;  
                    conNameKeys.put(objemailName,objA);                      
                    Contact conObj = new Contact();  
                    conObj.LastName = objA.Name;
                    conObj.email= objA.email__c;
                    createNewContact.add(conObj);  
                }  
                Insert createNewContact;  
                for (Contact con : createNewContact)   
                {  
                    system.debug('mapContainsKey ' + conNameKeys.containsKey(con.Email));
                   
                    if (conNameKeys.containsKey(con.Email))   
                    {  
                        conNameKeys.get(con.Email).contact__c = con.Id;  
                    }  
                }  
	
	}
    if(trigger.isafter && trigger.isupdate){
        list<ObjectA_c> objNewRecord= new List<ObjectA_c>();
        for(ObjectA__c objupdate: trigger.new){
            system.debug('objupdate'+objupdate);
            if(objupdate.Request_Status__c == 'Completed'){
                ObjectA_c newobj= new ObjectA_c();
                newobj.Request_Status__c='Approved';
                newobj.Name=objupdate.Name;
                newobj.Is_Auto_Approved__c=true;
                newobj.Email_c= objupdate.Email_c;
                objNewRecord.add(newobj);
                system.debug('objNewRecord'+objNewRecord);
            }
           
         }
		  insert objNewRecord;
    }
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Ruchi,

try with below code.
trigger ObjectATrigger on ObjectA__c (before insert,after update) {
    set<String> email= new set<string>();
    set<string> roles= new set<string>();
    if(trigger.isbefore && trigger.isinsert){
    for(ObjectA__c obj:trigger.new){
	if(obj.Email__c!=Null){
        email.add(obj.Email__c);
		}
	if(obj.Roles__c!=Null){	
        roles.add(obj.Roles__c);
		}
    }
	
	List<ObjectA__c> objAlist = [select id,Email__c,Roles__c from ObjectA__c where Email__c =:email OR Roles__c=:roles ];

    for(ObjectA__c objcheck: trigger.new){
	
        if(objAlist.size()>0){
            objcheck.Request_Status__c='Duplicate';
        }else
           objcheck.Request_Status__c='New'; 
    }
    }
	 
	Map<string,id> emailrelatedcontactsmap = new <string,id>();
	
	List<contact> coninsert = new List<contact>();
	List<ObjectA__c> objlistwithoutcontacts = new List<ObjectA__c>();
	
	for(contact con:[select id, email from contact where email=:email]){
	emailrelatedcontactsmap.put(con.email,con.id);
	}
	for(ObjectA__c obj:trigger.new){
	if(emailrelatedcontactsmap.containskey(obj.email__c)){
	obj.contact__c = emailrelatedcontactsmap.get(obj.email__C);
	}else if(!emailrelatedcontactsmap.containskey(obj.email__c)){
   objlistwithoutcontacts.add(obj);
	}	
	}
	
	if(objlistwithoutcontacts.size()>0){
	 List<Contact> createNewContact = new List<Contact>();
                Map<String,ObjectA__c> conNameKeys = new Map<String,ObjectA__c>();
                
                for (ObjectA__c objA : objlistwithoutcontacts)   
                {  
                    String objemailName = objA.Email__c ;  
                    conNameKeys.put(objemailName,objA);                      
                    Contact conObj = new Contact();  
                    conObj.LastName = objA.Name;
                    conObj.email= objA.email__c;
                    createNewContact.add(conObj);  
                }  
                Insert createNewContact;  
                for (Contact con : createNewContact)   
                {  
                    system.debug('mapContainsKey ' + conNameKeys.containsKey(con.Email));
                   
                    if (conNameKeys.containsKey(con.Email))   
                    {  
                        conNameKeys.get(con.Email).contact__c = con.Id;  
                    }  
                }  
	
	}
    if(trigger.isafter && trigger.isupdate){
        list<ObjectA_c> objNewRecord= new List<ObjectA_c>();
        for(ObjectA__c objupdate: trigger.new){
            system.debug('objupdate'+objupdate);
            if(objupdate.Request_Status__c == 'Completed'){
                ObjectA_c newobj= new ObjectA_c();
                newobj.Request_Status__c='Approved';
                newobj.Name=objupdate.Name;
                newobj.Is_Auto_Approved__c=true;
                newobj.Email_c= objupdate.Email_c;
                objNewRecord.add(newobj);
                system.debug('objNewRecord'+objNewRecord);
            }
           
         }
		  insert objNewRecord;
    }
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer
Ruchi Gupta KeshriRuchi Gupta Keshri
Hi @Ankaiah,
Thank you so much for the 2nd point condition is working fine now
but in 1st condition  objcheck.Request_Status__c='Duplicate'; not working.
and 3rd condition is also not working can you please help me with 3rd codition(If the Request Status is Completed for the record created , then for another new record (with same email) the Request Status should be Approved and Checkbox true. ) not able to create new record when newobj.Request_Status__c='Approved';
AnkaiahAnkaiah (Salesforce Developers) 
1 st condition:

Can you check, if you have any duplicate ObjectA__c records with email/Roles fields vaules or not.

3rd conditon:
Update the ObjectA__c  record with Request_Status__c= completed and check whether new record is created or not?

If not, keep the system.debug stamenets and figure out the issue.

Thanks!!​​​​​​​