• Ebi Amabebe
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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
       
  }
So I have this Command command button on a inline visual force page. 

 
<apex:commandButton id="urlButton" title="Send selected file(s) to MCA" value="Send selected file(s) to MCA" onclick="SendObject();return false;"/>
  <apex:commandButton id="sendurlbutton" title="Send to MCA with Docs" value="Send to MCA with Docs" onclick="generateUrls();return false;"/>
  </apex:form>
it calls the function generateUrls, the function is:
 
function generateUrls()
    {
        var isSelected = false;
        var inputElem = document.getElementsByTagName("input");
        var selectedIds = new Array();
        for(var i=0; i<inputElem.length; i++)
        {   
            if(inputElem[i].id.indexOf('checkedone')!=-1)
            {
                if(inputElem[i].checked)
                {
                    var checkedElement = document.getElementById(inputElem[i].id);
                    var closedTr = $j(checkedElement).closest('tr');
                    var menuForSelectedItem = $j(closedTr).find(".menubutton");
                    if(menuForSelectedItem)
                    {
                        selectedIds.push($j(menuForSelectedItem).attr('id').split('_')[1]);
                    }
                }
            }       
        }
        if(selectedIds.length == 0)
        {
            alert('Please select a file(s) before uploading!');
        }
        else
        {   
            var htmlText = '<div style="margin-bottom:10px;"><strong>Sending selected file(s) to MCA.</strong></div><div>Please wait for file status to appear...</div>';
            var dlg = $j('<div title="MCA File Upload Status..." />').html(htmlText).dialog({ width: 500, height: 250 });
            
            Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.GenerateUrlExtension.submitDocumentsToMCA}',
            selectedIds, 
            '{!MCA_Submission__c.Opportunity__c}', 
            '{!MCA_Submission__c.id}',
            '{!MCA_Submission__c.App_Id__c}',
            function(result, event){
                var updateText;
                if (event.status) {
                    updateText = '<div><strong>Upload Results:</strong></div><ul>';
                    for(var i=0; i < result.length; i++) {
                        updateText += '<li style="margin-bottom:10px;">' + result[i] + '</li>';
                    }
                    updateText += '</ul><div><strong>Process Complete.</strong></div>';
                    dlg.html(updateText);
                } else if (event.type === 'exception') {
                    alert('Exception' + event.message); 
                } else {
                    alert('Error' + event.message); 
                }
            }, 
            {escape: true}
        );

 So I have a custom button, and I'm trying to execute the same method however I'm not sure how I can get the attachmentids in a custom button. My question is it is possible? What am I missing. Here is what I am trying...
 
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/26.0/apex.js")}

var mca_id = "{!MCA_Submission__c.Id}";
var app_id = "{!MCA_Submission__c.App_ID__c}";
var sub_id = "{!MCA_Submission__c.Opportunity__c}";




var upload = sforce.apex.execute("GenerateUrlExtension","submitDocumentsToMCA",{sub_id,mca_id,app_id,  });



alert("Result: " + mca_request + upload);
document.location.reload(true);

in a custom button. I'm just not sure how to get the attach ment Ids in a var and then to the method. 
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
       
  }