+ Start a Discussion
Priya AakankshaPriya Aakanksha 

I want to write one trigger on task related to account object

I want to write one trigger on task 
whenever we create any account name as priya then that user cannot create any task.

Please provide me some suggestion.

Thanks,
Priya Aakanksha
AnkaiahAnkaiah (Salesforce Developers) 
Hi Priya,

Please use the below trigger code will work for you.
trigger Accountcheck  on Task (before insert, before update) {

set<id>accids=new set<id>();

for(task t:trigger.new){
    accids.add(t.whatid);
}
if(!accids.isempty()){
    List<Account>lstacc=[select id,Name from account where id in:accids AND Name=:'Priya'];
    Map<id,String>accmap=new Map<id,string>();
    for(account acc:lstacc){
        accmap.put(acc.id,acc.Name);
    }
    for(task t:trigger.new){
        if(accmap.containsKey(t.whatid)){
            t.addError ('you cant able to create a task for account priya');
        }
    }
}

}

If this helps, Please mark it as best answer

Thanks!!