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
VisithraVisithra 

I have got stuck in the trigger code,

Please help me with the trigger code for the below scenario,
When an lead is updated status as closed-Not converted, the description field  should be entered otherwise it should block the user to update lead.

 
SubratSubrat (Salesforce Developers) 
Hello Visi ,

To implement the desired trigger code, you can use the following Apex trigger for the Lead
trigger PreventLeadUpdate on Lead (before update) {
    for (Lead lead : Trigger.new) {
        if (lead.Status == 'Closed - Not Converted') {
            if (String.isBlank(lead.Description)) {
                lead.Description.addError('Please enter a description before closing the lead.');
            }
        } else {
            lead.Description = null; // Clear the description field if the status is not 'Closed - Not Converted'
        }
    }
}
This trigger fires before the update operation on Lead records. It checks if the lead's status is set to 'Closed - Not Converted'. If it is, it verifies that the description field is not blank. If the description is empty, it adds an error message to prevent the update.

If the status is not 'Closed - Not Converted', it clears the description field to ensure it remains empty.


If this helps , please mark this as Best Answer.
Thank you.