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

run test for triggertest class shows Query Exception
Hi Folks,
I wrote the trigger test class for trigger, and when I run tests for the trigger test class;
It shows the exception like,
System.QueryException:List has no rows for assignment to SObject,
Trigger is,
trigger TriggerAfterLeadConversion on Lead (after update)
{
for(Lead lead:Trigger.new)
{
if (Lead.IsConverted)
{
Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];
con.Birthdate = lead.Date_Of_Birth__c;
con.Email=lead.Email;
update con;
}
}
}
Trigger Test class is,
@isTest
private class TriggerAfterLeadConversionTest
{
static testMethod void myUnitTest()
{
Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11), Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy');
insert le;
Contact con = [SELECT Id FROM Contact WHERE Contact.Id = '00Q8000000SyVy2EAF'];
insert con;
con.Birthdate=le.Date_of_Birth__c;
con.Email=le.Email;
update con;
system.assertEquals('Email',le.Email);
}
}
(if i wrote the trigger test class like this, in place of red color in above code,
Contact con = new Contact(Id=le.ID),
it shows error like,
System.TypeException:Invalid id value for this SObject type: 008000000TCVXmEAp)
Please help me to resolve this issue...
Thanks in Advance,
Regards,
Srinivas Chittela
You'll want to create all of your required object in your test cases and not depend on any data in the database. Any inserts, updates or deletes are not committed to the database but are rolled back after your test completes.
You'll want to do something like:
// create the objectContact con = new Contact(fistname='my first', lastname='mylast',email='no@email.com');// insert the recordinsert con;
Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com
Better not do DMLs inside for-loop.
May be you could try inside the test class:
Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11), Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy', IsConverted=TRUE);
insert le;
Contact con = new Contact(LastName='reddy',Lead__c= le.Id);
insert con;
update le;
The idea is: create lead, create contact and associate with the Lead, update the Lead.
Hope it helps.
Hi Ivivanin,
Thanks for quick reply,
i am new to salesforce.com, as of your suggestions..
i did test trigger class like this,
@isTest
private class TriggerAfterLeadConversionTest
{
static testMethod void myUnitTest()
{
Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11), Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy', IsConverted=TRUE);
insert le;
Contact con = new Contact(lastName='reddy',ID=le.Id);
Account a=new Account(Id='123456',Name='srinuAccount');
insert con;
insert a;
con.Birthdate=le.Date_of_Birth__c;
con.Email=le.Email;
update con;
update le;
system.assertEquals('Email',con.Email);
}
}
when i run tests for this, it shows error like,
system.DmlException:Insert failed. first exception on row=0, first error: FIELD_INTIGRITY_EXCEPTION, converted Account empty for a converted lead:[convertedAccountI],
please help me to resolve this,
Thanks in Advance,
srinivas chittela
Hi Douglas,
Thanks for quick reply,
i am new to this salesforce.com, as of your suggestions,
i did test trigger class like this,
@isTest
private class TriggerAfterLeadConversionTest
{
static testMethod void myUnitTest()
{
Lead le=new Lead(Date_of_Birth__c=Date.newInstance(1984,10,11), Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy');
insert le;
Contact con = new Contact(firstName='srinivas',lastName='reddy',Email='svreddych@gmail.com');
insert con;
con.Birthdate=le.Date_of_Birth__c;
con.Email=le.Email;
update con;
system.assertEquals('Email',le.Email);
}
}
when i run test for this trigger it shows exception like this,
System.Exception, Assertion failed: Expected=Email, Actuval=svreddych@gmail.com.
please help me to resolve this...
thanks in advance,
thanks & regards,
srinivas chittela