Method never returns a value in AX 2012

hi Experts,

I have Created a method In DPclass (I have to show Category ) but it gives following error

“Method never returns a value”

private str Category(ItemId _itemId)
{
    ECORESCATEGORY eCORESCATEGORY ;
    INVENTTABLE iNVENTTABLE ;
    EcoResProductCategory  ecoResProductCategory; 
    ;
    
    while select * from ECORESCATEGORY
 
 exists join  iNVENTTABLE
 exists join  ecoResProductCategory
    
    where  EcoResCategory.RecId == EcoResProductCategory.Category
        &&    INVENTTABLE.PRODUCT  == EcoResProductCategory.PRODUCT 
        &&  iNVENTTABLE.ItemId == _itemId
    
        //info(strFmt("%1's Category ",ECORESCATEGORY.NAME));
    return strFmt (ECORESCATEGORY.NAME);
}

Please guide me

You don’t understand yourf own code because you made it difficult to read. Let me rewrite it for you:

private str category(ItemId _itemId)
{
    EcoResCategory ecoResCategory;
    InventTable inventTable;
    EcoResProductCategory ecoResProductCategory; 
    
    while select ecoResCategory
		exists join inventTable
			where inventTable.ItemId == _itemId
		exists join ecoResProductCategory
			where ecoResProductCategory == ecoResCategory.RecId
			   && ecoResProductCategory.Product == inventTable.Product
	{
		return ecoResCategory.Name;
	}
}

Now you can easily see that there is no return statement for the situation when the query doens’t fetch any record. You should either return a value or throw an exception.

By the way, there is no point using a while loop if you end the method when you find the first record. Therefore you could do this:

private str category(ItemId _itemId)
{
    EcoResCategory			ecoResCategory;
    InventTable 			inventTable;
    EcoResProductCategory 	ecoResProductCategory;    
    
	select firstOnly Name from ecoResCategory
		exists join inventTable
			where inventTable.ItemId == _itemId
		exists join ecoResProductCategory
			where ecoResProductCategory == ecoResCategory.RecId
			   && ecoResProductCategory.Product == inventTable.Product;

	return ecoResCategory.Name;	
}