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
ExploreForceExploreForce 

create insert trigger on Registration having lookup on parent object contacts

I want to create insert trigger on registration.
1. create lookup on contacts
2. Need to check whether email id already exists in contacts.
     if emailid alreday exists in contacts, Don''t register
     if emailid doesn't exist, then register the user with new mailid.
     
     Can someone help on this?

      trigger Registration on Registration__c (before insert) {
      List<String>newemail = new List<String>();
      List<Contact> parentObjList = new List<Contact>();
       List<Id> listIds = new List<Id>();

    for (Registration__c childObj : Trigger.new) {
        listIds.add(childObj.Contact__c);
    }

    parentObjList = [SELECT id,email FROM Contact WHERE ID IN :listIds];
}
Ramu_SFDCRamu_SFDC
Can you please elaborate the 2nd point. When you say check whether email id already exists in contacts, do you mean check all the contacts in the system or the registration records within that particular contact? Please clarify.
ExploreForceExploreForce
My requirement is:

If someone registers for an event with an email address not found in the Contact table, a new Contact should be created.
consider the Contact email address to be unique and should therefore be considered a unique identifier for the Contact.

Thanks in advance
Ramu_SFDCRamu_SFDC
Hi, Try this out

trigger Registration on Registration__c (before insert) {
      List<String>newemail = new List<String>();
      List<Contact> parentObjList = new List<Contact>();
       List<Id> listIds = new List<Id>();

    for (Registration__c childObj : Trigger.new) {
        listIds.add(childObj.Contact__c);
    }

    parentObjList = [SELECT id,email FROM Contact WHERE ID IN :listIds];
}
map<string,id> contactmap=new map<string.id>();
for(contact cn:parentObjList){
contactmap.add(cn.email,cn.id);
}

for (Registration__c childObj 2: Trigger.new){
if(contactmap.containskey(childobj2.email){
apexpages.adderror('contact already exist with this email');
}
}

Hope this helps.