function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cl0s3rcl0s3r 

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);
 			

//Build 
			//list<Asset> ToInsert = new list<Asset>();
 				for(Integer I = 0; I < cas.size(); I++){
 					list<Asset> ToInsert = new list<Asset>();
 						if(cas[I].Status.equals('Production-Ready')){
 							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.    
    							ToInsert.add(asset);
      							insert ToInsert;
      
    							PageReference pageRef = new PageReference('/' + theId);
								pageRef.setRedirect(true);
								return pageRef;
      
 							}else{
 								return null;
 							}
 			 			}

// Inserts all the objects within the ToInsert list 
  //insert ToInsert;
  //update cas;
  
  	//PageReference pageRef = new PageReference('/' + theId);
	//pageRef.setRedirect(true);
	//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>();
 						if(cas[I].Status.equals('Production-Ready'))

 

 

In your for loop 

there is a if condition

 

if(cas[I].Status.equals('Production-Ready'))

 

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

 

 

 

 

bob_buzzardbob_buzzard

It looks like this line to me:

 

if(cas[I].Status.equals('Production-Ready')){

 

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

bob_buzzardbob_buzzard

Cross post!

 

At least we both gave the same answer :)