1

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

1

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

  • Great that did fix the second problem, and it gave me an indication why the first one is happening. The object is returning the user does not exist, even though the user does exist. Is this a permission issue? – user2333346 May 2 '13 at 21:12
  • I suspect your problem may be more one of how you're using sFlag twice in If statements within your class without calling it as a subclass of it's own. In both IF statements, it can be assigned a value. That's likely creating conflicts with it being a public variable. – crmprogdev May 2 '13 at 21:41
  • Can you please give me an example [url] of what you mean? – user2333346 May 2 '13 at 21:51
  • By the way, you are right its the sFlag, its not passing between subs, I checked. But if I don't declare it as a public variable how will it pass between subs? – user2333346 May 2 '13 at 21:56
  • I think your problem is this, Inquery__c getInquery() needs to be called to set the state of sFlag, but you're returning null from it. As such, the state of sFlag isn't being preserved in memory to be passed to PageReference save(). So you need to return sFlag. In addition, you may need to explicitly call a new instance of Inquery__c getInquery() from PageReference save() in order to get the state of sFlag before you can run your If statement on it in that sub. – crmprogdev May 2 '13 at 22:05
2

Looking at your code looks like you will need to some more adjustments to get to where you wish to be. Instead check the code below, I assume the scenario is you need to check if the user is already existing in the org if so he can upload the file : If the above is true check the code below :

Page:

   <apex:page controller="newCheck_and_upload" tabStyle="Opportunity">
<apex:sectionHeader title="Please Enter user information and upload LogFile"/>
<apex:pagemessages /> 

<apex:outputtext value="User's Phone:" rendered="{!users_phone_number != ''}"/>
<apex:outputtext value="{!users_phone_number}" rendered="{!users_phone_number != ''}"/>

<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}" required="true"/>
         <apex:outputLabel value="Last Name" for="inqueryLastName"/>
         <apex:inputField id="inqueryLastName" value="{!inquery.Last_Name__c}"  required="true"/>
         <apex:outputLabel value="Email" for="inqueryEmail"/>
         <apex:inputField id="inqueryEmail" value="{!inquery.Email__c}"  required="true"/>
      </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>

controller:

   public  with sharing class newCheck_and_upload {

public Inquery__c inquery{get;set;}
public Document document {get;set;}
public string   users_phone_number{get;set;}
public List<user> u {get;set;}
public newCheck_and_upload(){
inquery = new Inquery__c();
document = new Document();
u = new List<user>();
users_phone_number = '';
}

 public PageReference save() {
 string user_query = '';


 if(inquery.first_name__c.length() == 0 && inquery.last_name__c.length() == 0 && inquery.email__c.length() == 0 ){   
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'One of the inputs to filter the user is missing. Please enter all the fields.'));
    } else {
    user_query = 'select id,name,phone from user where firstname like \'%' + inquery.first_name__c +'\' and lastname like \'%' + inquery.last_name__c +'\' and email like \'%'+ inquery.email__c +'\' limit 1';
     u = database.query(user_query);
    }
    if(u.size() >0){
        users_phone_number = u[0].phone;
        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'));
    }
    else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'User does not exist in the DB. Cannot upload file.'));

    }

    return null;
 }
 }
  • Can you tell me how to return another field, based on the email given by the user. For example how do I return the phone number for a user, after verifying the user exists. – user2333346 May 3 '13 at 17:07
  • Modified my answer – Rao May 3 '13 at 17:28
  • Im getting an error here, so im assuming that the "u" object now has all the user information, is that correct? if so I changed users_phone_number = u[0].Phone_number__c; to macth my object name. and it tells me that "Error: newCheck_and_upload_test Compile Error: Invalid field last_name__c for SObject User". – user2333346 May 3 '13 at 17:53
  • if I use the users_phone_number = u[0].phone; as is it gives me an error at run time – user2333346 May 3 '13 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.