+ Start a Discussion
RajeevlsHydRajeevlsHyd 

Exception Error on a class

I have a class which is used in opportunity insertion ... but when I am running it, it is giving exception error (highlighted)

-----------

 

public static void OppInsert(Opportunity[] newOpportunity)
{
String Record_Type = '0145000000000fdAAA'
TestWeek__c TestWeek;
try
{

TestWeek = [select Wk_Nmbr__c, Year__c from TestWeek__c where Start_Date__c <= TODAY and End_Date__c >= TODAY]; // This is returniong values 

}
catch(System.QueryException e)
{
trigger.new[0].addError(e);
return;
}
if(TestWeek !=null)
{
// loop thru the newly created opportunities
for (Opportunity opportunityLoop : newOpportunity)
{
if (opportunityLoop.RecordTypeId == Record_Type.substring(0, Record_Type.length()-3))
{

 

---------------

 

It is firing for other record types of opportunity and giving the exceprion on opportunity insertion as  : 

FIELD_CUSTOM_VALIDATION_EXCEPTION, List has no rows for assignment to SObject: []

 

Any suggestions here ?

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Do like this

 

public static void OppInsert(Opportunity[] newOpportunity)
{
String Record_Type = '0145000000000fdAAA'
List<TestWeek__c> TestWeek = new List<TestWeek__c>();
try
{

TestWeek = [select Wk_Nmbr__c, Year__c from TestWeek__c where Start_Date__c <= TODAY and End_Date__c >= TODAY]; // This is returniong values 

}
catch(System.QueryException e)
{
trigger.new[0].addError(e);
return;
}
if(TestWeek.get(0) !=null)
{
// loop thru the newly created opportunities
for (Opportunity opportunityLoop : newOpportunity)
{
if (opportunityLoop.RecordTypeId == Record_Type.substring(0, Record_Type.length()-3))
{

//Your code

 

#Note: Use TestWeek.get(0) whereever you used TestWeek.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

Satish_SFDCSatish_SFDC
Is there any other code which executes before this code.
I see that it says there are no rows to assign to an sObject[] array.
But you have not used an array variable.

Regards,
Satish Kumar
RajeevlsHydRajeevlsHyd

There is a trigger which calls this class method .. other than this trigger , it is not used .

 

trigger History_Trigger on Opportunity (before insert, before update)
{
if (trigger.isInsert)
{
History_Class.OppInsert(trigger.new);
}

 

------------------------------------

 

TestWeek__c is custom object which holds some data , related  to opportunity, when I am running tests , this trigger is stopping and is referring the class to add exception

souvik9086souvik9086

Do like this

 

public static void OppInsert(Opportunity[] newOpportunity)
{
String Record_Type = '0145000000000fdAAA'
List<TestWeek__c> TestWeek = new List<TestWeek__c>();
try
{

TestWeek = [select Wk_Nmbr__c, Year__c from TestWeek__c where Start_Date__c <= TODAY and End_Date__c >= TODAY]; // This is returniong values 

}
catch(System.QueryException e)
{
trigger.new[0].addError(e);
return;
}
if(TestWeek.get(0) !=null)
{
// loop thru the newly created opportunities
for (Opportunity opportunityLoop : newOpportunity)
{
if (opportunityLoop.RecordTypeId == Record_Type.substring(0, Record_Type.length()-3))
{

//Your code

 

#Note: Use TestWeek.get(0) whereever you used TestWeek.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
Satish_SFDCSatish_SFDC

Try changing the TestWeek__c to an array.

.....................
String Record_Type = '0145000000000fdAAA'
TestWeek__c[] TestWeek;
...........................

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Dhaval PanchalDhaval Panchal
Use
List<TestWeek__c> TestWeek;
instead of
TestWeek__c TestWeek;
RajeevlsHydRajeevlsHyd

Thanks dapanchal and souvik