Skip to main content
Danilo Ong ha preguntado en #Apex
Hello! I'm creating a before update Trigger that won't allow changing the agreement_start_date and agreement_end_date of an agreement when the jobs associated with it are not within the duration of the new dates.

Background:

- An agreement needs an agreement_start_date and agreement_end_date as its duration. 

- An agreement can have multiple Jobs assigned to it and every Job only has one service agreement assigned.

- Every Job has a job_start_date and job_end_date and is within the duration of the agreement_start_date and agreement_end_date.

Agreement_ABC

agreement_start_date = June 1, 2018

agreement_end_date = July 31, 2018

1. Job_01: 

job_start_date = July 1, 2018

job_end_date = July 15, 2018

2. Job_02:

job_start_date = June 2, 2018

job_end_date = June 30, 2018

Agreement_ABC New Dates

agreement_start_date = June 5, 2018

agreement_end_date = July 30, 2018

---> display an error disallowing to save as the new start date conflicts with Job_02 job_start_date

Scenario:

I want to update the agreement_start_date and agreement_end_date with new date values, however, a Trigger will not allow them to save the new dates if jobs associated to it are not within the new date duration.

How can I maximise the use of Set, List, and Map to develop the Trigger? Thanks.
12 respuestas
  1. 11 jul 2018, 6:34
    Hi Danilo,

    As per your requirement I have written the trigger below:

    trigger checkDates on Agreement_ABC__c(before update) {

    Set<Id> setOfAgreements = new Set<Id>();

    for(Agreement_ABC__c agreement : trigger.new){

    //This conditon will check if start date or end date is updated on Agreement

    if(trigger.oldMap.get(agreement.id).agreement_start_date__c != agreement.agreement_start_date__c || trigger.oldMap.get(agreement.id).agreement_end_date__c != agreement.agreement_end_date__c) {

    //This set will store ids of only those agreements where start date or end date is updated

    setOfAgreements.add(agreement.id);

    }

    }

    //If there is any agreement whose start date or end date is updated then we will execute this block

    if(setOfAgreements.size() > 0) {

    //This will contains the ids of all the agreements which consists of an error.

    Set<Id> setOfAgreementWithError = new Set<Id>();

    Date agreementStartDate;

    Date agreementEndDate;

    Integer counterForStartDate;

    Integer counterForEndDate;

    //This will query all the agreements with their respective Jobs

    //Replcae Jobs__r with child relationship name

    for(Agreement_ABC__c agreement : [Select Id, (Select job_start_date__c, job_end_date__c from Jobs__r ) from Agreement_ABC__c where Id IN :trigger.new]) {

    //Fetch updated start date and end date from trigger.new map

    agreementStartDate = trigger.newMap.get(agreement.id).agreement_start_date__c;

    agreementEndDate = trigger.newMap.get(agreement.id).agreement_end_date__c;

    counterForStartDate = 0;

    counterForEndDate = 0;

    for(Job__c job : agreement.Jobs__r) {

    if(agreementStartDate <= job.job_start_date__c) {

    counterForStartDate++;

    }

    if(agreementEndDate >= job.job_end_date__c) {

    counterForEndDate++;

    }

    }

    if(agreement.Jobs__r.size() > 0 && (counterForStartDate != agreement.Jobs__r.size() || counterForEndDate != agreement.Jobs__r.size()) ) {

    setOfAgreementWithError.add(agreement.id);

    }

    }

    for(Agreement_ABC__c agreement : trigger.new) {

    if(setOfAgreementWithError.containsKey(agreement.id)) {

    agreement.addError('Enter your Error Message here');

    }

    }

    }

    }

    //Please take care of the API names for all the field and objects.

    Please let me know if you face any issue with this and if you need any further help or information on this.

    Thanks,

    Abhishek Bansal.
0/9000