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
Chris HustedChris Husted 

Help with Apex Trigger for Contacts

Hello -

I have a new trigger in place to prevent users from deleting of contacts from our environment.

Here is a copy of my code:

trigger DeletePrevention on Contact (before delete) {

    for (Contact mo : Trigger.old) {

        mo.addError('Youre Not Allowed to Delete Client Contacts');
    }
}

However, I need to figure out to restrict this to ONLY one RecordType of Contact ('Client').

Any suggestions or help would be greatly appreciated.

Thanks!
 
Best Answer chosen by Chris Husted
Vivek DeshmaneVivek Deshmane
Hi Chris,
Please try following code.
trigger DeletePrevention on Contact (before delete) {
    //Retrive Client Record Type Id
    public  Id clientRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client').getRecordTypeId();

    for (Contact mo : Trigger.old) {

        if(mo.recordTypeId != null && (clientRecordTypeId !=null &&  clientRecordTypeId == mo.recordTypeId))
        {
         mo.addError('Youre Not Allowed to Delete Client Contacts');
        } 
    }
}
Best Regards,
-Vivek
Vivek.deshmane@gmail.com

All Answers

Vivek DeshmaneVivek Deshmane
Hi Chris,
Please try following code.
trigger DeletePrevention on Contact (before delete) {
    //Retrive Client Record Type Id
    public  Id clientRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client').getRecordTypeId();

    for (Contact mo : Trigger.old) {

        if(mo.recordTypeId != null && (clientRecordTypeId !=null &&  clientRecordTypeId == mo.recordTypeId))
        {
         mo.addError('Youre Not Allowed to Delete Client Contacts');
        } 
    }
}
Best Regards,
-Vivek
Vivek.deshmane@gmail.com
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you.
trigger DeletePrevention on Contact (before delete) {

	Set<String> setId = new Set<String>();
    for (Contact mo : Trigger.old) {
		setId.add(mo.id);
	}	

	List<Contact> lstContact = [select id,recordType.Name from Contact where id in : setId];
	
    for (Contact mo : lstContact) 
	{
		
		if(mo.recordType.Name =='Client')
		{
			mo.addError('Youre Not Allowed to Delete Client Contacts');
		}	
    }
}

Please let us know if this will help you.

Thanks,
Amit Chaudhary
 
Chris HustedChris Husted
Thanks Vivek and Amit, both work great!

However, I'm running into an issue with my Apex class (test coverage) needed to deploy the trigger to production. 

If possible, can you help?
Vivek DeshmaneVivek Deshmane
Hi Chris,

Please try below code.Hope this will help in test coverage
@isTest
    private class NoDeleteonContactTest 
    {
        private static testmethod void NoDeleteonClientContactTest()
        {      
              
               Test.startTest();
               try{
               Id clientRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client').getRecordTypeId();
               Account Acct = new Account(Name = 'Test');
               insert Acct;

               contact cont = new contact(Accountid = Acct.id, LastName='Test',RecordTypeId=clientRecordTypeId);
               insert cont;
                        
               delete cont;
               }catch (Exception e) {
                System.assert(e.getMessage().contains('Youre Not Allowed to Delete Client Contacts'));
                }
               Test.stopTest();
               

        }


    }
    
    Please let me know if this help you and mark the best answer
    Best Regards,
    -Vivek
    vivek.deshmane@gmail.com