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 

Help me with the trigger

Need to block the user for updating the address field more that 5 times, store the number of times allowed in a cutom label and use in trigger, if require create a custom fields to track number of changes so far completed.
SwethaSwetha (Salesforce Developers) 
HI Visi,
Try below approach

Step 1:
Create a Custom Label:
Go to Setup -> Custom Labels -> New Custom Label.
Provide a Label Name, such as "Max_Address_Changes".
Set the value to 5 (the maximum allowed number of address changes).

Step 2:
Create a Custom Field to Track Changes:
Create a custom Number field on the contact object  to store the count of address changes.
Name the field something like "Address_Change_Count__c".
Set the initial value of the field to 0. (Else trigger will give null pointer exception)
In the trigger, increment this field whenever the address is updated.

Step 3:
Trigger:
trigger AddressUpdateTrigger on Contact (before update) {
    Integer maxChanges = Integer.valueOf(Label.Max_Address_Changes);
    
    for (Contact contact : Trigger.new) {
        if (contact.MailingStreet != Trigger.oldMap.get(contact.Id).MailingStreet) {
            // Address field is being updated
            if (contact.Address_Change_Count__c >= maxChanges) {
                contact.addError('You have reached the maximum allowed address changes.');
            } else {
                contact.Address_Change_Count__c += 1; // Increment the change count
            }
        }
    }
}


Upon implementation, it would throw error upon crossing 5 updates to MailingStreet

User-added image


If this information helps, please mark the answer as best. Thank you