You need to sign in to do that
Don't have an account?
Ganesh 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
Regards,
Ganesh
Please find below revised code and let me know if that helps you.
Best Regards,
BALAJI
All Answers
This can be done using Trigger. Please find below sample code and let me know if that works for you.
Best Regards,
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
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
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.
Please find below revised code and let me know if that helps you.
Best Regards,
BALAJI
Thanks , I will check this and get back to you .
Thanks a lot , much appreciated.
Regards,
Ganesh
I had to make a few minor changes but it Worked !!! .
Thanks a lot Balaji.
Regards,
Ganesh
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
I have changed the above code.
Best Regards,
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
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