function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iperez_geniusiperez_genius 

trigger help

Code:
trigger updateLead on Task (after insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
System.debug('1');
System.debug(t.Subject+'2');// value is mailMerge
System.debug(t.What.Type+'3'); // value is null

if (t.What.Type=='Lead' && t.Subject=='mailMerge')
{
System.debug('4');
Lead l = new Lead(Id = t.WhatId);
if (l.SentInfoPack__c == 'no')
{
System.debug('5');
l.SentInfoPack__c = 'yes';
leadsToUpdate.add(l);
}
}
}
update leadsToUpdate;
}


So this trigger compiles and executes howerver there is a problem with obtaining values.

the red are the values i need and the subject value comes out fine, however the green value is null.

can someone please advise.

I have no idea how to fix my problem. I also need code not just advice. I am on a steep learning curve here, this is my first look at apex code :)

Any help and advice is greatly appreciated.

Ilan
 

micwamicwa
What is not a field of a task, try it with WhatId
Code:
System.debug(t.WhatId); // should not be null

 
I'm not sure whether type is a legal property...
werewolfwerewolf
Actually that should work -- What is in fact a valid reference from Lead, and Type is a legal property on it.  I have no idea why it doesn't work, you should file a case with Salesforce Support on it.

Quick n' dirty workaround: make a String variable equal to WhatId, then check if the first 3 characters are '00Q'.  All leads' IDs will start with 00Q.
BoxBox
I'm afraid to traverse relationships of the objects you need to make a SOQL call as the trigger object will only contain information from the object that the trigger was called from.

i.e.

for(Task[] arrTask : ([
                                    Select     t.Who.Name, t.Who.Id, t.WhoId, t.What.Name, t.What.Id, t.WhatId
                                    From     Task t
                                ]))
{
    for(Task sTask : arrTask)
    {
       // do your processing
    }
}

Hope that's what you were looking for.
BoxBox

Sorry, you will also need:

// this will retrieve all Tasks and the extended relationship information for all tasks within the trigger
for(Task[] arrTask : ([
                                    Select     t.Who.Name, t.Who.Id, t.WhoId, t.What.Name, t.What.Id, t.WhatId
                                    From     Task t
                                    Where    id in trigger.newMap.keySet()
                                ]))
{
    for(Task sTask : arrTask)
    {
       // do your processing
    }
}