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
SabrentSabrent 

Conditional function

I am trying to alert a reminder if file is not selected to upload. I know i am doing something wrong in the function displaymessage(). Can someone point me to a correct way of doing this. Thanks!

 

 

<apex:page id="thePage" standardController="Account" extensions="attachmentsample">
<script type="text/javascript">
function displaymessage(){
var fileInput = document.getElementsByNameId("{!$Component.thePage.theForm.pageBlock.pageBlockSection.fileUpload}");
if(fileInput=="" ||fileInput==null){
alert("You must attach documents !");
}
else {
alert("You have successfully attached documents");
}

}
</script>

<apex:form id="theForm">
<apex:sectionHeader title="Upload a Attachment into Salesforce"/>
<apex:pageblock id="pageBlock" >
<apex:pageblocksection id="pageBlockSection" columns="1">
<apex:inputfile id="fileUpload" value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Click Here to Attach File" action="{!Savedoc}" onclick="displaymessage()" immediate="true"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>

</apex:page>

 

Starz26Starz26

Try changing

 

var fileInput = document.getElementsByNameId("{!$Component.thePage.theForm.pageBlock.pageBlockSection.fileUpload}");

 

to

 

var fileInput = document.getElementById('{!$Component.fileUpload}').value;

Or keep and change the " to ' and add .value

 

Also change onclick="displaymessage() to onclick="return displaymessage();"

 

in the saveDoc() method make sure it is a pagereference and returns Null

SabrentSabrent

Thanks @Starz26 I will try that.

SabrentSabrent

Did as suggested, without success. What am I doing wrong?

 

function displaymessage(){
var fileInput = document.getElementsByNameId("{!$Component.fileUpload}").value;
if(fileInput=="" || fileInput==null){
alert("You must attach documents !");
}
else {
alert("You have successfully attached documents");
}
}
....
<apex:pageblocksection id="pageBlockSection" columns="1">
<apex:inputfile id="fileUpload" value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Click Here to Attach File" action="{!Savedoc}" onclick="return displaymessage()"/>
</apex:pageblocksection>


Controller

Public Pagereference Savedoc() {
String accid = System.currentPagereference().getParameters().get('id');

Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);

/* insert the attachment */
insert a;
return NULL;
}

 

Starz26Starz26
function displaymessage(){
var fileInput = document.getElementsByNameId('{!$Component.fileUpload}').value; 
if(fileInput=="" || fileInput==null){
    alert("You must attach documents !");
    }
else {
    alert("You have successfully attached documents");
    }
}
....
<apex:pageblocksection id="pageBlockSection" columns="1">
          <apex:inputfile id="fileUpload" value="{!myfile.body}" filename="{!myfile.Name}" />
           <apex:commandbutton value="Click Here to Attach File" action="{!Savedoc}" onclick="return displaymessage();"/>        
      </apex:pageblocksection>


Controller

 Public Pagereference Savedoc()     {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
         
         /* insert the attachment */
         insert a;
       return NULL;
    } 

 Try changing to above. you still had the " " around !$Component.fileUpload instead of single ' '

 

Also, added a ; after displayMessage()

 

Java is very specific and while it may look ok, it is not.

 

You can test where the Java is failing by putting alert('Alert'); before a line in the script. If the messge pops up then the java runs at least until that point. Move the alert within the java script (like after the var fileinput) and if it poped up before then does not you know the var line is not working.

 

Others may have a better way to test the java but this is what I do to quickly debug where in the script is failing.

 

 

SabrentSabrent

I tried as suggested without success.After moving the Alert('message') it seems that the following line is failing.

I also tried document.getElementsById instead of document.getElementsByNameId

Any suggestions?? Than you once again and I am very grateful for your time and help.



 

var fileInput = document.getElementsByNameId('{!$Component.fileUpload}').value; 



Starz26Starz26

Someone with more experiece may need to chime in. I had the exact code I gave you  working but it was using an inputText and not inputFile.

 

Sorry I could not be of more help.

 

 

SabrentSabrent

Yeh you are right it works with inputText. 

That's all right, I hope someone has a suggestion.

Unfortunately Salesforce Help Desk doesn't support javascript code.

 

Thanks. You have been very helpful.

jshimkoskijshimkoski

Quick note, document.getElementsByNameId does not exist in JavaScript.

Try the following:

 

var fileInput = document.getElementById('fileUpload').value;

 

SabrentSabrent

 Yeh i tried that  yet no alert. I must be doing something silly. 

 

 function displaymessage(){
    	var fileInput = document.getElementsById('page1:form1:mainPageBlock:pageBlockSectionb:fileUpload').value; 
		if(fileInput=="" || fileInput==null){
    	alert("You must complete step4 after all of your documents are attached!");	
    	}
		else {}
    }  

 

Also tried

document.getElementsById('fileUpload').value;


jshimkoskijshimkoski

"Element" is not plural. IDs must be unique in html, meaning, you cannot have more than one element with "fileInput" as its ID.

 

It should be:

document.getElementById('fileUpload').value;

 

Not:

document.getElementsById('fileUpload').value;

 

Copy and paste the following:

 

function displaymessage(){
	var fileInput = document.getElementById('fileUpload').value; 
	if(fileInput == "" || fileInput == null)
	{
		alert("You must complete step4 after all of your documents are attached!");	
	}
}

 

That should work for you.

SabrentSabrent

I very much appreciate the suggestions provided by everyone.

 

sadly this still doesn't work. It's very frustrating.

 

jshimkoskijshimkoski

What browser are you using by chance?

 

The file input is an extremely pesky form field due to varying browser security restrictions.

 

Try it out in Chrome or Firefox if you haven't.

SabrentSabrent

I tried it in firefox. No success.

Thanks for following it up. Much appreciated!!!