ForceChallenges
ProblemsDailyNotesInterview
Sign in

Practice

All problemsSOQL practiceApex trigger challengesDaily challengeOpportunityTrigger.oldMapBefore UpdateBulkificationContactValidationGROUP BYSemi-JoinAccountBefore Insert

Interview questions

All questionsAdminAdministrationApexApex TriggerAsync ApexDevelopmentField ServiceField ServiceIntegrationLwc

By company

DeloitteSalesforceAccentureInfosysPwCCognizant

Learn

Study notes

ForceChallenges

AboutContact usPrivacy PolicyTerms of ServiceFor AI assistants (llms.txt)
© 2026 ForceChallenges · Salesforce SOQL & Apex trigger practice
Development questions66 of 102
DevelopmentMedium 1 min read

Write a trigger on a custom junction object Project_Resource__c to prevent linking the same Contact to the same Project__c more than once.

Asked atDeloittePersistentZS Associates

Answer

Use before insert, before update on the junction object. Compare against existing junction records and others in the same batch using a Contact+Project key.

apex
trigger PRTrigger on Project_Resource__c (before insert, before update) {
    Set<Id> projects = new Set<Id>(), contacts = new Set<Id>();
    for (Project_Resource__c r : Trigger.new) { projects.add(r.Project__c); contacts.add(r.Contact__c); }
    Set<String> keys = new Set<String>();
    for (Project_Resource__c e : [SELECT Project__c, Contact__c FROM Project_Resource__c
                                  WHERE Project__c IN :projects AND Contact__c IN :contacts AND Id NOT IN :Trigger.new]) {
        keys.add(e.Project__c + '|' + e.Contact__c);
    }
    for (Project_Resource__c r : Trigger.new) {
        if (!keys.add(r.Project__c + '|' + r.Contact__c)) r.addError('This Contact is already linked to the Project.');
    }
}

A duplicate/unique text field (e.g. a formula-populated external key with Unique) is a declarative alternative.

apex-triggertriggercodingscenariojunctionduplicates
Practice SOQL & Apex
PreviousWrite a trigger that calculates the average Rating_Score__c from Feedback__c child records and updates the parent Product2 record using AggregateResult.Next When an Account is restored from the Recycle Bin (after undelete), append " - Restored" to its Name and send a notification to the System Admin.

More Development questions

  • What is reactivity in LWC and when does a field trigger a re-render?
  • What are the best practices to improve LWC performance?
  • How do you unit test an LWC with Jest?
  • How do you use a third-party JavaScript library or static resource in LWC?
  • Why can renderedCallback run multiple times and how do you prevent repeated execution?