...

View Full Version : 2nd function only if form.submit() is successful?



tsalb
08-19-2011, 07:59 PM
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)


<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.


<apex:actionFunction name="insertFile" action="{!insertFile}" oncomplete="submitFile();return false;"/>

the pertinent "html" (visualforce) for this page


<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:


//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...


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/AmazonS3/latest/dev/index.html?HTTPPOSTForms.html)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum