3

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?

3

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) {
        jobRecordTypeIds.add(job.RecordTypeId);
    }

    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;
        }
    }
}
|improve this answer|||||
  • Great, thanks for the elaborate answer. Helps a lot. – gato negro Jun 12 '13 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 Jun 13 '13 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. – Daniel Ballinger Jun 13 '13 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 Jun 14 '13 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. – Daniel Ballinger Jun 14 '13 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.