+ Start a Discussion
Michael VinciMichael Vinci 

Preventing user from creating a task for a specific record type

Hello,


I was wondering if it is possible to prevent a user from creating a task object with a Related To field that is assigned to an account of a specific record type? Tried using validation rules but unfortunately there I cannot find a way to reference the record type of the object in order to create the conditions



Thanks
MV
Best Answer chosen by Michael Vinci
Narender Singh(Nads)Narender Singh(Nads)
Hi Michael,

You will have to write a trigger to achieve that since we can't access the recordtype for the related account record.

Your trigger will look like this:
trigger TaskValidation on Task (before insert) {
    if(trigger.new.size()==0){
            string AccID=trigger.new[0].whatid;
            if(AccID!=NULL && accId.startswith('001')){
                account acc=[select id,recordtypeid from account where id=:AccID ];
                if(acc.recordtypeid=='PUT YOUR RECORD TYPE ID HERE ')
                    trigger.new[0].adderror('Task creation not allowed for this type of Recordtype of Account');
            }
        }
}
 
 

All Answers

Raj VakatiRaj Vakati
yes .. remove those record type permission from the user profile .. 

https://help.salesforce.com/articleView?id=permissions_record_type_access.htm&type=5

https://help.salesforce.com/articleView?id=admin_recordtype.htm&type=5

https://help.salesforce.com/articleView?id=users_profiles_record_types.htm&type=5
Narender Singh(Nads)Narender Singh(Nads)
Hi Michael,

You will have to write a trigger to achieve that since we can't access the recordtype for the related account record.

Your trigger will look like this:
trigger TaskValidation on Task (before insert) {
    if(trigger.new.size()==0){
            string AccID=trigger.new[0].whatid;
            if(AccID!=NULL && accId.startswith('001')){
                account acc=[select id,recordtypeid from account where id=:AccID ];
                if(acc.recordtypeid=='PUT YOUR RECORD TYPE ID HERE ')
                    trigger.new[0].adderror('Task creation not allowed for this type of Recordtype of Account');
            }
        }
}
 
 
This was selected as the best answer