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
Arun Kumar 271Arun Kumar 271 

Disable a button in standard Account object based on a custom picklist field value

Hi,

Salesforce standard 'account' object has lookup relationship with standard 'contacts' object. i see a button 'new contacts' for adding contacts to the account record. I have a custom picklist field in standard object named 'status', (values: 'active', 'inactive' and 'to be remediated'),i want to ensure no contacts can be added to the account if the account 'status' picklist value is 'inactive'.

How do i do that? i am new to salesforce and appreciate your time in helping me, Thanks in advance.
Swaraj Behera 7Swaraj Behera 7
Hi Arun,
Please use below trigger.It will work as per your requirement.
 
trigger ContactTrigger on Contact (before insert)
{

    Set<ID> setaccId= new Set<Id>();
    
    for ( Contact t : Trigger.new )
       
    {

         System.debug(t.accountid+'test accountid');
        if ( t.accountid!=null)
        {
               
                setaccId.add(t.accountid);

            }   
        }
        System.debug(setaccId+'set account ids ');
    

    
    if(setaccId.size() > 0 )
    {
    
        Map<Id,account> mapacc = new Map<Id,account>( [Select id,Status__c from account where id=:setaccId ] );
        System.debug(mapacc+'map account');
        for ( contact t : Trigger.new )
        {
            if ( t.accountid !=null && mapacc.containsKey(t.accountid) )
            {
                account acc = mapacc.get(t.accountid);
                
                if(acc.Status__c=='inactive')
                {
                    t.addError('You can not add a Contact if related account status is inactive ');
                }
            }
        }    
    }
}

Thanks,
Swaraj Behera