You need to sign in to do that
Don't have an account?

Help with test code for after insert on Lead object
Hi ,
I created a trigger on Lead object and some custom lookup fields to update after insert.
It works fine. I added test code and the code coverage is not covering all code.
Really appreciate of some one can help with what I missed. Here is my trigger and class.
FYI, my test code also includes convert lead whcih is extra.
***Trigger***
trigger UpdateLeadDistifields on Lead (after insert) {
Set<ID> LeadId = new Set<Id>();
for (Lead cLead : Trigger.New){
Lead l = [select Id,OwnerId,PartnerAccountId,RecordTypeId from Lead where Id = :cLead.Id];
RecordType rt = [select Name FROM RecordType where Id = :l.RecordTypeId];
if (rt.name == 'DealRegistration'){
User u = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId != '' and ContactId != '' and IsPortalEnabled = true];
Account a = [select Id, RecordTypeId from Account where Id = :u.AccountId ];
RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
l.PortalUser__c = u.Id;
l.PortalAccount__c = u.AccountId;
if (accrt.Name == 'Distributor'){
l.DistiAccount__c = l.PartnerAccountId ;
}
}else {
l.PortalUser__c = l.OwnerId;
l.PortalAccount__c = l.PartnerAccountId;
}
update l;
}
}
********test**********
@IsTest
private class TestXXCSRLeadConvertClass{
static testMethod void TestXXCSRLeadConvertClass() {
test.startTest();
Boolean bolCreateOpp;
String strAccountId;
String strContactId;
User usr = [select Id,PartnerApprover__c,AccountId from User where Name='Tim House' and IsPortalEnabled = true];
RecordType rt = [select Id,Name FROM RecordType where Name='Deal Registration'];
Lead l = new Lead();
l.FirstName = 'CRM Testing First';
l.LastName = 'CRM Testing Last';
l.Company = 'CRM Testing INCtest';
l.description = 'Test descr';
l.city = 'test';
l.street = 'test';
l.state = 'CA';
l.country = 'United States';
l.status = 'Qualified';
l.email = 'test@testnetgear.com';
l.website = 'www.testcrm.com';
l.ApprovalStatus__c='Approved';
l.RecordTypeId= rt.Id;
l.OwnerId = usr.Id; //Timhouse
insert l;
//update
l = [select Id,OwnerId,PartnerAccountId,RecordTypeId,DistiAccount__c,PortalUser__c ,PortalAccount__c from Lead where Id = :l.Id];
// system.debug("Owner"+l.OwnerId);
// system.debug("AccountId"+usr.AccountId);
if (rt.Name == 'DealRegistration'){
User usr1 = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId != '' and ContactId != '' and IsPortalEnabled = true];
Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId ];
RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
// user usr1 = [select Id,AccountId from User where Id = :l.OwnerId];
// Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId and IsPartner = true];
// RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
system.assertEquals(l.OwnerId,usr1.Id );
system.assertEquals(l.PartnerAccountId,usr1.AccountId);
if (accrt.Name == 'Distributor'){
system.assertEquals(l.PartnerAccountId ,usr1.AccountId);
}
}
Id leadId = l.Id;
bolCreateOpp = false;
//Create a reference to the VF page
PageReference pageRef = Page.XXCSR_LEAD_CONVERT;
Test.setCurrentPageReference(pageRef);
//Create an instance of the controller extension and call the autoRun method.
//autoRun must be called explicitly even though it is "autoRun".
ApexPages.StandardController sc = new ApexPages.standardController(l);
XXCSRLeadConvertClass leadconvt = new XXCSRLeadConvertClass(sc);
leadconvt.autoRun();
//String nextPage = sc.save().getUrl();
List<SelectOption> testacct = new List<SelectOption>();
testacct = leadconvt.getlstCompanyInfo();
system.debug(testacct);
List<SelectOption> testcon = new List<SelectOption>();
testcon = leadconvt.getlstContactInfo();
system.debug(testcon);
leadconvt.doNothing();
leadconvt.strAccountId= '1';
leadconvt.strcontactId= '1';
leadconvt.convertlead();
//Retrieve the converted Lead info and the opps and roles.
l = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId ,Status, ApprovalStatus__c,
Company,OwnerId,firstname,lastname,city,country from Lead where Id = :leadId];
// system.assert(!l.IsConverted,'Lead Converted' );
test.stopTest();
}
}
Thank you for your help,
Pallavi
I created a trigger on Lead object and some custom lookup fields to update after insert.
It works fine. I added test code and the code coverage is not covering all code.
Really appreciate of some one can help with what I missed. Here is my trigger and class.
FYI, my test code also includes convert lead whcih is extra.
***Trigger***
trigger UpdateLeadDistifields on Lead (after insert) {
Set<ID> LeadId = new Set<Id>();
for (Lead cLead : Trigger.New){
Lead l = [select Id,OwnerId,PartnerAccountId,RecordTypeId from Lead where Id = :cLead.Id];
RecordType rt = [select Name FROM RecordType where Id = :l.RecordTypeId];
if (rt.name == 'DealRegistration'){
User u = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId != '' and ContactId != '' and IsPortalEnabled = true];
Account a = [select Id, RecordTypeId from Account where Id = :u.AccountId ];
RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
l.PortalUser__c = u.Id;
l.PortalAccount__c = u.AccountId;
if (accrt.Name == 'Distributor'){
l.DistiAccount__c = l.PartnerAccountId ;
}
}else {
l.PortalUser__c = l.OwnerId;
l.PortalAccount__c = l.PartnerAccountId;
}
update l;
}
}
********test**********
@IsTest
private class TestXXCSRLeadConvertClass{
static testMethod void TestXXCSRLeadConvertClass() {
test.startTest();
Boolean bolCreateOpp;
String strAccountId;
String strContactId;
User usr = [select Id,PartnerApprover__c,AccountId from User where Name='Tim House' and IsPortalEnabled = true];
RecordType rt = [select Id,Name FROM RecordType where Name='Deal Registration'];
Lead l = new Lead();
l.FirstName = 'CRM Testing First';
l.LastName = 'CRM Testing Last';
l.Company = 'CRM Testing INCtest';
l.description = 'Test descr';
l.city = 'test';
l.street = 'test';
l.state = 'CA';
l.country = 'United States';
l.status = 'Qualified';
l.email = 'test@testnetgear.com';
l.website = 'www.testcrm.com';
l.ApprovalStatus__c='Approved';
l.RecordTypeId= rt.Id;
l.OwnerId = usr.Id; //Timhouse
insert l;
//update
l = [select Id,OwnerId,PartnerAccountId,RecordTypeId,DistiAccount__c,PortalUser__c ,PortalAccount__c from Lead where Id = :l.Id];
// system.debug("Owner"+l.OwnerId);
// system.debug("AccountId"+usr.AccountId);
if (rt.Name == 'DealRegistration'){
User usr1 = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId != '' and ContactId != '' and IsPortalEnabled = true];
Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId ];
RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
// user usr1 = [select Id,AccountId from User where Id = :l.OwnerId];
// Account a = [select Id, RecordTypeId from Account where Id = :usr1.AccountId and IsPartner = true];
// RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
system.assertEquals(l.OwnerId,usr1.Id );
system.assertEquals(l.PartnerAccountId,usr1.AccountId);
if (accrt.Name == 'Distributor'){
system.assertEquals(l.PartnerAccountId ,usr1.AccountId);
}
}
Id leadId = l.Id;
bolCreateOpp = false;
//Create a reference to the VF page
PageReference pageRef = Page.XXCSR_LEAD_CONVERT;
Test.setCurrentPageReference(pageRef);
//Create an instance of the controller extension and call the autoRun method.
//autoRun must be called explicitly even though it is "autoRun".
ApexPages.StandardController sc = new ApexPages.standardController(l);
XXCSRLeadConvertClass leadconvt = new XXCSRLeadConvertClass(sc);
leadconvt.autoRun();
//String nextPage = sc.save().getUrl();
List<SelectOption> testacct = new List<SelectOption>();
testacct = leadconvt.getlstCompanyInfo();
system.debug(testacct);
List<SelectOption> testcon = new List<SelectOption>();
testcon = leadconvt.getlstContactInfo();
system.debug(testcon);
leadconvt.doNothing();
leadconvt.strAccountId= '1';
leadconvt.strcontactId= '1';
leadconvt.convertlead();
//Retrieve the converted Lead info and the opps and roles.
l = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId ,Status, ApprovalStatus__c,
Company,OwnerId,firstname,lastname,city,country from Lead where Id = :leadId];
// system.assert(!l.IsConverted,'Lead Converted' );
test.stopTest();
}
}
Thank you for your help,
Pallavi
I dont have much experience with Apex. Do you want to look my test code and suggest please.
developer console shows that following lines of code from trigger is not covered.
User u = [select Id ,AccountId from User where Id = :l.OwnerId and AccountId != '' and ContactId != '' and IsPortalEnabled = true];
Account a = [select Id, RecordTypeId from Account where Id = :u.AccountId ];
RecordType accrt = [select Name FROM RecordType where Id = :a.RecordTypeId];
l.PortalUser__c = u.Id;
l.PortalAccount__c = u.AccountId;
if (accrt.Name == 'Distributor'){
l.DistiAccount__c = l.PartnerAccountId
Thanks
Pallavi