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 

another test coverage question

firstly let me say there needs to be a tutorial section to explain how to do all the things necessary to create a trigger and deploy it with all term explained....

secondly i have created my trigger, which is tested and works 100% in my developer license.

I am trying to bring it into my production environment.

I have downloaded eclipse installed the plugin, and saved my trigger to saleforce, production...
the trigger is inactive though. From what i have read inorder to activate the trigger i need to get my test coverage greater than 75%.

apparently to do this i need to create a test class.

Briefly my trigger, on insertion of a task with subject mail_merged a custom field in a lead is set from no to yes.

Below is my attempt at a test class

Code:
public class updateLead {
 static testMethod void testUpdateLead() {
  Task t1 = new Task (subject='Mail merge document(s) generated: mail_Merged');
  Task t2 = new Task (subject='Email');
  Task[] myTasks = new Task[]{t1,t2};
  insert myTasks;
  Task queryTask1 = [select Id,subject from Task where Id = :t1.Id];
  system.assertEquals('Mail merge document(s) generated: mail_Merged',queryTask1.subject);
  Task queryTask2 = [select Id,subject from Task where Id = :t2.Id];
  system.assertEquals('Email',queryTask2.subject);

 }
}

 here is my trigger

Code:
trigger updateLead on Task (after insert) {
Lead[] leadsToUpdate;for (Task t: Trigger.new)
{
String accountid = t.whoId;
leadsToUpdate = [select id, SentPack__c, email from lead where id =: accountid];

String Otype = accountid.substring(0, 3);

 if (Otype=='00Q' && t.Subject=='Mail merge document(s) generated: mail_Merged')
 {
 
  for (Lead ltu : leadsToUpdate)
  {
   if (ltu.SentPack__c == 'no')
{

ltu.SentPack__c = 'yes' ;
update ltu;
}  


  }


 }
}

}

 what must i do next to get test coverage great than 75%

Please help...

thanks in advance

Ilan   


iperez_geniusiperez_genius
Since my post...

I have created my sandbox and tested my trigger...and it works fine

I have uploaed the test class that is written above and it has a test coverage of 100%

I am not sure what to do now...

How do i activate the trigger in the production interface?

Ilan
iperez_geniusiperez_genius
let me re-phrase...

when i run the class in the sandbox i get a test coverage of 50%...

is someone able to help me with my test class

Ilan
iperez_geniusiperez_genius
adn so i continue to answer my own questioins :)
here is my new tst unit class

Code:
public class updateLead {
 static testMethod void testUpdateLead() {

Lead ld = new Lead ( FirstName = '~Test Lead', LastName='test', SentPack__c='no', email='asd@aasd.com', street='asd', state='asd', postalcode='2345', city='australia', phone='234234');
 insert ld;
 
system.debug('>>>>'+ld);
 Lead queryLead1 = [select Id,SentPack__c from Lead where Id = :ld.Id];
 
system.debug('>>>>Lead'+queryLead1);
 system.assertEquals('no',queryLead1.SentPack__c);
  Task tsk = new Task (subject='Mail merge document(s) generated: mail_Merged');
  insert tsk;
Task queryTask1 = [select Id,subject, whoid from Task where whoId = :ld.Id];
system.debug('<<>><<>>'+queryTask1);  
system.assertEquals('Mail merge document(s) generated: mail_Merged',queryTask1.subject);
system.assertEquals('yes',queryLead1.SentPack__c);
  
 }
}

I have debug statement throughout the class and the trigger and i know that the class enters the trigger... the task gets created but at line 7
the trigger ends with
20080616063540.511:Trigger.updateLead: line 7, column 1: !!!!Task:{SystemModstamp=2008-06-16 06:35:40,.....}
System.NullPointerException: Attempt to de-reference a null object
And for the life of me i am not sure why this is the case.

can someone help?
Ilan

jrotensteinjrotenstein
Which line is Line 7, that is causing the exception?

Also, please note that your code is not bulk-safe. You are retrieving leadsToUpdate once for every incoming record, and possibly calling Update for each Lead record too. An alternative strategy would be to try retrieving all leads in one call and updating all records in one call (for examples, see the Force.com Cookbook or other Forum discussions on Triggers).
iperez_geniusiperez_genius
the excpetion is occuring in the trigger its ths line
String Otype = accountid.substring(0, 3);

it looks like...

all i can understnad is that the task in not being created...or can't be found so when it tries to get a substring of the accountid
the result is null...and the rest of the trigger falls over....



but that shouldn't give a problem because the trigger only updates when a trigger has both the correct subject and the task belongs to a

lead...

anyway can maybe someone can update the code for me...

this is very urgent that i solve this...

Ilan

P.s. in regards to the bulk issue...i have looked and looked and looked and i can't find how to load all the leads into a list

i had this as my code and it kept on giving problems

List<Lead> leadsToUpdate = new List<Lead>();

but i couldn't get it to work...

anyways...more imporntantly is the test class not working...
for the moment.




iperez_geniusiperez_genius
you will be happy to know i have solved 99% of my problems...yay!!!

my test class works fine...my trigger works fine...however....

the new and latest issue in the trigger saga...which has been going on for 3 weeks is

so the trigger activates and correctly modifies the data but when the last command of "update the lead" the data is not saved...

and therefore after the trigger has finished and we return to the test class and assert that a specific value has changed the response is negative.

I am not sure why this is...

my new and improved code is below

The Class
Code:
public class updateLead {
static testMethod void testUpdateLead() {

Lead ld = new Lead ( FirstName = '~Test Lead', LastName='test', SentPack__c='no', email='asd@aasd.com', street='asd', state='asd', postalcode='2345', city='australia', phone='234234');
insert ld;

system.debug('>>>>'+ld);
Lead queryLead1 = [select Id,SentPack__c from Lead where Id = :ld.Id];
String who = ld.id;
system.debug('>>>>Lead'+queryLead1);
system.assertEquals('no',queryLead1.SentPack__c);
Task tsk = new Task (subject='Mail merge document(s) generated: mail_Merged', whoID = who);
insert tsk;
system.debug('####'+tsk);

Task queryTask1 = [select Id,subject, whoid from Task where whoId = :ld.Id];
system.debug('<<>><<>>'+queryTask1);
system.assertEquals('Mail merge document(s) generated: mail_Merged',queryTask1.subject);
system.assertEquals('yes',queryLead1.SentPack__c);//result is no

}
}

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

String Otype;
for (Task t: Trigger.new)
{
String accountid = t.whoId;
leadsToUpdate = [select id, SentPack__c, email from lead where id =: accountid];
system.debug('!!!!'+t);

Otype = accountid.substring(0, 3);

if (Otype=='00Q' && t.Subject=='Mail merge document(s) generated: mail_Merged')
{

for (Lead ltu : leadsToUpdate)
{

system.debug('***'+ltu);
if (ltu.SentPack__c == 'no')
{
system.debug('**1'+ltu.SentPack__c);// result is no
ltu.SentPack__c = 'yes' ;
system.debug('**2'+ltu.SentPack__c);//result is yes
update ltu;
}


}


}
}

}

 





jrotensteinjrotenstein
If the value of the Lead/Task has changed as a result of your trigger, retrieve it again (via another SELECT) and THEN test the values.
iperez_geniusiperez_genius
coincidentally i worked this out eventually by my self...as you can tell i am really proud of myself...

i have a coverage of 100% and a success stamp on my trigger...

i now am trying to work out how to activate my trigger in the production interface
jrotensteinjrotenstein
Within Eclipse, open the Meta file and change Active 'false' to 'true'. Then just migrate to Production.
iperez_geniusiperez_genius
in my sandbox i get a coverage of 100%
i have updated my files in eclipse,
and i get a coverage of 54%,
not only that the trigger compiles fine, that is no errors, but the class has a ! mark with an error stating file is not saved on server.

so i really dont know how to get this working...


can someone help me please?


Ilan


jrotensteinjrotenstein
If it is not saved on the server, then there is probably a syntax error in your file. Try to Save, then look for errors, before running the Tests.
iperez_geniusiperez_genius
yes...i have just noticed that the class is pretty much not there.

I am trying to sync it with the server but all i can do is sync it with the sandbox...
i run the test in the sandbox and i get hte coverage of 100%

i have no idea how to get the class saved onto the porduction ui


I am about to break something :)

this should be an easy exercise, instead its bladdy impossible grrr....

No idea how to sync it...i have of course tried deploy, save to server, sync to server...nothing seems to change the class on the production server

please help

ilan