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
DevSFDevSF 

Make Fields Required Only Before Saving a Record

Hi Friends,

My scenario is When the owner of the record is changed.
Then for the new owner, there are some mandatory fields that the user has to be filled before saving the record.

How do i make the fields required only before saving the records.

How could i achieve this.....?
Veenesh VikramVeenesh Vikram
You can try using Validation Rule's ISCHANGED() method.

Like, 
ISCHANGED(OwnerId) && ISBLANK(YOUR_Field1) && ISBLANK(YOUR_Field2)

This way, you are enforcing Validation Rule to fire whan the owner is changed.

Regards
Veenesh
Nachu RY 4Nachu RY 4
Please try the below trigger:

 
trigger tname on Account (before update) {
    
   for(account acc: trigger.new){
      
       if(acc.ownerid != trigger.oldmap.get(acc.id).ownerid){
           acc.Nachu__check__c= true;
       }
       
        if(acc.ownerid == trigger.oldmap.get(acc.id).ownerid && accnew.Nachu__check__c == true && yourfield == null){ 
           acc.adderror('Please fill the requiredfield');
       }
       
       
       if(acc.ownerid == trigger.oldmap.get(acc.id).ownerid && accnew.Nachu__check__c == true && yourfield != null){ 
                      acc.Nachu__check__c= flase;

       }
   }  
   
}
Patrick GasperszPatrick Gaspersz
Hello,

1. You need a way to distinct that fields become mandatroy when a certain process step or field change has ocured.
 2. Then you need a validation to check whether the mandatory fiels are filled

A very basic way to achieve this:
1. Creating a reference on "Case" to a specific characteristic of the new Owner. Create a formula refering to the field using the User$ relationship.
2. Create Validation Rules on Cases. AND([new formulafield] = "VALUE", ISBLANK([Mandatory Field]))

I created a simple example. If the first name of the case owner is "Roos", then the field [Discription] should be required.
1. Formula field:
- name: Owner_First_Name
- formula: $User.First_Name
2. Validation: AND(Owner_Field_Name = "Roos", ISBLANK(Description))

Groeten,

Patrick


 



 
Naresh YadavNaresh Yadav
Hi Salesforce Dev 9

See the below example.
Suppose currently you have owner1 and owner2 and some fields like field1 , field2, field3.
So when owner1 changed to owner2 then field2 and field3 is required for this user.
 
if(ownerId == owner2){
     if(field2 == null || string.isBlank(field2)){
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'This field is required.'));
     }
      if(field3 == null || string.isBlank(field3)){
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'This field is required.'));
     }
}
Nachu RY 4Nachu RY 4
Please try the below trigger:

You have to create the checkbox in your object. then try the below code.
 
trigger tname on Account (before update) {
    
   for(account acc: trigger.new){
      
       if(acc.ownerid != trigger.oldmap.get(acc.id).ownerid){
           acc.Nachu__check__c= true;
       }
       
        if(acc.ownerid == trigger.oldmap.get(acc.id).ownerid && accnew.Nachu__check__c == true && yourfield == null){ 
           acc.adderror('Please fill the requiredfield');
       }
       
       
       if(acc.ownerid == trigger.oldmap.get(acc.id).ownerid && accnew.Nachu__check__c == true && yourfield != null){ 
                      acc.Nachu__check__c= flase;

       }
   }  
   
}