Skip to main content
added 78 characters in body
Source Link
sfdcJameson
  • 1.1k
  • 12
  • 23

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (before insert) {
       if(trigger.isInsert && trigger.isBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, AccountId FROM Contact WHERE AccountId != null]);

        for(Task t : newList){
            if(contactMap.get(t.WhoId) != null){
                 t.WhatId = contactMap.get(t.WhoId).AccountId;
            }
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (before insert) {
       if(trigger.isInsert && trigger.isBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, AccountId FROM Contact WHERE AccountId != null]);

        for(Task t : newList){
            t.WhatId = contactMap.get(t.WhoId).AccountId;
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (before insert) {
       if(trigger.isInsert && trigger.isBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, AccountId FROM Contact WHERE AccountId != null]);

        for(Task t : newList){
            if(contactMap.get(t.WhoId) != null){
                 t.WhatId = contactMap.get(t.WhoId).AccountId;
            }
        }
    }
}
deleted 55 characters in body
Source Link
sfdcJameson
  • 1.1k
  • 12
  • 23

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (afterbefore insert) {
       if(trigger.isInsert && trigger.isAfterisBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        for(TaskMap<Id,Contact> tcontactMap := new Map<Id,Contact>([SELECT Id, Who.AccountId FROM TaskContact WHERE Id IN :newList AND WhoIdAccountId != null AND Who.type = 'Contact']null]){;
            t.WhatId = t.who.AccountId;
         for(Task t : tList.add(tnewList);{
        }
      t.WhatId = if(tListcontactMap.sizeget() > 0t.WhoId){
            update tList;.AccountId;
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (after insert) {
       if(trigger.isInsert && trigger.isAfter){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        for(Task t : [SELECT Id, Who.AccountId FROM Task WHERE Id IN :newList AND WhoId != null AND Who.type = 'Contact']){
            t.WhatId = t.who.AccountId;
            tList.add(t);
        }
        if(tList.size() > 0){
            update tList;
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (before insert) {
       if(trigger.isInsert && trigger.isBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, AccountId FROM Contact WHERE AccountId != null]);

        for(Task t : newList){
            t.WhatId = contactMap.get(t.WhoId).AccountId;
        }
    }
}
added 247 characters in body
Source Link
sfdcJameson
  • 1.1k
  • 12
  • 23

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (after insert) {
       if(trigger.isInsert && trigger.isAfter){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        for(Task t : [SELECT Id, Who.AccountId FROM Task WHERE Id IN :newList AND WhoId != null AND Who.type = 'Contact']){
            t.WhatId = t.who.AccountId;
            tList.add(t);
        }
        if(tList.size() > 0){
            update tList;
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (after insert) {
       if(trigger.isInsert && trigger.isAfter){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        for(Task t : [SELECT Who.AccountId FROM Task WHERE Id IN :newList AND WhoId != null AND Who.type = 'Contact']){
            t.WhatId = t.who.AccountId;
        }
    }
}

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (after insert) {
       if(trigger.isInsert && trigger.isAfter){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        for(Task t : [SELECT Id, Who.AccountId FROM Task WHERE Id IN :newList AND WhoId != null AND Who.type = 'Contact']){
            t.WhatId = t.who.AccountId;
            tList.add(t);
        }
        if(tList.size() > 0){
            update tList;
        }
    }
}
added 247 characters in body
Source Link
sfdcJameson
  • 1.1k
  • 12
  • 23
Loading
Source Link
sfdcJameson
  • 1.1k
  • 12
  • 23
Loading