+ Start a Discussion
Newbie  2014Newbie 2014 

Errors to Delete records

Hello: I have a simplest test class to delete a single record, code below. I have checked the select statement via 'query editor' in developer console; it returns a valid id. I just can't figure out why my delete statement does not work!  My class passes the test alright, but when I go and check the line item under the parent opportunity, I can see the line item is there unaffected. Please advise! Thanks very much



@istest
public class del1
{
static testmethod void unittest() {
 
List<opportunitylineitem> ol = [select id from opportunitylineitem where id =: '00km0000001WVUSAA4'] ;
delete ol;

}
}
Anil SavaliyaAnil Savaliya
Hey,

Try this,You are not making seealldata = true ,So query can't find record.But this is not good way to write testclass,Insert some dummy record in test class and  than delete ,If you write test like below way making id hardcoded,Test class will be failed in another instance.

@istest(SeeAllData = True )
public class del1
{
static testmethod void unittest() {

List<opportunitylineitem> ol = [select id from opportunitylineitem where id =: '00km0000001WVUSAA4'] ;
delete ol;

}
}

If My solution work for you ,Please mark as Like and resolved.

Thanks,
Anil Savaliya
Newbie  2014Newbie 2014
Thanks very much for your reply, added seealldata=true, but it didn't work either. I am so clueless. Any other suggestion?

I agree with you wholeheartedly about not to hardcode. This is just a test class to test one trigger on the object so.. 
Anil SavaliyaAnil Savaliya
@istest
public class del1
{
static testmethod void unittest() {


opportunitylineitem OTLL = New opportunitylineitem ();
(Add All required Field for  opportunitylineitem like  OTLL.Name = 'Test Test '; etc etc.)
Insert OTLL;

Delete OTLL;
}
}
Newbie  2014Newbie 2014
Thanks very much Anil for all your feedbacks but looks like the problem lies elsewhere with some permission issue; cuz I tried insertion as you suggested, test class passed but no insertion happened. I tried deleting parent opportunity, test class passes but no deletion happened. It almost feel like my sandbox data set have some kind of read only locks on them so that test classes can only access them, but can't edit/insert/delete. If I directly pull the records from the CRM and delete/edit, it does work, but not allowing same from my apex classes! I recently started apex coding so don't know much details to post and others to help me I guess. 

Thanks for trying to help I'd say. Take care, Newbie