Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2nd function only if form.submit() is successful?

    Newbie developer/sys admin working on a SalesForce platform here, so excuse my ignorance with terminology.

    This is a file upload problem. The current code first inserts a record into Salesforce, THEN posts to Amazon S3. The record is just a unique URL that is a link to where Amazon houses the file. Problem here is that sometimes users cancel/close the page before form submits, but the record is already created. Now, I need to reverse this so that it first posts to Amazon S3, then inserts the "link" record.

    Is there a way to evaluate if a post is successful THEN trigger the apex method OR a second javascript function?

    Relevant bits here: (if we need the entire page, i can paste that too, but alot of it is apex and salesforce specific)

    Code:
            <script type="text/javascript">
            var sendFile = false;
            var ProgressImage = document.getElementById('{!$Resource.ajaxloader}');
            document.getElementById('{!$Component.hiddenServerURL}').value = '{!$Api.Enterprise_Server_URL_140}';       
            function setFileName(file) {
                var f = file.replace(/\\/g, "");
                f = f.replace(/C\:fakepath/g, ""); <!--Required for IE8-->
                document.s3Form.key.value = "{!CustomerName}/{!OrderName}/" + f;
                document.getElementById('{!$Component.fileName}').value = f;
                suffix = f.lastIndexOf(".") + 1;
                contentType = f.slice(suffix);
                document.getElementById('{!$Component.contentType}').value = contentType;
            }
            function setFileType(type) {
                document.getElementById('{!$Component.fileType}').value = type;
            }
            function checkFile() {
                if (document.s3Form.file.value=="") {
                    alert("Please, select a file.");
                    return false;
                } 
                else if (document.s3Form.fType.value=="--None--") {
                    alert("Please, select a file type.");
                    return false;
                }
                else {     
                	loadSubmit();       
                  	insertFile();
                   	sendFile = true;
                }
            }
            function submitFile() {
                if(sendFile = false) {
                return false;
                }
                else {
                document.s3Form.submit();
                }
            }
            function loadSubmit() {
    			document.getElementById("progress").style.visibility = "visible";
    			setTimeout(function(){ProgressImage.src = ProgressImage.src},100);
    			return true;
    		}    
            function cancelFile() {  
                window.location.href = "{!$Setup.companyInfo__c.colliers__c}";
            }
            function completeFile() {
                completeOrder();
            }   
        </script>
    apex:actionFunction on this page, javascript can directly make a call to the class and invoke a method there.

    Code:
    <apex:actionFunction name="insertFile" action="{!insertFile}" oncomplete="submitFile();return false;"/>
    the pertinent "html" (visualforce) for this page

    Code:
                <apex:pageBlockButtons location="bottom">
                    <input class="btn" type="button" value="Upload File" onClick="checkFile();return false;"/>                
                    <input class="btn" type="button" value="Cancel" onClick="cancelFile();return false;"/>
                    <input class="btn" type="button" value="Complete Order" onClick="completeFile();return false;"/>
                </apex:pageBlockButtons>
    This piece is from the class/extension:

    Code:
        //SF File insert on an object (passed from page)
        public PageReference insertFile() {
            this.file.Name = fileName;
            this.file.Type__c = fileType;
            this.file.Content__c = contentType;
            insert this.file;
            return null;
        }
    Ideally, i would like to change the checkFile() function to something like the below - but haven't had much luck in researching how to accomplish this...

    Code:
            function checkFile() {
                if (document.s3Form.file.value=="") {
                    alert("Please, select a file.");
                    return false;
                } 
                else if (document.s3Form.fType.value=="--None--") {
                    alert("Please, select a file type.");
                    return false;
                }
                else {     
                	loadSubmit();       
                  	if(document.seForm.submit() = true)
    			insertFile();
                }
            }
    UPDATE:
    I just learned that S3 should return a "success_action_status" if the post is successful - how do i use this to achieve what I need? (http://docs.amazonwebservices.com/Am...POSTForms.html)
    Last edited by tsalb; 08-19-2011 at 08:04 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •