[SalesForce] Having problems passing a variable and displaying a Message

I'm new to VisualForce and to Java,

I have a custom object I created called "Inquery__c"
And I have a few users on record,

I'm trying to allow only the users that exist in the "Inquery__c" to upload a file to Documents,

Here is the code

<apex:page controller="newCheck_and_upload" tabStyle="Opportunity">
<apex:sectionHeader title="Please Enter user information and upload LogFile"/>
<apex:messages /> 
<apex:form enctype="multipart/form-data">
   <apex:pageBlock>
      <apex:facet name="footer">
      <apex:outputPanel >
         <apex:commandButton action="{!save}" value="Check and Uplod" styleClass="btn"/>
      </apex:outputPanel>
      </apex:facet>
   <apex:pageBlockSection title="User Information">
      <apex:panelGrid columns="2">
         <apex:outputLabel value="First Name" for="inqueryFirstName"/>
         <apex:inputField id="inqueryFirstName" value="{!inquery.First_Name__c}"/>
         <apex:outputLabel value="Last Name" for="inqueryLastName"/>
         <apex:inputField id="inqueryLastName" value="{!inquery.Last_Name__c}"/>
         <apex:outputLabel value="Email" for="inqueryEmail"/>
         <apex:inputField id="inqueryEmail" value="{!inquery.Email__c}"/>
      </apex:panelGrid>
   </apex:pageBlockSection>
   <apex:pageBlockSection title="Upload File">
      <apex:panelGrid columns="2">
         <apex:outputLabel for="file"/>
         <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
      </apex:panelGrid>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

And the Class

public  with sharing class newCheck_and_upload {

Inquery__c inquery;
public Boolean sFlag;

public Inquery__c getInquery() {
if(inquery == null) inquery = new Inquery__c();
   return inquery;
}

public Document document {
get {
  if (document == null)
    document = new Document();
  return document;
}
set;
}


 public PageReference save() {

     if(inquery == null) {
      sFlag = false;
     }else {
     sFlag = true;
     }  

 if(sFlag){   
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'User Exists'));
    // sFlag = false;
    } else {
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'User Does not exist'));
     //return null;
    }

      insert inquery;

    document.AuthorId = UserInfo.getUserId();
    document.FolderId = UserInfo.getUserId(); // put it in running user's folder

    try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    } finally {
      document.body = null; // clears the viewstate
      document = new Document();
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));

    return null;
 }

}

When I enter a user that exists or a user that does not exist (both ways) the file is never uploaded. This means that sFlag is never True.

When i try saving an existing file i get the following error:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Email__c duplicates value on record with id: a08K0000001XxV5: []

Error is in expression '{!save}' in component in page exist_upload.

.

This means that the "inquery == null" stament is not working,.

And it return null,.

But when it tries to save the account,.

it tells me that the account is a duplicate!!.

.

Does anyone know why?

Can someone please help?

Thanks

Best Answer

I think I can answer this part of your question real quick:

2) The second problem I'm having is that the "ApexPages.addMessage" doesn't work. No messages are displayed in any of the cases. Not sure what I'm doing wrong.

At the second line of your initial VF page, needs to have the following: <apex:pagemessages/>

In essence, you don't have anyplace for the the messages to appear! Once you add that line, any error messages should begin to appear. Once you get your error messages working for you, I suspect you'll be able to sort out the root cause of your other problem.

That said, off hand, I don't think you want to be returning anything from that IF statement. Removing the return null appears to be what you need to do. It would seem you've detected that sFlag=true;? For the logic in your controller, do you also want to set it as such? Unfortunately, I don't have time right now to go through all of your code, but I hope this helps get you moving along and pointed in the right direction. :)

Related Topic