You need to sign in to do that
Don't have an account?
Justin George
Trigger creates a task on insert - issue assigning owner
Hey all,
I have written a trigger that creates a related Task record when a new record on a custom object is inserted. The issue I am running into is assigning the OwnerID. The correct owner comes from a series of lookups to related records. First of all, I am having trouble envisioning the maps that are needed in order to bulkify the trigger. There are two paths that depend on record type.
Here's the logic:
Path 1
- New Investigation__c record is created (Investigation__c.RecordType == 'Injury Investigation')
- Insert Task related to Investigation__c
Path 2
- New Investigation__c record is created (Investigation__c.RecordType == 'Auto Investigation')
- Insert Task related to Investigation__c
Feable attempt to bulkify:
I have written a trigger that creates a related Task record when a new record on a custom object is inserted. The issue I am running into is assigning the OwnerID. The correct owner comes from a series of lookups to related records. First of all, I am having trouble envisioning the maps that are needed in order to bulkify the trigger. There are two paths that depend on record type.
Here's the logic:
Path 1
- New Investigation__c record is created (Investigation__c.RecordType == 'Injury Investigation')
- Insert Task related to Investigation__c
- OwnerId = Investigation__r.Claim__r.Claimant_Lookup__r.Supervisor_Lookup__c
- WhatId = Investigation__c.Id
- Subject = 'Test'
- WhatId = Investigation__c.Id
- Subject = 'Test'
Path 2
- New Investigation__c record is created (Investigation__c.RecordType == 'Auto Investigation')
- Insert Task related to Investigation__c
- OwnerId = Investigation__r.Claim__r.Driver__r.Supervisor_Lookup__c
- WhatId = Investigation__c.Id
- Subject = 'Test'
- WhatId = Investigation__c.Id
- Subject = 'Test'
trigger AutoCreateTask on Investigation__c (after insert) { List<Task> tasks = new List<Task>(); for (Investigation__c newInv: Trigger.New) { Claim__c claim = [select Driver__c, Claimant_Lookup__c FROM Claim__c where Id =: newInv.Claim__c]; Investigation__c investigation = [Select Id, RecordType.Name from Investigation__c where Id =: newInv.Id]; if (investigation.RecordType.Name == 'Auto Investigation') { Tasks.add(new Task( OwnerId = ???????, WhatId = newInv.Id, Subject = 'Please complete driver investigaiton', ActivityDate = System.today().addDays(14), Type = 'Investigation' )); } if (investigation.RecordType.Name == 'Injury Investigations') { Tasks.add(new Task( OwnerId = ??????, WhatId = newInv.Id, Subject = 'Please complete injury investigation', ActivityDate = System.today().addDays(2), Type = 'Investigation' )); } } insert tasks; }
Feable attempt to bulkify:
trigger AutoCreateTask on Investigation__c (after insert) { List<Task> tasks = new List<Task>(); List<Id> drivers = new List<Id>(); List<Id> supervisors = new List<Id>(); for (Investigation__c newInv: Trigger.New) { drivers.add(newInv.Driver__c); } if (!drivers.isEmpty()){ Map<Id, Contact> driverMap = new Map<Id, Contact>([ SELECT Name FROM Contact WHERE Id in: drivers ]) } for (Investigation__c newInv: Trigger.New) { if (!driverMap.containsKey(newInv.Driver__c)) { continue; } Contact driver = driverMap.get(newInv.Driver__c); Id supervisor = driver.Supervisor_Lookup__c; supervisors.add(supervisor); } if (!nameSet.isEmpty()) { Map<Id, User> superMap = new Map<Id, User>(); for (User user : [ select Id from User where Id in :supervisors ]) { superMap.put(user.Id, User); } } }
Justin George
I should probably give some more background. Driver__c and Claimant_Lookup__c are both lookups to Contact. Supervisor_Lookup__c is a lookup to User.