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
kirankumarreddy punurukirankumarreddy punuru 

how prevent inserting task record if contact is no longer with the firm using a trigger

Hi,
   I have a requirement to prevent saving the task record if the  contact is no longer with the firm , using the trigger.
   Can anyone helpme  how to write .

Thanks,
kiran.
Neetu_BansalNeetu_Bansal
Hi,

Have you written workflow or trigger for inserting task?

Thanks,
Neetu
kirankumarreddy punurukirankumarreddy punuru
Hi Neetu,
               I have wrote the trigger and it is preventing the new task insertion if the no longer with the firm check box is checked in contact .
               Now the trigger is working ,but when we click on the save button , it is showing the error which i have written in the trigger,Can we                        redirect from the task page to contact record page by writing any logic in the trigger??
             
                 My trigger is 

                     Trigger TaskInsertPrevention on Task(before insert)
         { 
                  Set<Id> taskSetId = new Set<Id>(); 
                  for(Task task :Trigger.New)
                     {
                          String s1 =task.WhoId;
                          if(s1.startsWith('003e'))
                              {
                                taskSetId.add(task.WhoId);
                               }
          
                   for(Contact  con :[Select Id , Contact.No_longer_with_firm__c  from  Contact where Id In : taskSetID])
                         {
                    if(con.No_longer_with_firm__c == True)
                         {
                        task.addError('You cannot create a new task if the contact is no longer with the firm');
                         }
                          }
                      }
          }   
Neetu_BansalNeetu_Bansal
Hi,

Yes this is what I want to explain you that where you are creating task (I guess trigger), there only you can put the check that if Contact is no longer with firm, don't insert task.

You cannot prevent by the above trigger you specified, it will always throw error.

Thanks,
Neetu
kirankumarreddy punurukirankumarreddy punuru
Hi Neetu ,
               I understood what u have said , can  we add a link in that error message to redirect to contact page.

Thanks,
Kiran.