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 

complete newbie --> needs a simple trigger

I know its completely slack on my behalf not to have searched through all the posts to find my solution but i am in a bind with time, and i think this will be quicker. Hopefully someone is kind enough to help me.

The situatioin:
A lead comes in, we complete a mail merge and against their name a completed task is created stating a mail merge has been inacted.
I need to create a trigger that updates a custom field (sentPack) in a Lead  from default value "no" to "yes"

i am not sure how to do this.

can anyone help me. Please this is urgent

thanks in advance

regards
Ilan Perez
werewolfwerewolf
When you say "we complete a mail merge" do you mean that somebody manually does that, something automatically does that, or it's something you want the system to do?

This sounds more like a job for a workflow field update or an Scontrol than for a trigger.
iperez_geniusiperez_genius
The mail merge is down manaully, from the tools

I will re-iterate the process.

Leads come in every week.
Each friday a mail merge is created to all new leads or all leads that have not been sent a welcome pack.
Once the mail merge is created a completed task is added onto every lead in the mail merge stating they (the lead) have been included in a mail merge.

What i need is:
based on the the completed task i need to set a custoim field called "Sent Info pack" from no to yes.


Thats hwat i need help with...

thanks in advance

ilan
iperez_geniusiperez_genius
Code:
//This code is supposed to on the creation of a task check if the subject is mail merge.
//If it is mail merge task has been created update the leads that have the SentInfoPack set from no to Yes

trigger taskCreate on Task (before insert) { for (Task t : Trigger.new) { if (t.subject != 'mail merge') { for (Lead l:lead) { if (l.SentInfoPack__c != 'no'){
l.SentInfoPack__c = 'yes'
}
}


}
}
}

I know this is not correct because it doesn't work, but its a start from my end.

can anyone help me?

ilan

werewolfwerewolf
Well that is a start.

What you need to do is as follows (some pseudocode here):

Code:
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new) {
 if (t.What.Type=='Lead' && <some other criteria about the task here>) {
  Lead l = new Lead(Id = t.WhatId);
  l.FieldToChange__c = 'My changes';
  leadsToUpdate.add(l);
 }
}

update leadsToUpdate;

 Basically, if the tasks pertain to leads, then update the fields on those leads.
iperez_geniusiperez_genius
Code:
trigger updateLead on Lead (before insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
if (t.What.Type=='Lead' && t.subject=='mail Merge')
{
Lead l = new Lead(Id = t.WhatId);
if (l.sentInfoPack__c == 'no')
{
l.sentInfoPack__c = 'yes';
leadsToUpdate.add(l);
}
}
}
update leadsToUpdate;

}


werewolf this is my attempt at the code.
the error i am getting is
Error: Compile Error: Loop variable must be of type SOBJECT:Lead at line 4 column 15

...besides the fact that i am not too sure wether this will actually do what i want.

I suppose i dont understand how salesforce loads the values of the leads into arrays...in anycase this for anothe day.

back to the trigger can you advise further
werewolfwerewolf
Ah.  You've put the trigger on the wrong thing.  Put your trigger on Task instead and it will probably work.

What it's complaining about is you're doing a for (Task t : Trigger.new), but you've set your trigger on Lead so it thinks Trigger.new is going to contain a bunch of Leads.  What you really want it to contain is a bunch of Tasks.
iperez_geniusiperez_genius
Code:
trigger updateLead on Task (after insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
if (t.What.Type=='Lead' && t.subject=='mail Merge')
{
Lead l = new Lead(Id = t.WhatId);
if (l.sentInfoPack__c == 'no')
{
l.sentInfoPack__c = 'yes';
leadsToUpdate.add(l);
}
}
}
update leadsToUpdate;

}

Hows this?

 
iperez_geniusiperez_genius
Code:
trigger updateLead on Task (after insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
if (t.What.Type=='Lead' && t.subject=='mail Merge')
{
Lead l = new Lead(Id = t.WhatId);
if (l.sentInfoPack__c == 'no')
{
l.sentInfoPack__c = 'yes';
leadsToUpdate.add(l);
}
}
}
update leadsToUpdate;
}

 the error i am getting is...
Error: Compile Error: Incorrect SObject type: Task should be Lead at line 1 column 1

I am not sure waht to do?

werewolfwerewolf
It's probably because you already defined a trigger called updateLead on Lead.  Try deleting all those triggers and starting from scratch.
iperez_geniusiperez_genius
werewolf thanks for your help...
my mistake seems to be that i tried to save the trigger under leads > triggers rather than task > triggers.
so it now compiles fine but it doesn't work :(

i have checked all the field names to make sure the are correct but the trigger still doesn't work
here is the code once again
Code:
trigger updateLead on Task (before insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Task t: Trigger.new)
{
if (t.What.Type=='Lead' && t.Subject=='mailMerge')
{
Lead l = new Lead(Id = t.WhatId);
if (l.SentinfoPack__c == 'no')
{
l.SentinfoPack__c = 'yes';
leadsToUpdate.add(l);
}
}
}
update leadsToUpdate;
}

 werewolf we are so close,



werewolfwerewolf
Are you sure your fields are matching?  your "mailMerge" and "no" and "yes" and so on?

Try turning on the debug log when you run this, that will help you figure out where it's exiting.
iperez_geniusiperez_genius
Here is the log...

*** Beginning updateLead on Task trigger event BeforeInsert for null

20080604011423.991:Trigger.updateLead: line 4, column 1: SelectLoop:LIST:SOBJECT:Task
20080604011423.991:Trigger.updateLead: line 16, column 1: Update: LIST:SOBJECT:Lead

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 100
Number of script statements: 3 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0
Static variables and sizes:
updateLead:leadsToUpdate:4


*** Ending updateLead on Task trigger event BeforeInsert for null

werewolfwerewolf
Well it looks like it actually did update the lead.
werewolfwerewolf
Maybe put some System.debug (or whatever it is) statements in there so that you'll get little traces in the debug log?  My best guess is that if you're not updating the lead, it's because your if conditions aren't matching.  I'd put my money on the
l.SentinfoPack__c == 'no'

That looks mighty sketchy to me. If that's a boolean field you should be comparing it to false (not the string 'false', I mean the boolean literal false).
werewolfwerewolf
And if that's the case you should be setting it to true too.
iperez_geniusiperez_genius
how do i print the values of the variables into the log?

werewolfwerewolf
Use System.debug() statements.
iperez_geniusiperez_genius
Just to let you know my process...
i have a lead,
i simply create a task on that lead with subject
"mail Merge"

results --> task gets created fine but no field update :(
iperez_geniusiperez_genius
just to let you know that

t.What.Type is null

that piece of code is not working...


i am not sure why though
werewolfwerewolf
Interesting -- try making the trigger after insert maybe?
iperez_geniusiperez_genius
*** Beginning updateLead on Task trigger event AfterInsert for 00T4000000cIQ0K

20080605003631.636:Trigger.updateLead: line 4, column 1: SelectLoop:LIST:SOBJECT:Task
20080605003631.636:Trigger.updateLead: line 6, column 1: 1
20080605003631.636:Trigger.updateLead: line 7, column 1: mailMerge2
20080605003631.636:Trigger.updateLead: line 8, column 1: null3
20080605003631.636:Trigger.updateLead: line 22, column 1: Update: LIST:SOBJECT:Lead

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 100
Number of script statements: 6 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0
Static variables and sizes:
updateLead:leadsToUpdate:4


*** Ending updateLead on Task trigger event AfterInsert for 00T4000000cIQ0K

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');
System.debug(t.What.Type+'3');

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;
}

No idea why this is not working...

is there any way of printing into the log the a field from each lead.

can i give you access to my account for you to check out that its setup correctly?

I really need to sort this out :(

Look forward to your reply

ilan


iperez_geniusiperez_genius
Code:
trigger updateLead on Task (after insert)
{
Lead[] leadsToUpdate;
Task[] taskEdit;



for (Task t: Trigger.new)
{
leadsToUpdate = [select id, SentInfoPack__c, email from lead];
taskEdit =[select whoid, subject from task];

for (Lead ltu : leadsToUpdate)
{
//System.debug(ltu);
Lead[] leadInserts;
for (Task tsk : taskEdit)
 if ((tsk.whoid == ltu.id) && (tsk.subject == 'mailMerge266'))
{
if (ltu.SentInfoPack__c == 'no')
{
System.debug('Insert'+ltu.id);
String strLead = ltu.id;
ltu.SentInfoPack__c = 'yes' ;
update ltu;
}

}

}



}


}

werewolf

I re-wrote the code and here is my solution...i am not sure how efficient it is, and i am awaiting on salesforce in activate mas mail merge so that i can do a final test but it seems to work when i create a task that has the subject "mailMerge266" and that the task has a whoid of type lead

Thanks for your help

Ilan
werewolfwerewolf
No no no, don't do that.  Doing those queries inside a for loop is a sure recipe for failure -- as soon you have more than 1000 leads or so in your system, this trigger will fail every time.

Go back to the trigger you had a few iterations ago, with just a quick modification it should work OK:

Code:
trigger updateLead on Task (after insert)
{
 List<Lead> leadsToUpdate = new List<Lead>();
 for (Task t: Trigger.new)
 {
  if (t.Subject=='mailMerge')
  {
   Lead l = [select Id, SentInfoPack__c from Lead where Id=:t.WhatId and SentInfoPack__c='no'];
   if (l!=null)
   {
    l.SentInfoPack__c = 'yes';
    leadsToUpdate.add(l);
   }
  }
 }

 update leadsToUpdate;
}

 
This is still not very nice because I'm doing a query in the for loop, for which I am currently slapping myself on the wrist, but if that t.What.Type isn't working then I see no alternative.  Anyway since the update batch size maxes out at 200 then you'll be doing at most 200 queries here, which is terrible but won't hit the limit.
iperez_geniusiperez_genius
I have returned to our original code...
trying to work through the issues,

like the what.type issue