+ Start a Discussion
Ebi AmabebeEbi Amabebe 

Save Error: Missing Return Statement Required return type string

I am a beginner, I wrote this code to basically take two JSON POSTS and combine them I am getting a Save Error: Missing Return Statement Required return type string. My code is as follows, any help would be great! I have a return statement and it is a string so i'm not sure what I am missing. 
global with sharing class SendMCAwFiles {
  	global SendMCAwFiles (ApexPages.StandardController stdController)
  {
  }
  
  
 @RemoteAction
    global static String submittoMCAwDocs(List<Id>attachmentIds, String parentId, String mcaSubmissionID, String app_id,boolean allow_resubmit) {
    
    List<String> files = new List<String>();

    String documentUL_json;
    String response;
    MCA_Submission__c mcaSub = [SELECT Id,App_ID__c, Error_Message__c,FP_URL__c, Submission_Date_Time__c,FP_Username__c, FP_Password__c, Opportunity__c FROM MCA_Submission__c WHERE Id = :mcaSubmissionId];

    for (String attachmentId : attachmentIds) {
      documentUL_json = '';
      response = '';
      
      cg__OpportunityFile__c file = [SELECT Id, cg__File_Size__c, CreatedDate, MCA_Doc_Type__c, cg__Content_Type__c, cg__File_Name__c, MCA_File_Upload_Date__c FROM cg__OpportunityFile__c WHERE Id = :attachmentId];
      string fileURL = cg.SDriveTools.getAttachmentURL(parentId, attachmentId, 7200000);
      
      system.debug('UPLOAD FILE: ' + file.Id + ', URL: ' + fileURL);

        documentUL_json = MCAJSONUploadDocument.MCAJSONUploadDocument(
                mcaSub.FP_Username__c,
                mcaSub.FP_Password__c,
                mcaSub.Id,
                file.MCA_Doc_Type__c,
                file.cg__File_Name__c,
                fileURL
        );
     
    }
    
// now all documents that are attached are  stored withing the DocumentUL__Json string 

        if(mcaSub.App_ID__c != null && !allow_resubmit) {
        	
           system.debug('App has already been submitted.');
           
        } else {

            String jsonRequest=MCAJsonConstruct.MCAJsonConstruct(mcaSubmissionID);
            
            System.debug('MCA Json Request: ' + jsonRequest);
            
            Datetime currentDateTime = Datetime.now();
            String results;
            String DocwSub = documentUL_json + jsonRequest;
            
            try {
                Http httpProtocol = new Http();
		        HttpRequest request = new HttpRequest();
                request.setEndPoint(mcaSub.FP_URL__c+'submit_application with documents');
                request.setMethod('POST');
                request.setTimeout(120000);
                request.setHeader('Content-Type', 'application/json');
                //  set body to DowwSub which is the String for hte document attachment plus the Object.
                request.setBody(DocwSub);
                HttpResponse json_response = httpProtocol.send(request);
                String response2 = json_response.getBody();
                System.debug('MCA Json Response: ' + response);
               
				JSONParser parser =  JSON.createParser(response);
				
				string  IsError='';
				string  Message='';
				string  RowId='';

				//parser response  
                while (parser.nextToken() != JSONToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                        String text = parser.getText();
                        String fieldName = parser.getCurrentName();
                        if (parser.nextToken() != JSONToken.VALUE_NULL) {
                            if (text == 'IsError') {
                                IsError = parser.getText();
                            } else if (text =='Message') {
                                parser.nextValue();
                                Message = parser.getText();
                            } else if (text == 'RowId') {
                                RowId= parser.getText();
                            } else {
                                System.debug(response);
                            }
                        }
                    }
                }  // end while
               
                if(IsError == 'false') {
                    mcaSub.App_ID__c = RowId;
                    results = 'Send to MCA was: ' + Message;
                } else {
                    results = 'There was a problem: ' +  Message;
                    System.debug('PROBLEM: ' +  Message);
                }
                mcaSub.Submission_Date_Time__c = currentDateTime;
                mcaSub.Error_Message__c =  Message;

                update mcaSub;

            } catch(System.CalloutException e) {
                System.debug('FAILED: ' + e);
                results = 'ERROR: ' + e;
            }
            return results;
        }  // end if (mca.App_ID__c != null && !allow_resubmit)
    }  // end Pequalwdocs
       
  }
Best Answer chosen by Ebi Amabebe
Shiva RajendranShiva Rajendran
yes Ebi you are right.
Long story short , you must move your return results ; statement at the end of the function.
In this case , you must also declare the results variable somewhere at the start of the function not inside any code block

Let me know if it works fine

Thanks and Regards,
Shiva RV

All Answers

Shiva RajendranShiva Rajendran
Hi Ebi,
I think it is a simple code issue
Just make your return results; statment  at the end before the last " } "  . I mean by before closing the function write your return results so you must also initailize your result variable at the start of the function and not inside any block of code.
This error pops up because the function is suppose to return string but  in some branch section of your code it doesn't return anything
Let me if the solution helps.

Thanks and Regards,
Shiva RV
Ebi AmabebeEbi Amabebe
Thank you for your response Shiva!

I'm not sure what you mean. I should move my results variable definition to ouside my if statement? 

 
Shiva RajendranShiva Rajendran
yes Ebi
Shiva RajendranShiva Rajendran
yes Ebi you are right.
Long story short , you must move your return results ; statement at the end of the function.
In this case , you must also declare the results variable somewhere at the start of the function not inside any code block

Let me know if it works fine

Thanks and Regards,
Shiva RV
This was selected as the best answer