I have a simple trigger that sets the default value on a lookup field on Object B from Object C.

trigger UpdateQuestionSet on Job__c (before insert, before update) {
  for (Job__c questions: Trigger.new){
    ts2__Question_Set__c[] ApplicationQuestions;
   ApplicationQuestions = [SELECT Id, Name FROM Question_Set__c WHERE Name = 'Application Questions'];
  if(ApplicationQuestions.size() > 0)
    questions.Question_Set__c = ApplicationQuestions[0].id;
   // sets the default value


I need to set the default based on the record type... should I map the two and then insert the value based on the map? So name = x, record type y, insert = xyname if exists in lookup z?

1 Answer 1


The first thing you should look at here is bulkifying your trigger so that it doesn't need to perform a SOQL call for each Job__c in Trigger.new. See Best Practice: Bulkify Your Code

Part of this will be minimising the number of SOQL calls you make to get the Question Set Id for each job.

I'm assuming that you want to lookup a Question_Set__c by Name based on the Record Type of the Job__c record?

One thing that might make this much easier is to add the Job RecordTypeId as field on the Question_Set__c. Then you wouldn't need to do the lookup via the Question Set Name. Below I've assumed that such a field exists as JobRecordTypeId__c. If you don't want to go down that path you will need to add a mapping step to translate between the Job Record Type and the Question Set Name.

Example bulkified trigger:

trigger UpdateQuestionSet on Job__c (before insert, before update) {
    Set<Id> jobRecordTypeIds = new Set<Id>();

    for (Job__c job: Trigger.new) {

    if(jobRecordTypeIds.size() > 0) {

        Question_Set__c[] applicationQuestions =   
            [SELECT Id, JobRecordTypeId__c FROM Question_Set__c 
             WHERE JobRecordTypeId__c in :jobRecordTypeIds];
        // Build up a map here to easily go from the job Record Type to the Question Set Id
        Map<Id, Id> mapFromRecordTypeIdToQuestionSetId = new Map<Id, Id>();
        for (Question_Set__c questionSet : applicationQuestions) {
            mapFromRecordTypeIdToQuestionSetId.put(questionSet.JobRecordTypeId__c, questionSet.Id);

        for (Job__c job: Trigger.new) {
            // TODO: Should check that the map actually contains the Job Id key first.
            Id questionSetId = mapFromRecordTypeIdToQuestionSetId.get(job.Id);
            job.Question_Set__c = questionSetId;
  • Great, thanks for the elaborate answer. Helps a lot.
    – gato negro
    Commented Jun 12, 2013 at 12:56
  • I'm adapted it a touch to fit my labels + objects, created the field, but no trigger is being fired. This only first if: The record type on the position = the record type on the question set?
    – gato negro
    Commented Jun 13, 2013 at 21:16
  • @gatonegro The trigger isn't firing or the trigger is firing but not going into the first if statement? If the latter, that would suggest that the set of Job Record Type Ids is empty. Commented Jun 13, 2013 at 21:20
  • The trigger is firing, but when the position is created, the question set is not being associated. The trigger should update the job record type id field on the question set based on what record type is applied to the job correct?
    – gato negro
    Commented Jun 14, 2013 at 13:18
  • Maybe try testing the contents of the trigger as anonymous apex. You could populate the jobRecordTypeIds set with the RecordTypeId from the Job and then examine the debug log to see why the trigger isn't working. Commented Jun 14, 2013 at 20:00

You must log in to answer this question.

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