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
Pratik MungalparaPratik Mungalpara 

Apex trigger on contact object to update field null when field have lookup relationship

I am new to coding and have a question.

Reuirement is: I have custom field (checkbox) named " inactive". another standard field on same object named "Report to". "Report To" field have lookup relationship to contact itself. 

I want to remove any value in "report to" or i would say i want to make "report to" field null if "inacitve" field is checked. 

Can you help me write trigger for it. 
ShirishaShirisha (Salesforce Developers) 
Hi Pratik,

Greetings!

Please refer the sample code given in this blog (https://developer.salesforce.com/forums/?id=9060G000000IBlvQAG).

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
sachinarorasfsachinarorasf
Hi Pratik,

Use the below trigger for your requirement it may helpful for you

trigger TriggerForUpdateContactReport on Contact(before insert, before update) {
    if( (trigger.isUpdate && trigger.isBefore) || (trigger.isInsert && trigger.isBefore)){
        for(Contact con : trigger.new){
            if(con.Inactive__c == true){
                con.Report_To__c = '';
            }
        }
    }
}



I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com