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
Krishan Gopal 27Krishan Gopal 27 

how to validate on relationship field using trigger

facing trouble to one object to to another in trigger to solve that query
 
  • If Candidate is applying for a job whose status is Not Active(deactivate job). He should get an error, “This Job is not active. You can not apply for this job.

in this queston candidate object have a lookup field to select job  and according to job object  field active status . candidate shold able to select .

how to write query to check from old job data to a new candidate User-added imageUser-added image
Best Answer chosen by Krishan Gopal 27
Maharajan CMaharajan C
Hi,

If  Active__c field is Checkbok in Jobs object then use the below code:
 
trigger ActiveCheck on Candidate__c(before insert){
    set<Id> jobIdSet = new set<Id>();
    for(Candidate__c cb : Trigger.New){
        if(cb.jobs__c != null){
            jobIdSet.add(cb.jobs__c);
        }
    }
    if(!jobIdSet.IsEmpty()){
        Map<Id,jobs__c> jobMap = new Map<Id,jobs__c>([Select Id, Active__c from jobs__c where ID IN: jobIdSet]);
        for(Candidate__c c : Trigger.New){
            if(c.jobs__c != null){
                if(jobMap.get(c.jobs__c).Active__c == False){
                        c.addError('The Job is not Active. You cannot Apply for this job');
                }
            }
        }
    }
}

Thanks,
Maharajan.C

All Answers

CharuDuttCharuDutt
Hii Krishna
Try Below Code
trigger ActiveCheck on Candidate__c(before insert){
       for(Candidate__c C : trigger.new){
         if(C.Job__c != null && C.Job__r.Active__c != 'Active'){
            C.AddError(This Job Is not Active.Try Another Job);
      }
   }
}
Please Mark It As Best Answer If It Helps
Thank You! 
Maharajan CMaharajan C
Hi Krishan,

You can't access the parent objects details from trigger.New. Trigger.New will holds only the current record details.

Try the below code:
trigger ActiveCheck on Candidate__c(before insert){
    set<Id> jobIdSet = new set<Id>();
    for(Candidate__c cb : Trigger.New){
        if(cb.jobs__c != null){
            jobIdSet.add(cb.jobs__c);
        }
    }
    if(!jobIdSet.IsEmpty()){
        Map<Id,jobs__c> jobMap = new Map<Id,jobs__c>([Select Id, Active__c from jobs__c where ID IN: jobIdSet]);
        for(Candidate__c c : Trigger.New){
            if(c.jobs__c != null){
                if(jobMap.get(c.jobs__c).Active__c == 'false'){
                        c.addError('The Job is not Active. You cannot Apply for this job');
                }
            }
        }
    }
}

Thanks,
Maharajan.C
sunil yogisunil yogi

Please Copy and Paste below code and Check this. 

trigger ActiveCheck on Candidate__c(before insert){
       for(Candidate__c cb : trigger.new){
         if(cb.Job__c != null && cb.Job__r.Active__c == false){
            cb.AddError('This Job is not Active You cannot apply for this job');
      }
   }
}

 

Thanks

Maharajan CMaharajan C
Hi,

If  Active__c field is Checkbok in Jobs object then use the below code:
 
trigger ActiveCheck on Candidate__c(before insert){
    set<Id> jobIdSet = new set<Id>();
    for(Candidate__c cb : Trigger.New){
        if(cb.jobs__c != null){
            jobIdSet.add(cb.jobs__c);
        }
    }
    if(!jobIdSet.IsEmpty()){
        Map<Id,jobs__c> jobMap = new Map<Id,jobs__c>([Select Id, Active__c from jobs__c where ID IN: jobIdSet]);
        for(Candidate__c c : Trigger.New){
            if(c.jobs__c != null){
                if(jobMap.get(c.jobs__c).Active__c == False){
                        c.addError('The Job is not Active. You cannot Apply for this job');
                }
            }
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer