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
Patrick HanahanPatrick Hanahan 

Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 36 column 1

This is the error I'm getting on this code. The error is for the last for loop. I thought I knew what the error was saying, but the wording is kind if confusing, and everything I've tried hasn't fixed it. Any ideas?
 
global class ParseEmailSubjectForJobNumber implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
                                                         

Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

try {
// Creating Regex to base pattern on and pulling email subject into a string
String jobNum;
integer cancelled = 0;
Opportunity opportunity;
String regex = '\\b\\d{7}\\b';
String subjectSentence = email.subject;

// Splitting the email subject into a list of words and removing all "(" and ")" and "J"
// to make the regex simpler
List<String> subjectSplit = subjectSentence.split('\\s+');  
for (String input : subjectSplit){
    input = input.replaceAll('(', '');
    input = input.replaceAll(')', '');
    input = input.replaceAll('J', '');
}        

Pattern p = Pattern.compile(regex);

// checks for the word cancelled in the subject, and sets the variable "cancelled" to 1 if it finds it (the variable is for later use)
for (String input : subjectSplit){
    if (input == 'cancelled' || input == 'Cancelled')
        cancelled = 1;
}

// loops through all words in the subject to find the job number, then sets the opportunity with the corresponding
// job number to cancelled if the subject also contains the word cancelled
for (String input : subjectSplit){
    Matcher m = p.matcher(input);
       if (m.find()){
           jobNum = input;
           if ([select count() from Opportunity where Job__c = :input] != 0 && cancelled == 1){
                opportunity = [select Id from Opportunity where Job__c = :jobNum];
                opportunity.StageName = 'Cancelled';
           }
       }else{
           break;
       }
}
}catch(Exception E){

}  
}
}

 
Amit Singh 1Amit Singh 1
Hello,
You need to add a return Statement before closing the curly braces of your global method because it has a return type.
global class ParseEmailSubjectForJobNumber implements Messaging.InboundEmailHandler {
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
  Messaging.Inboundenvelope envelope) {


  Account account;
  Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

  try {
   // Creating Regex to base pattern on and pulling email subject into a string
   String jobNum;
   integer cancelled = 0;
   Opportunity opportunity;
   String regex = '\\b\\d{7}\\b';
   String subjectSentence = email.subject;

   // Splitting the email subject into a list of words and removing all "(" and ")" and "J"
   // to make the regex simpler
   List < String > subjectSplit = subjectSentence.split('\\s+');
   for (String input: subjectSplit) {
    input = input.replaceAll('(', '');
    input = input.replaceAll(')', '');
    input = input.replaceAll('J', '');
   }

   Pattern p = Pattern.compile(regex);

   // checks for the word cancelled in the subject, and sets the variable "cancelled" to 1 if it finds it (the variable is for later use)
   for (String input: subjectSplit) {
    if (input == 'cancelled' || input == 'Cancelled')
     cancelled = 1;
   }

   // loops through all words in the subject to find the job number, then sets the opportunity with the corresponding
   // job number to cancelled if the subject also contains the word cancelled
   for (String input: subjectSplit) {
    Matcher m = p.matcher(input);
    if (m.find()) {
     jobNum = input;
     if ([select count() from Opportunity where Job__c = : input] != 0 && cancelled == 1) {
      opportunity = [select Id from Opportunity where Job__c = : jobNum];
      opportunity.StageName = 'Cancelled';
     }
    } else {
     break;
    }
   }
  } catch (Exception E) {
	
  }
  return null;
 }
}

Thanks,
Amit Chaudhary 8Amit Chaudhary 8
you need to return the result.
like below code
global class ParseEmailSubjectForJobNumber implements Messaging.InboundEmailHandler {
	
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
															 Messaging.Inboundenvelope envelope) 
	{
															 

		Account account;
		Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

		try {
			// Creating Regex to base pattern on and pulling email subject into a string
			String jobNum;
			integer cancelled = 0;
			Opportunity opportunity;
			String regex = '\\b\\d{7}\\b';
			String subjectSentence = email.subject;

			// Splitting the email subject into a list of words and removing all "(" and ")" and "J"
			// to make the regex simpler
			List<String> subjectSplit = subjectSentence.split('\\s+');  
			for (String input : subjectSplit){
				input = input.replaceAll('(', '');
				input = input.replaceAll(')', '');
				input = input.replaceAll('J', '');
			}        

			Pattern p = Pattern.compile(regex);

			// checks for the word cancelled in the subject, and sets the variable "cancelled" to 1 if it finds it (the variable is for later use)
			for (String input : subjectSplit){
				if (input == 'cancelled' || input == 'Cancelled')
					cancelled = 1;
			}

			// loops through all words in the subject to find the job number, then sets the opportunity with the corresponding
			// job number to cancelled if the subject also contains the word cancelled
			for (String input : subjectSplit){
				Matcher m = p.matcher(input);
				   if (m.find()){
					   jobNum = input;
					   if ([select count() from Opportunity where Job__c = :input] != 0 && cancelled == 1){
							opportunity = [select Id from Opportunity where Job__c = :jobNum];
							opportunity.StageName = 'Cancelled';
					   }
				   }else{
					   break;
				   }
			}
		}catch(Exception E){
		}  
		result.success = true;
		return result;
	}
}


Let us know if this will help you