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
Ganesh PrasathGanesh Prasath 

Cross standard object Trigger

Hi we have a scenario where a user creates a task and enters new contacts met during a site visit, this value entered in the contact has to be checked on the contacts object and if not present already a new record has to be inserted. Am a newbie to apex programming any help would be much appreciated.

Regards,
Ganesh
Best Answer chosen by Ganesh Prasath
BALAJI CHBALAJI CH
Hi Ganesh,

Please find below revised code and let me know if that helps you.
trigger TaskTrigger on Task (before insert) {
    
    set<string> Names = new set<string>();
    list<contact>ContactList = [select id, name from contact];
    
    for(contact c: [select id, Name from contact])
    {
        Names.add(c.Name);
    }
    
    list<contact>NewContactList = new list<contact>();
    for(task t : trigger.new)
    {
        if(Names.contains(t.New_key_Contact_Met__c)==true)
        {
            t.New_key_Contact_Met__c.addError('This contact already exists');
        }
        else
        {
            contact c = new contact();
            c.LastName = t.New_key_Contact_Met__c;
            NewContactList.add(c);
        }
    }
    if(NewContactList.size() > 0)
    insert NewContactList;
}

Best Regards,
BALAJI

All Answers

BALAJI CHBALAJI CH
Hi Holly Hyder,

This can be done using Trigger. Please find below sample code and let me know if that works for you.
 
trigger ContactCheck on Contact (before insert) {
    
    set<string> Lastnames = new set<string>();
    list<contact>ContactList = [select id, Lastname from contact];
    
    for(contact c: [select id, Lastname from contact])
    {
        Lastnames.add(c.Lastname);
    }
    
    for(contact c: trigger.new)
    {
        if(Lastnames.contains(c.Lastname)==true)
        {
            c.Lastname.addError('This contact already exists');
        }
    }
}

Best Regards,
BALAJI
Ganesh PrasathGanesh Prasath
Hi Balaji , 

Thanks a lot for the reply , but the trigger should be based on the value entered in the Task tab (New_key_Contact_Met__c) , this value should be checked if already present in Contacts , if available  it should throw an error , else  a new contact with the value entered should be created.

Thanks a lot again.

Regards,
Ganesh
BALAJI CHBALAJI CH
Hi Ganesh,
You mean to say that New_key_Contact_Met__c is custom field in Task Object and whenever a new Task is created, the value of this New_key_Contact_Met__c field should be checked with all Contacts. If there is any contact with that value, then an error should be thrown, otherwise new contact should be created.
Is this the Requirement, please let me know if I am Right or any changes.

Best Regards,
BALAJI
Ganesh PrasathGanesh Prasath
Hi Balaji, 

  Yes, New_key_Contact_Met__c is a custom field on tasks but may not be filled in for every new task , but if a new contact is met then the name of the new contact will be entered in this field and a check on the contact object should be done (by the trigger) , if the name (perhaps i will include a field for email as well as it would make more sense (would be unique) ) already exists an error should be displayed else a new contact with the provided details should be created on the contact object. Please let me know if am not clear enough.

Thanks again Balaji.
BALAJI CHBALAJI CH
Hi Ganesh,

Please find below revised code and let me know if that helps you.
trigger TaskTrigger on Task (before insert) {
    
    set<string> Names = new set<string>();
    list<contact>ContactList = [select id, name from contact];
    
    for(contact c: [select id, Name from contact])
    {
        Names.add(c.Name);
    }
    
    list<contact>NewContactList = new list<contact>();
    for(task t : trigger.new)
    {
        if(Names.contains(t.New_key_Contact_Met__c)==true)
        {
            t.New_key_Contact_Met__c.addError('This contact already exists');
        }
        else
        {
            contact c = new contact();
            c.LastName = t.New_key_Contact_Met__c;
            NewContactList.add(c);
        }
    }
    if(NewContactList.size() > 0)
    insert NewContactList;
}

Best Regards,
BALAJI
This was selected as the best answer
Ganesh PrasathGanesh Prasath
Hi Balaji , 

 Thanks , I will check this and get back to you .

Thanks a lot , much appreciated.

Regards,
Ganesh
Ganesh PrasathGanesh Prasath
HI Balaji , 

 I had to make a few minor changes but it Worked !!! . 

Thanks a lot Balaji.

Regards,
Ganesh
Ganesh PrasathGanesh Prasath
Hi Balaji , 

Just a clarification , I need this trigger to work only on tasks that are of type meeting , it should not be executed for other types , how can this be achieved?

Regards,
Ganesh
BALAJI CHBALAJI CH
You can simply keep another IF condition to check whether the task's type is meeting or not.
I have changed the above code.
 
trigger TaskTrigger on Task (before insert) {
    
    set<string> Names = new set<string>();
    list<contact>ContactList = [select id, name from contact];
    
    for(contact c: [select id, Name from contact])
    {
        Names.add(c.Name);
    }
    
    list<contact>NewContactList = new list<contact>();
    for(task t : trigger.new)
    {
        if(t.TaskSubtype == 'Meeting') //This IF conditions checks whether task type is Meeting or not
        {
            if(Names.contains(t.New_key_Contact_Met__c)==true)
            {
                t.New_key_Contact_Met__c.addError('This contact already exists');
            }
            else
            {
                contact c = new contact();
                c.LastName = t.New_key_Contact_Met__c;
                NewContactList.add(c);
            }
        }
    }
    if(NewContactList.size() > 0)
        insert NewContactList;
}


Best Regards,
BALAJI
Ganesh PrasathGanesh Prasath
Hi Balaji,

  Thanks a lot Balaji for the tip , I had to  change the field to Type = 'Visits' and it is working now. Thanks a lot for the assistance.

Regards,
Ganesh
Ganesh PrasathGanesh Prasath
Hi Balaji

This is the trigger that I had ended up with and that is working

trigger TaskTrigger on Task (after update , before insert) {
    
    set<string> Emails = new set<string>();
    list<contact>ContactList = [select id, email from contact];
    
    for(contact c: [select id, Email from contact])
    {
        Emails.add(c.Email);
    }
    
    list<contact>NewContactList = new list<contact>();
    for(task t : trigger.new)
    
  {
        if( t.Type == 'Visits') //This IF conditions checks whether task type is Meeting or not

    if (t.New_Key_Contact_s_Email_address__c <> null)
  { 
    
    {
        if((Emails.contains(t.New_Key_Contact_s_Email_address__c) == true) )
            {
            t.New_Key_Contact_s_First_Name__c.addError('This contact already exists');
            t.New_Key_Contact_s_Last_Name__c.addError('This contact already exists');
            t.New_Key_Contact_s_Email_address__c.addError('This Email already exists');
            }
        else
        {
            contact c = new contact();
            c.FirstName = t.New_Key_Contact_s_First_Name__c;
            c.LastName = t.New_Key_Contact_s_Last_Name__c;
            c.Position__c = t.New_Key_Contact_s_Position__c;
            c.Email = t.New_Key_Contact_s_Email_address__c;
            NewContactList.add(c);
        }
    }
    
   }
 }
    if(NewContactList.size() > 0)
    insert NewContactList;

how ever when I tried to deploy it from the sand box to production it failed saying that the trigger doesnt have code coverage, I googled a bit and found that I have to write a test class , where do I have to write and how ?

Regards,
Ganesh