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
Rick RossiRick Rossi 

Trigger question on the task

I am running into issues on my trigger that is failing: It is supposed to prevent task creation related to an "inactive" contact and any time a user tries to create a task for an "inactive" contact. Any help would be great, this is what I have so far.

trigger TaskTrigger  on Task (before insert, before update) {

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

for(task t:trigger.new){
    Ctids.add(t.whatid);
}
if(!ctids.isempty()){
    List<Contact>lstacc=[select id,Status__c from contact where id in:ctids AND Status__c =:'Active'];
    Map<id,String>mapofcts=new Map<id,string>();
    for(Contact record: contacts) {
        mapofaccts.put(ct.id,Ct.Status__c);
    }
    for(task t:trigger.new){
        if(mapofaccts.containsKey(t.whatid)){
            t.addError ('Cannot Create a task For An In-Active Contact');
        }
    }
}

}
Abdul KhatriAbdul Khatri
Hi Rick

Please change t.WhatId to t.WhoId
 
trigger TaskTrigger  on Task (before insert, before update) {

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

for(task t:trigger.new){
    Ctids.add(t.whatid);
}

if(!ctids.isempty()){
    List<Contact>lstacc=[select id,Status__c from contact where id in:ctids AND Status__c =:'Active'];
    Map<id,String>mapofcts=new Map<id,string>();
    for(Contact record: contacts) {
        mapofaccts.put(ct.id,Ct.Status__c);
    }
    for(task t:trigger.new){
        if(mapofaccts.containsKey(t.whoid)){
            t.addError ('Cannot Create a task For An In-Active Contact');
        }
    }
}

}

I hope this help
Rick RossiRick Rossi
Hey! Thank you for the help. I am getting the following error:  Compile Error: Variable does not exist: contacts at line 12 column 25 ( for(Contact record: contacts) {

What would the resolution be for this? Any help would be great thanx!
Abdul KhatriAbdul Khatri
Hi Rick,

Looks like your code has the following issues, my be you copied from other possible acct trigger (my guess). Here are the problems why I am saying that
  1. In line for(Contact record: contacts), the contact list variable name is actually lstacc and not contacts, also in the code you are using ct so change the record to ct in the same line
  2. In line mapofaccts.put(ct.id, Ct.Status__c), variable name is mapofcts and not mapofaccts, also changed the same later in code on line mapofaccts.contains.. 

Please try the below code. I have fixed everthing, you can use code as is.
trigger TaskTriggerCreateInactiveContact on Task (before insert, before update) 
{
	set<id> Ctids = new set<id>();

    for(task t:trigger.new){
        Ctids.add(t.whoid);
    }

	if(!Ctids.isEmpty())
    {
    	List<Contact> lstacc = [select id,Status__c from contact where id in:ctids AND Status__c = 'Active'];
    	Map<id,String> mapofcts = new Map<id,string>();
    	for(Contact ct: lstacc) {
        	mapofcts.put(ct.id,Ct.Status__c);
    	}
    	for(task t:trigger.new){
        	if(mapofcts.isEmpty() || !mapofcts.containsKey(t.whoid)){
            	t.addError ('Cannot Create a task For An In-Active Contact');
        	}
    	}
	}
}

I hope this help
Abdul KhatriAbdul Khatri
Hi Rick,

Was the solution helpful?