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
darrelndarreln 

Trigger to set task 'public' checkbox based on Contact record type

Hello all,

been doing some research, and understand that I cannot directly reference a contact or account record type from the task object. Here is my need:

I created a new record type for the contact object, let's call it ABC. Anytime a user creates a activity of type 'task' where the Parent object is a contact and the record type is ABC, I want to make sure the user clicks the "Public" checkbox on the task record when he/she saves the record. 

I was thinking either show an error if it's not checked, or simply set the checkbox to TRUE when the record is saved, regardless of whether the user remembered or not. 

From what I understand, this will require a trigger to be written. Looking for some help / sample trigger code that would get me there, as I am not a programmer :|

thank you!
 
Best Answer chosen by darreln
Varun SinghVarun Singh
Hi Darrel!
IsViseibleInSelfService is  api name  of  public filed.
  if you want  to create custom public  checkboc then  use public__c inseted of IsVisibleInSelfService  in this code

Use  this  code  to show  error  message  when  contact is "ABC" type and public  is not  checked

 
trigger PublicFORABC on Task (after insert,after Update) {
    Set<Id> ids=new Set<Id>();
    Set<Id> Conids=new Set<Id>();
    for(Task t:trigger.new){
    ids.add(t.id);
        if(t.WhoId != null && (((String)t.WhoId).substring(0,3) == '003') ) {
        conIds.add(t.WhoId);
        }
    }
    List<Contact> con=[Select id from contact where id in:conIds and RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'ABC')];
   
    for(Task t:trigger.new){
        if(t.IsVisibleInSelfService<> true && con.size()>0 ) {
                    t.AddError('Public filed Should be checked');

        }
    }
}
I hope  this  is  helpful  for  you
 

All Answers

Varun SinghVarun Singh
Hi Darrel!
IsViseibleInSelfService is  api name  of  public filed.
  if you want  to create custom public  checkboc then  use public__c inseted of IsVisibleInSelfService  in this code

Use  this  code  to show  error  message  when  contact is "ABC" type and public  is not  checked

 
trigger PublicFORABC on Task (after insert,after Update) {
    Set<Id> ids=new Set<Id>();
    Set<Id> Conids=new Set<Id>();
    for(Task t:trigger.new){
    ids.add(t.id);
        if(t.WhoId != null && (((String)t.WhoId).substring(0,3) == '003') ) {
        conIds.add(t.WhoId);
        }
    }
    List<Contact> con=[Select id from contact where id in:conIds and RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'ABC')];
   
    for(Task t:trigger.new){
        if(t.IsVisibleInSelfService<> true && con.size()>0 ) {
                    t.AddError('Public filed Should be checked');

        }
    }
}
I hope  this  is  helpful  for  you
 
This was selected as the best answer
darrelndarreln
Varun, this works beautifully, thank you so much for your assistance!
 
darrelndarreln
Varun, when I transfer this trigger to Prod, I get a code coverage error (says i need at least 1%, currently at 0%. What does that mean, can you help me with that?