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
nmnbawanmnbawa 

restrict activity related list

I have a picklist on account object called "Status"and one of the values is "Duplicate"

I want to prevent the users from creating any tasks/events against an account when the status is duplicate.


Any ideas how to achieve it.


I cannot create a validation rule on the Activity object as it does not allow to reference custom account fields.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
trigger accountBlocker on Task (after insert) {
    map<id,account> accounts = new map<id,account>();
    for(task record:trigger.new)
        if(record.accountid!=null)
            accounts.put(record.accountid,null);
    for(task record:trigger.new)
        if(record.whatid!=null&&record.whatid.getsobjecttype()==account.sobjecttype)
        accounts.put(record.whatid,null);
    accounts.putall([select id,status__c from account where id in :accounts.keyset()]);
    for(task record:trigger.new)
        if(
           (record.accountid != null &&
            accounts.get(record.accountid).status__c=='Duplicate') ||
           (record.whatid != null && 
            record.whatid.getsobjecttype()==account.sobjecttype &&
            accounts.get(record.whatid).status__c=='Duplicate')) {
        record.whatid.addError('You cannot assign a task to a duplicate accoiunt.');
    }
}

Then, modify this trigger for the Event object, too.

All Answers

sfdcfoxsfdcfox
trigger accountBlocker on Task (after insert) {
    map<id,account> accounts = new map<id,account>();
    for(task record:trigger.new)
        if(record.accountid!=null)
            accounts.put(record.accountid,null);
    for(task record:trigger.new)
        if(record.whatid!=null&&record.whatid.getsobjecttype()==account.sobjecttype)
        accounts.put(record.whatid,null);
    accounts.putall([select id,status__c from account where id in :accounts.keyset()]);
    for(task record:trigger.new)
        if(
           (record.accountid != null &&
            accounts.get(record.accountid).status__c=='Duplicate') ||
           (record.whatid != null && 
            record.whatid.getsobjecttype()==account.sobjecttype &&
            accounts.get(record.whatid).status__c=='Duplicate')) {
        record.whatid.addError('You cannot assign a task to a duplicate accoiunt.');
    }
}

Then, modify this trigger for the Event object, too.

This was selected as the best answer
nmnbawanmnbawa

Thanks a ton.

 

Can you please provide the test class for code coverage as well.

sfdcfoxsfdcfox
static testmethod void test() {
  account a = new account(name='test',status__c='duplicate');
  insert a;
  try {
    task t = new task(subject='test',whatid=a.id,activitydate=date.today());
    insert t;
  } catch(exception e) {

  }
}

Possibly a few more fields, depending on your system's validation, but that should just about do it.