• Bob Bacon
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 8
    Replies
Martijn Schwarzer kindly wrote a trigger for me that was an improvement over the one I had but I can't get it to save and Martijn must be on holiday...

Here is the trigger.  Can anyone figure out why I get this error?  "Compile Error: unexpected token: '{' at line 17 column 40"

trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.left(3) == '00Q'){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}

Thanks in advance.

Bob Bacon
I have a relatively simple working trigger but I can't seem to get a test class to successfully run.  I have spent a few hours researching for a solution but I'm a novice and still can't figure it out.  Can anyone please help?  TIA

The trigger is:
trigger UpdateAccountType on Task (before insert , before update) { 
    Set<Id> leadId = new Set<Id>(); 
        for(Task t : Trigger.new){ 
            if(t.WhoId != null){ 
                String s1 = t.WhoId; 
            if(s1.left(3) == '00Q'){ 
                leadId.add(t.whoId); 
            } 
        } 
    } 
Lead lead = [Select Id, Type__c From Lead Where Id In : leadId limit 1]; 
    for(Task tk: Trigger.New){ 
    tk.Account_Type__c = lead.Type__c; 
    } 
 }

The test class I'm trying to run is:
@IsTest
Public Class TestAcountType {
static testmethod void insertTask() {
    Task t = new Task();
        t.WhoId = '00QJ000000B1nPQ';
        t.Type = 'Other';
    insert t
    }
}
I'm just learning triggers and have a relatively simple goal.  When a new task is created I need to copy the contents of a custom field on the Lead object and put it in a custom field on the Task object.

I've been reading here for about 4 hours and the triggers proposed are significantly more complex.  However, based on my reading here is a trigger that I assembled.  Note that lead.type__c is the source field and task.account_type__c is the destination field.

trigger UpdateAccountType on Task (after update) {
    Set<Id> leadIds=new Set<Id>();
    for(Task t : Trigger.new){
    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){   //check if the task is associated with a lead
        if( !leadIds.contains(t.whoid)){
            leadIds.add(t.whoId);
            t.Account_Type__C = lead.Type__c;
            }}}}

Can anyone help with this or, have I taken a wrong turn and need to start from stratch?

TIA
Martijn Schwarzer kindly wrote a trigger for me that was an improvement over the one I had but I can't get it to save and Martijn must be on holiday...

Here is the trigger.  Can anyone figure out why I get this error?  "Compile Error: unexpected token: '{' at line 17 column 40"

trigger UpdateAccountType on Task (before insert , before update) {
    Set<Id> leadId = new Set<Id>();
    for(Task t : Trigger.new){
        if(t.WhoId != null && t.WhoId.left(3) == '00Q'){
            leadId.add(t.whoId);
        }
    }

    //Query all leads from trigger.new (I removed the Limit 1 --> this will only retrieve the first lead)
    List<Lead> leads = [Select Id, Type__c From Lead Where Id In : leadId];

    //Create map for easy retrieval of records
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);

    for(Task tk: Trigger.New){
        //Check if lead is available in Map
        if(leadMap.containsKey(tk.WhoId){
            Lead lead = leadMap.get(tk.WhoId);  //Get lead from Map
            tk.Account_Type__c = lead.Type__c;
        }
    }
}

Thanks in advance.

Bob Bacon
I have a relatively simple working trigger but I can't seem to get a test class to successfully run.  I have spent a few hours researching for a solution but I'm a novice and still can't figure it out.  Can anyone please help?  TIA

The trigger is:
trigger UpdateAccountType on Task (before insert , before update) { 
    Set<Id> leadId = new Set<Id>(); 
        for(Task t : Trigger.new){ 
            if(t.WhoId != null){ 
                String s1 = t.WhoId; 
            if(s1.left(3) == '00Q'){ 
                leadId.add(t.whoId); 
            } 
        } 
    } 
Lead lead = [Select Id, Type__c From Lead Where Id In : leadId limit 1]; 
    for(Task tk: Trigger.New){ 
    tk.Account_Type__c = lead.Type__c; 
    } 
 }

The test class I'm trying to run is:
@IsTest
Public Class TestAcountType {
static testmethod void insertTask() {
    Task t = new Task();
        t.WhoId = '00QJ000000B1nPQ';
        t.Type = 'Other';
    insert t
    }
}
I'm just learning triggers and have a relatively simple goal.  When a new task is created I need to copy the contents of a custom field on the Lead object and put it in a custom field on the Task object.

I've been reading here for about 4 hours and the triggers proposed are significantly more complex.  However, based on my reading here is a trigger that I assembled.  Note that lead.type__c is the source field and task.account_type__c is the destination field.

trigger UpdateAccountType on Task (after update) {
    Set<Id> leadIds=new Set<Id>();
    for(Task t : Trigger.new){
    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){   //check if the task is associated with a lead
        if( !leadIds.contains(t.whoid)){
            leadIds.add(t.whoId);
            t.Account_Type__C = lead.Type__c;
            }}}}

Can anyone help with this or, have I taken a wrong turn and need to start from stratch?

TIA