There are 2 Custom objects Position__c and Job_postings__c. They both have master detail relationship. where Position__c is master and job posting is detail.

Requirement: For a day only one Job posting should be created by the user for a particular Position. On the same day when the user is trying to create another Job posting for the same Position, error message should be thrown. But on the next day the user should be able to create a new Job posting for that same Position.

How can this be achieved using triggers?. Also i want to know whether this can be achieved with workflows and validation rule?.

  • 1
    So separate users should be able to create postings on the same day?
    – Adrian Larson
    Dec 31, 2016 at 16:37
  • 1
    As a workflow rule, you could assign a Field Update to update a Unique Index field that adds the date to the Position Id. The system will prevent duplicate indexes from being created. The only downside to that configuration is that the users will only receive a generic unique index error instead of a customized message.
    – sfdcfox
    Dec 31, 2016 at 16:38
  • Tried this solution and it worked as you mentioned. Thanks
    – Sisir
    Jan 1, 2017 at 17:41

2 Answers 2


You will have a trigger on the job posting object. When inserted (before insert), you should check if the position for that job posting already has other job postings. If yes, then you should get the time of the last one inserted. If the date equals today, then you cannot insert the object (using .addError method).

It would probably look like this:

trigger on Job_Posting__c (before insert) {
    if (Trigger.isBefore && Trigger.isInsert) {
        List<Id> positions_ids = new List<Id>();

        // get the positions ids
        for (Job_Posting__c j : Trigger.new) {

        // query all job postings for those positions
        List<Job_Posting__c> other_jobs = [SELECT Id, CreatedDate, CreatedById FROM Job_Posting__c WHERE Position__c IN positions_ids];

        for (Job_Posting__c j : Trigger.new) {
            for (Job_Posting__c existent_record : other_jobs) {
                // check if we are comparing two records for the same position
                // if the created date for the existing record equals the current date
                // (that is, the insertion date for the record on Trigger.new), then 
                // we can send that error to the user.
                if (j.Position__c == existent_record.Position__c && j.CreatedById == UserInfo.getUserId()) {
                    Date existent_record_created_date = existent_record.CreatedDate.date();

                    if (existent_record_created_date == Date.today()) {
                        j.addError('Cannot create job position because there\'s another one created on this date.');
  • You've omitted the criteria regarding the createdBy user. In his question he stated "one Job posting should be created by the user for a particular Position". The createdBy should be an essential part of your criteria.
    – crmprogdev
    Dec 31, 2016 at 16:05
  • @crmprogdev true. I've edited my answer. Thank you. Dec 31, 2016 at 16:56
  • 1
    Don't forget to name your trigger...
    – Adrian Larson
    Dec 31, 2016 at 23:28
  • Thank you so much. Got one error when I created this trigger. 'colon' was missing before positions_ids in the SOQL query. I inserted a colon before it and everything worked fine. Thanks.
    – Sisir
    Jan 1, 2017 at 17:29

My interpretation is that you want to limit the entire org to one Job Posting per Position. If you want to limit it on a per-user basis as well, that complicates things. But if that interpretation is correct, you can perform this validation without any Apex. You just need a Roll-Up Summary Field to save the MAX(CreatedDate) of your detail records and validate that the value is not today.

Roll-Up Summary Field

  1. Navigate to Setup > Create > Objects > Position.
  2. Under Custom Fields & Relationships, click the New button.
  3. Set the Label to Most Recent Posting.
  4. Select Job Posting as the Summarized Object.
  5. Select MAX as the Roll-Up Type.
  6. Select Created Date as the Field to Aggregate.
  7. Set the remaining configuration as you wish and save the field.

Validation Rule

On the Job Posting object, create this rule:

    ISNEW(), DATEVALUE(Position__r.Most_Recent_Posting__c) = TODAY()
  • 1
    Should probably add a check for ISNEW so the record can be edited if needed
    – Eric
    Jan 1, 2017 at 0:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .