Saturday, July 1, 2017

Upload File using Visualforce page and Dynamic fields display with onchange event.

Hi,

I have requirement like based on Pick list value fields has to show or hide and upload a file using <apex:inputFile> attach to object. Based on Do you have Vendor Number? it is show or hide some of the fields using action support.

Visualforce Page:

<apex:page controller="CaseWithVendorCLS2" showHeader="true"  tabStyle="case">
<apex:pageMessages />
 <apex:form id="theFm"  enctype="multipart/form-data">

  <apex:pageblock id="thePb"> 
  <apex:pageblockButtons >
   <apex:commandButton value="Save" action="{!save}"/>
   <apex:commandButton value="Cancel" action="{!Cancel}" immediate="true" />
  </apex:pageblockButtons>
  
  <apex:actionRegion >
  <apex:pageblockSection id="vendorDet" columns="2" title="Vendor Details" collapsible="false">
   <apex:inputField value="{!caseObj.Has_Vendor_Number__c}">  
  <apex:actionSupport event="onchange" reRender="thePb"            status="actStatusId">
    <apex:actionStatus id="actStatusId" >
                <apex:facet name="start" >
                  <img src="/img/loading.gif" />                    
                </apex:facet>
    </apex:actionStatus>
   </apex:actionsupport>
   </apex:inputField> 
    <apex:inputField value="{!caseObj.Vendor_Number__c}"/>
  </apex:pageblockSection>
  </apex:actionRegion>
  
  <apex:pageblockSection id="personDet" title="Personal Details" collapsible="false" rendered="{!caseObj.Has_Vendor_Number__c == 'No'}" > 
   <apex:inputField value="{!caseObj.First_Name__c}" required="{!caseObj.Has_Vendor_Number__c == 'No'}"/>
   <apex:inputField value="{!caseObj.Last_Name__c}" required="{!caseObj.Has_Vendor_Number__c == 'No'}" />
   <apex:inputField value="{!caseObj.Email__c}" required="{!caseObj.Has_Vendor_Number__c == 'No'}" />
   <apex:inputField value="{!caseObj.Phone__c}" required="{!caseObj.Has_Vendor_Number__c == 'No'}" />  
  </apex:pageblockSection>
  
  <apex:pageblockSection id="caseDet" title="Case Details" collapsible="false"> 
   <apex:inputField value="{!caseObj.Type}"/>
   <apex:inputField value="{!caseObj.Priority}"/>
   <apex:inputField value="{!caseObj.Reason}"/>
   <apex:inputField value="{!caseObj.Subject}" required="true" />
   <apex:inputField value="{!caseObj.Description} " required="true" />  
     <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>
    
  </apex:pageblockSection>
  
  </apex:pageblock> 

 </apex:form>
</apex:page>


Controller Class:

global class CaseWithVendorCLS3 {

public Case caseObj {get;set;}

public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
}

public CaseWithVendorCLS3(){
 caseObj = new Case();
 caseObj.Has_Vendor_Number__c = 'Yes';

}

public PageReference save(){
PageReference pgr=null;
if((caseObj.Vendor_Number__c !=null && caseObj.Vendor_Number__c.length()>0) && caseObj.Has_Vendor_Number__c =='Yes'){
 List<Account> accList = [Select id from Account where accountNumber = :caseObj.Vendor_Number__c Limit 1];
 system.debug('accList is-->'+accList );

 if(accList !=null && accList.size()>0){
   caseObj.AccountID = accList.get(0).id;
   List<Contact> contctsLst = [select id, Email,Phone from Contact where accountId= :accList.get(0).id];
   if(contctsLst  !=null && contctsLst.size()>0){
    system.debug('accObj.contacts-->'+contctsLst);
     caseObj.ContactID =  contctsLst.get(0).id;
     caseObj.SuppliedEmail =  contctsLst.get(0).Email;
     caseObj.SuppliedPhone =  contctsLst.get(0).Phone;
   }
 }
 else{
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Account details with vendor number is not available.'));
   return pgr;
 }
 }
 else if(caseObj.Vendor_Number__c ==null && caseObj.Has_Vendor_Number__c =='Yes'){
   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please provide vendor number if you have vendor Number.'));
   return pgr;
 }
   system.debug('caseObj values is=='+caseObj);
   try{
   insert caseObj;
  if(attachment.name != null && attachment.name.length()>0){
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = caseObj.id; // the record the file is attached to
    attachment.IsPrivate = false;
    insert attachment;
  }
   
  pgr = new PageReference('/apex/caseWithVendorThankyou');
  pgr.setRedirect(true);
   
  }catch(Exception e){
    system.debug('exception is-->'+e.getMessage());
 }
  
return pgr;

}

public PageReference cancel(){
PageReference pgr = new PageReference('/apex/caseWithVendor3');
pgr.setRedirect(true);

return pgr;

}

}

Output :

save image


save image

No comments:

Post a Comment