+ Start a Discussion
Ewa FrystEwa Fryst 

validation rule trigger

HI All,
I would like to prevent a user from creating a task against transactional accounts (Account Record Type = TransactionalAccount). So all the other Account records types are fine, just transactional should not have tasks. I found the trigger below. Could anyone change it for me with the Record type instead of the Type picklist? I will also need one like that for Events. I would appreciate your help.
Many thanks
Ewa

trigger TaskCanbeCreatedforSoldtoOnly 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,Type from account where id in:accids AND Type=:'Bill To Customer']; Map<id,String>mapofaccts=new Map<id,string>(); for(account acc:lstacc){ mapofaccts.put(acc.id,acc.Type); } for(task t:trigger.new){ if(mapofaccts.containsKey(t.whatid)){ t.addError ('Please select the Sold to Customer'); } } } }
Best Answer chosen by Ewa Fryst
Maharajan CMaharajan C

Hi Ewa,

Please use the below trigger:

Task:

trigger TaskCanbeCreatedforSoldtoOnly on Task (before insert, before update) 
{ 
    set<id>accids=new set<id>(); 
    for(task t:trigger.new){
        accids.add(t.whatid); 
    } 
    if(!accids.isempty()){
        // Use the Recordtype label below
        Map<Id,Account> mapofaccts = new Map<Id,Account>([select id,RecordType.Name from account where id in:accids AND RecordType.Name='TransactionalAccount']);
        for(task t:trigger.new){
            if(mapofaccts.containsKey(t.whatid))
            { 
                // Change the Error Message below as you want
                t.addError ('Please select the Sold to Customer'); 
            } 
        } 
    } 
}

Event: 
 
trigger TaskCanbeCreatedforSoldtoOnly on Event (before insert, before update) 
{ 
    set<id>accids=new set<id>(); 
    for(Event t:trigger.new){
        accids.add(t.whatid); 
    } 
    if(!accids.isempty()){
        // Use the Recordtype label below
        Map<Id,Account> mapofaccts = new Map<Id,Account>([select id,RecordType.Name from account where id in:accids AND RecordType.Name='TransactionalAccount']);
        for(Event t:trigger.new){
            if(mapofaccts.containsKey(t.whatid))
            { 
                // Change the Error Message below as you want
                t.addError ('Please select the Sold to Customer'); 
            } 
        } 
    } 
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C

Hi Ewa,

Please use the below trigger:

Task:

trigger TaskCanbeCreatedforSoldtoOnly on Task (before insert, before update) 
{ 
    set<id>accids=new set<id>(); 
    for(task t:trigger.new){
        accids.add(t.whatid); 
    } 
    if(!accids.isempty()){
        // Use the Recordtype label below
        Map<Id,Account> mapofaccts = new Map<Id,Account>([select id,RecordType.Name from account where id in:accids AND RecordType.Name='TransactionalAccount']);
        for(task t:trigger.new){
            if(mapofaccts.containsKey(t.whatid))
            { 
                // Change the Error Message below as you want
                t.addError ('Please select the Sold to Customer'); 
            } 
        } 
    } 
}

Event: 
 
trigger TaskCanbeCreatedforSoldtoOnly on Event (before insert, before update) 
{ 
    set<id>accids=new set<id>(); 
    for(Event t:trigger.new){
        accids.add(t.whatid); 
    } 
    if(!accids.isempty()){
        // Use the Recordtype label below
        Map<Id,Account> mapofaccts = new Map<Id,Account>([select id,RecordType.Name from account where id in:accids AND RecordType.Name='TransactionalAccount']);
        for(Event t:trigger.new){
            if(mapofaccts.containsKey(t.whatid))
            { 
                // Change the Error Message below as you want
                t.addError ('Please select the Sold to Customer'); 
            } 
        } 
    } 
}

Thanks,
Maharajan.C
This was selected as the best answer
Ewa FrystEwa Fryst
Hi Maharajan,
That works perfectly. Thank you so much! I appreciate it.
Ewa