• Justin George
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hello, I have written a trigger that autmatically creates a Task record when a custom object record is created. The Owner of the Task is located through a string of related lookups. Currently, the trigger works, but it is not bulkified. I have attempted to bulkify the trigger, but I got lost is the logic and I could not find a similar piece of sample code to reference. Can someone point me in the right direction or give me some guidance.
 
trigger AutoCreateTask on Investigation__c (after insert) {

  List<Task> tasks = new List<Task>();

    for (Investigation__c newInv: Trigger.New) {

      Investigation__c investigation = [Select Id, RecordType.Name, Driver__c, Employee__c from Investigation__c where Id =: newInv.Id];


                
        if (investigation.RecordType.Name == 'Auto Investigation') {   
          Contact driver = [select Supervisor__c FROM Contact where Id =: investigation.Driver__c];
          Tasks.add(new Task( OwnerId = driver.Supervisor__c,
                              WhatId  = newInv.Id,
                              Subject = 'Please complete driver investigaiton',
                              ActivityDate = System.today().addDays(14),
                              Type = 'Investigation'
                              ));
        }

        if (investigation.RecordType.Name == 'Injury Investigations') {   
          Contact employee = [select Supervisor__c FROM Contact where Id =: investigation.Employee__c];
          Tasks.add(new Task( OwnerId = employee.Supervisor__c,
                              WhatId  = newInv.Id,
                              Subject = 'Please complete injury investigaiton',
                              ActivityDate = System.today().addDays(2),
                              Type = 'Investigation'
                              ));
        }    
    }    
  insert tasks;      
}

 
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
- OwnerId = Investigation__r.Claim__r.Claimant_Lookup__r.Supervisor_Lookup__c
- 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'
 
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);
      }


      }
    }


 
I have written a trigger that updates the owner field to a queue on a custom object. The queue is selected based on fields on a related object. The logic is as follows:

- The trigger must traverse through a lookup and return the value of a field on the lookup object: 
Claim__c.Location_Lookup__r.Node__c = '1234'
- Trigger must also reference a field on the object:
Claim__c.Coverage__c = 'AB'
- Trigger should concatenate '1234AB' and return a queue with the same name (1234AB).
- The Claim__c owner field should then be updated to '1234AB'

Here is the working trigger:
trigger ReassignOwnerClaim on Claim__c (before insert, before update) {

    for(claim__c newClaim : Trigger.new){

    Hierarchy__c location = [Select Node__c from Hierarchy__c where ID =: newClaim.Location_Lookup__c LIMIT 1];
    Group[] queue = [Select Id, Name from Group where Name =: location.Node__c + newClaim.Coverage__c LIMIT 1];
   
        if (queue.size() > 0) {
            newClaim.OwnerId = queue[0].Id;
        }

    }
}

The issue is the trigger is not bulkified. I have attempted to bulkify the trigger, but I got lost is the logic and I could not find a similar piece of sample code to reference. Can someone point me in the right direction or give me some guidance.

Thanks
 
Hello, I have written a trigger that autmatically creates a Task record when a custom object record is created. The Owner of the Task is located through a string of related lookups. Currently, the trigger works, but it is not bulkified. I have attempted to bulkify the trigger, but I got lost is the logic and I could not find a similar piece of sample code to reference. Can someone point me in the right direction or give me some guidance.
 
trigger AutoCreateTask on Investigation__c (after insert) {

  List<Task> tasks = new List<Task>();

    for (Investigation__c newInv: Trigger.New) {

      Investigation__c investigation = [Select Id, RecordType.Name, Driver__c, Employee__c from Investigation__c where Id =: newInv.Id];


                
        if (investigation.RecordType.Name == 'Auto Investigation') {   
          Contact driver = [select Supervisor__c FROM Contact where Id =: investigation.Driver__c];
          Tasks.add(new Task( OwnerId = driver.Supervisor__c,
                              WhatId  = newInv.Id,
                              Subject = 'Please complete driver investigaiton',
                              ActivityDate = System.today().addDays(14),
                              Type = 'Investigation'
                              ));
        }

        if (investigation.RecordType.Name == 'Injury Investigations') {   
          Contact employee = [select Supervisor__c FROM Contact where Id =: investigation.Employee__c];
          Tasks.add(new Task( OwnerId = employee.Supervisor__c,
                              WhatId  = newInv.Id,
                              Subject = 'Please complete injury investigaiton',
                              ActivityDate = System.today().addDays(2),
                              Type = 'Investigation'
                              ));
        }    
    }    
  insert tasks;      
}

 
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
- OwnerId = Investigation__r.Claim__r.Claimant_Lookup__r.Supervisor_Lookup__c
- 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'
 
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);
      }


      }
    }


 
I have written a trigger that updates the owner field to a queue on a custom object. The queue is selected based on fields on a related object. The logic is as follows:

- The trigger must traverse through a lookup and return the value of a field on the lookup object: 
Claim__c.Location_Lookup__r.Node__c = '1234'
- Trigger must also reference a field on the object:
Claim__c.Coverage__c = 'AB'
- Trigger should concatenate '1234AB' and return a queue with the same name (1234AB).
- The Claim__c owner field should then be updated to '1234AB'

Here is the working trigger:
trigger ReassignOwnerClaim on Claim__c (before insert, before update) {

    for(claim__c newClaim : Trigger.new){

    Hierarchy__c location = [Select Node__c from Hierarchy__c where ID =: newClaim.Location_Lookup__c LIMIT 1];
    Group[] queue = [Select Id, Name from Group where Name =: location.Node__c + newClaim.Coverage__c LIMIT 1];
   
        if (queue.size() > 0) {
            newClaim.OwnerId = queue[0].Id;
        }

    }
}

The issue is the trigger is not bulkified. I have attempted to bulkify the trigger, but I got lost is the logic and I could not find a similar piece of sample code to reference. Can someone point me in the right direction or give me some guidance.

Thanks