+ Start a Discussion
khushi Yadavkhushi Yadav 

dependent first field to another field and vice versa in salesforce

In Candidate Object. 
If user enters the First name Field then Last name should be Mandatory. & If user enters the Last name Field then First name should be Mandatory.
Pavan WaghmodePavan Waghmode
Hi  Khushi,

Create validation rule on candidate object. below is syntax for the same

IF(NOT(ISBLANK(FirstName)),ISBLANK(LastName) ,IF(NOT(ISBLANK(LastName)),ISBLANK(FirstName),false))

Thanks 
Pavan
khushi Yadavkhushi Yadav
I write this rule and it works
OR(
(First_name__c ="" && Not(ISBLANK(Last_Name__c))),
(Last_Name__c ="" && Not(ISBLANK(First_name__c)))
)
tuaroivao nhamnhientuaroivao nhamnhien

Thanks a lot!!!!! 
-------------------------
iPrice.vn (https://iprice.vn/xiaomi/dien-thoai-may-tinh-bang/dien-thoai-thong-minh/)

Deepali KulshresthaDeepali Kulshrestha
Hi Khushi,

Greetings to you!

Use the below code to solve your problem.
And you can modify it accordingly.
 
Trigger--
trigger CandidateTrigger on Candidate__c (before insert, before update) {
     if(trigger.isBefore){
        if(Trigger.isInsert){
           MandatoryField.fillMandatoryField(trigger.new);
        }
       if(trigger.isUpdate){
         MandatoryField.fillMandatoryField(trigger.new);  
    }
}
}

Apex controller--

public class MandatoryField {
    public static void fillMandatoryField(List<Candidate__c> candidateList){
        try{
        for(Candidate__c candidateObj : candidateList){
            if(candidateObj.First_Name__c == Null){
                candidateObj.addError('Please fill the first Name of contact');
            }
            else{
                if(candidateObj.Last_Name__c == Null){
                  candidateObj.addError('Please fill the Second Name of contact');
                }
            }
        }
        }catch(Exception ex){
            System.debug('Exception in code ::'+ex.getCause()+'Exception in line number ::'+ex.getLineNumber());
        }
    }
}

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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com