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
snehil kumarsnehil kumar 

Trigger to count the number of job available for specific post.

HI All,
Help me out with this trigger,
Thanks in Advance
Job has a field called Active (checkbox), If job is active and Candidate for that Job is being hired, trigger should calculate total number of Candidates hired so far including the current one and check if it exceeds or equals to number of open position on the Job. If it exceeds or equals, the Job's Active fields should be set to false (unchecked) so the Job is taken out from Active Job lists hence Candidates can not apply for it anymore.
Job manager should be allowed to hire n number of Candidates. Where n= Number of Open Positions. Throw error when Job Manager Hire Candidates beyond Number of Open Positions limit stating "Sorry, No vacancies available for this Job"
Once Job is deactivated, new Candidates should not be allowed to apply for particular deactivated job. Provide error for such Candidates who are applying for Deactivated Job stating "Sorry, Job does not exist".
If more than one Candidate is being hired for the Job at a time, throw an error for such .
Candidates stating "At a time only one Candidate can be hired for a Job"
Christan G 4Christan G 4
Hi Sneil, I hope you are well. Based on your request, can you please provide the following information / answer the following questions:
  1. API of the Active checkbox field
  2. Are Candidates stored in a separate object? If yes, what is its API Name?
  3. For the part that states, "If more than one candidate is being hired for the job at a time, throw an error stating that, "At a time only one Candidate can be hired for a Job" - Wouldn't the exceptions you mentioned regarding limiting how many people can apply take care of this requirement?
Hostars MastersHostars Masters
Hi, if you want to choose a new direction of development for yourself then you should really think about cryptocurrencies because they really allow you to catch a good luck at a high risk. On https://cryptopotato.com/bitmedia-crypto-ad-network-making-advertising-easier/ you can get acquainted with new opportunities and get a clear development strategy that will allow you to achieve the desired.
Serena WarrnerSerena Warrner
Certainly! Here's an example trigger in Apex code that implements the logic you described:


trigger CandidateTrigger on Candidate__c (before insert) {
    Map<Id, Job__c> jobs = new Map<Id, Job__c>();
    Set<Id> jobIds = new Set<Id>();
    Map<Id, Integer> jobPositions = new Map<Id, Integer>();
    
    // Collect Job records and their open positions
    for (Candidate__c candidate : Trigger.new) {
        if (candidate.Job__c != null) {
            jobIds.add(candidate.Job__c);
        }
    }
    
    jobs = new Map<Id, Job__c>([SELECT Id, Active__c, OpenPositions__c, (SELECT Id FROM Candidates__r) FROM Job__c WHERE Id IN :jobIds]);
    
    // Perform validation and update Active field if necessary
    for (Candidate__c candidate : Trigger.new) {
        if (candidate.Job__c != null) {
            Job__c job = jobs.get(candidate.Job__c);
            
            if (job != null) {
                if (!job.Active__c) {
                    candidate.addError('Sorry, Job does not exist.');
                } else if (job.Candidates__r.size() > 0) {
                    candidate.addError('At a time only one Candidate can be hired for a Job.');
                } else {
                    Integer hiredCandidates = jobPositions.containsKey(job.Id) ? jobPositions.get(job.Id) : 0;
                    hiredCandidates++;
                    
                    if (hiredCandidates >= job.OpenPositions__c) {
                        job.Active__c = false;
                        jobPositions.put(job.Id, hiredCandidates);
                    } else {
                        jobPositions.put(job.Id, hiredCandidates);
                    }
                }
            }
        }
    }
    
    update jobs.values(); // Update the modified Job records
}


Please note that the code assumes you have a custom object called Candidate__c and a custom object called Job__c. Adjust the field and object names to match your specific Salesforce configuration.    https://www.yourtexasbenefits.bid/

This trigger runs before inserting Candidate records and performs the necessary validations and updates on related Job records. It checks if the Job is active, counts the number of Candidates already hired, and updates the Active field based on the conditions you described.

Make sure to test this trigger thoroughly in a development or sandbox environment before deploying it to a production org.
Alex HeyyAlex Heyy
it's interesting