+ Start a Discussion

I need an extra set of eyes, I am overlooking a return.

I am writing code to create an asset when a case is closed as well as a Date field is equal to Today().  At the moment I am getting , "Non-void method might not return a value or might have statement after a return statement". I have gone over the code and I thought I have the returns taken case of.  Anyone would like to look it over I would appreciate it.

public class CaseAsset {
	private final Case c;
	public CaseAsset(ApexPages.StandardController stdController) {
		this.c = (Case)stdController.getRecord();
	//For testability
	//public CaseAsset(Asset a) {
	//	this.a = a;
//capture the case id from the button
		public PageReference caseDetails(){
			String theID = ApexPages.currentPage().getParameters().get('id');
			if (theID == null){
				return null;
			return caseDetails(theID);
		public PageReference caseDetails(String theID){
		 //private void PageReference caseDetails(String theID){

//Build Oportunity Line Item List object with results from the matching records of the query
 			list<Case> cas = [Select id, Status, X30_Day_Warranty_Start_Date__c,AccountId, Related_Products__c, ContactId  from Case where id =:theId];
 			list<Product2> prod = [Select Name from Product2 where Id = :cas[0].Related_Products__c];
 			System.debug('<---Alerting Product2-----> '+prod);

			//list<Asset> ToInsert = new list<Asset>();
 				for(Integer I = 0; I < cas.size(); I++){
 					list<Asset> ToInsert = new list<Asset>();
 							if(cas[I].X30_Day_Warranty_Start_Date__c == date.today()){
 								//create the case object 
 								Asset asset = new Asset();
 								asset.AccountId = cas[i].AccountId;
 								asset.Product2Id = cas[i].Related_Products__c;
 								asset.Status = 'Active';
 								//asset.Name = cas[i].Related_Products__c;
 								asset.Name = prod[0].Name;
 								//asset.Effective_Date__c = System.today();
 								System.debug('<---Alerting on the asset creation----->'+ asset);


								//Insert the cas object into the ToInsert list.    
      							insert ToInsert;
    							PageReference pageRef = new PageReference('/' + theId);
								return pageRef;
 								return null;

// Inserts all the objects within the ToInsert list 
  //insert ToInsert;
  //update cas;
  	//PageReference pageRef = new PageReference('/' + theId);
	//return pageRef;


Shashikant SharmaShashikant Sharma

See this part of your code

for(Integer I = 0; I < cas.size(); I++){
 					list<Asset> ToInsert = new list<Asset>();



In your for loop 

there is a if condition




if it fails for all the items then your code will not return anything, Compilers are very sharp :)






It looks like this line to me:




If the condition evaluates to false, there isn't an else condition or a catch-all return at the end of the method.


Cross post!


At least we both gave the same answer :)