+ Start a Discussion
ssousanssousan 

Having problems passing a variable

Hi Everyone,

 

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:outputLabel value="Institution" for="inqueryInstitution"/>
        <apex:inputField id="inqueryInstitution" value="{!inquery.Institution__c}"/>
        <apex:outputLabel value="Position" for="inqueryPosition"/>
        <apex:inputField id="inqueryPosition" value="{!inquery.Position__c}"/>
        <apex:outputLabel value="Country" for="inqueryCountry"/>
        <apex:inputField id="inqueryCountry" value="{!inquery.Country__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=true;        

 

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'));                 

} 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 <apex:page> 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?

 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Nantha_WipNantha_Wip

 

try the below code..

 

 

Create inquiry,document property and initialize that vairables in class constructor...

 

 

<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:outputLabel value="Institution" for="inqueryInstitution"/>
        <apex:inputField id="inqueryInstitution" value="{!inquery.Institution__c}"/>
        <apex:outputLabel value="Position" for="inqueryPosition"/>
        <apex:inputField id="inqueryPosition" value="{!inquery.Position__c}"/>
        <apex:outputLabel value="Country" for="inqueryCountry"/>
        <apex:inputField id="inqueryCountry" value="{!inquery.Country__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>



public  with sharing class newCheck_and_upload {

  public inquery__c inquery{get;set;}    
  public Boolean sFlag=true;        
  public Document document{get;set;}
 
   public newCheck_and_upload()
   {
       inquery=new product2 ();
       document=new Document();
   }

public PageReference save() {        

if(inquery == null) {          

sFlag = false;         

}else {         

sFlag = true;         

}            

if(sFlag){           

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'User Exists'));                 

} 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;     

} }

 

 

Please mark this as answer if its solve your problem...

 

All Answers

Nantha_WipNantha_Wip

 

try the below code..

 

 

Create inquiry,document property and initialize that vairables in class constructor...

 

 

<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:outputLabel value="Institution" for="inqueryInstitution"/>
        <apex:inputField id="inqueryInstitution" value="{!inquery.Institution__c}"/>
        <apex:outputLabel value="Position" for="inqueryPosition"/>
        <apex:inputField id="inqueryPosition" value="{!inquery.Position__c}"/>
        <apex:outputLabel value="Country" for="inqueryCountry"/>
        <apex:inputField id="inqueryCountry" value="{!inquery.Country__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>



public  with sharing class newCheck_and_upload {

  public inquery__c inquery{get;set;}    
  public Boolean sFlag=true;        
  public Document document{get;set;}
 
   public newCheck_and_upload()
   {
       inquery=new product2 ();
       document=new Document();
   }

public PageReference save() {        

if(inquery == null) {          

sFlag = false;         

}else {         

sFlag = true;         

}            

if(sFlag){           

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'User Exists'));                 

} 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;     

} }

 

 

Please mark this as answer if its solve your problem...

 

This was selected as the best answer
ssousanssousan

Its giving me errors,

Because of this part,

 

   public newCheck_and_upload()
   {
       inquery=new product2 ();
       document=new Document();
   }

 

But the Doument variable is fine,

The file uploads, no problem,

 

Best

SFDC_VikashSFDC_Vikash

Hi 

 

In your case it will be as :

 

 public newCheck_and_upload()
   {
       inquery=new Inquery__c();
       document=new Document();
   }

Nantha_WipNantha_Wip

Hi,

 

Replace product2 with your custom object since i have used product object for testing..

 

 

ssousanssousan

I've solved this problem,

But in a different way,

 

Thanks everyone for thier help,

 

Best