0

I have one picklist on VF page based on changes this picklist I am rendering one VF component which contains some input fields. With the help of I am trying to create a case and Attachment (case related list ). the case created successfully but file not attached. I have checked the debug log which shows attachment body is null.

Please help me out this.

Please help me out this.

<------------ VF page---------------->



<!-- jquery datepicker plugin -->
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>        
<style>
    .navbar .navbar-nav > .flm_Assist > a,
        .navbar .navbar-nav > .flm_Assist > a:focus,
            .navbar .navbar-nav > .flm_Assist > a:hover {
                color: #fff !important;
                background-color: #fbb601 !important;
                padding-left: 10px;
            }
.customValildationError{
    outline: 0;
    border-color: #c00 !important;
    background-color: rgb(255, 255, 255) !important;
    box-shadow: 0 0 3px #c00;
}
.customValildationError:focus {    
    border-color: none;    
    box-shadow:none;
}
.requiredInput{
    border-left-color: #c00;
    border-left-style: solid;
    padding-left: 2px;
}
.errorMsg{
    color: #c00;
}
.form-control-label{
    margin-top:5px;
}
.loadingBG{
    position: fixed;
    top: 0; 
    left: 0;
    right: 0; 
    bottom: 0; 
    opacity: 0.5; 
    z-index: 1000; 
    background-color:#23466c;
}
.loadingBGP{
    position: fixed; 
    left: 0; top: 0; 
    bottom: 0; 
    right: 0; 
    z-index: 1001; 
    margin: 15% 50%;        
} 
.lookupInput {
    width: 100%;
    float: left;
    position: relative;
}
.lookupInput input {
    width: 90%;
    float: left;
}
.lookupInput a{
    position: absolute;
    width: 33px !important; 
    height:38px;
    margin-left: 5px;
    background-image: url(/img/setup/search-icon.png) !important;
    background-repeat: no-repeat !important;
    background-position: center !important;
    display: inline-flex !important;
    right:0;
}
.lookupInput a img{
    display:none !important;

}       
</style>  

<script type="text/javascript" >
    $(document).ready(function() {     
        $(".customDatePicker").datepicker();            
    });
</script>
<body>
    <apex:form id="IdFrm" html-autocomplete="off">
        <!-- Left Panel -->
        <aside id="left-panel" class="left-panel">
            <c:SideMenuBar />
        </aside>
        <!-- Left Panel -->            
        <!-- Right Panel -->            
        <div id="right-panel" class="right-panel">                
            <!-- Header-->
            <c:HeaderBar />
            <!-- Header-->                                
            <!--Main container -->  
            <div class="content mt-3">                                     
                <div class="col-lg-3">
                    <div class="assist-side-menu">                             
                        <!-- Assist type dropdown menu -->
                        <apex:actionRegion >
                            <div class="dropdown">
                                <apex:selectList id="IdPkcList" value="{!selectedAssist}" multiselect="false" size="1" styleClass="btn dropdown-toggle btn-primary ">
                                    <apex:selectOptions value="{!assistType}"/>
                                    <apex:actionSupport event="onchange" action="{!actionSupportOCMethod}" reRender="IdAssistFormHeader,IdAssistContent,IdTicketStatus,IdTicketDetails" status="IdStatus" oncomplete="myInit();" />
                                </apex:selectList>
                            </div>                                          
                        </apex:actionRegion>                            
                    </div>
                </div>
                <div class="col-lg-9">                       
                    <div class="assist-content-area">
                        <div class="services-content">
                            <apex:outputPanel id="IdAssistContent">
                                <!-- default msg seciton once page load -->                             
                                <apex:outputPanel id="IdAssistForm" rendered="{!IsAssist}">
                                    <apex:outputPanel rendered="{!if(selectedAssist==null,true,false)}">
                                        <div class="assist-content-area">
                                            <div class="services-content">
                                                <h3>University Services</h3>                                                    
                                            </div>
                                        </div>
                                    </apex:outputPanel>
                                    <!-- assist request form section -->                                    
                                    <apex:outputPanel rendered="{!if(selectedAssist!=null,true,false)}">  
                                        <script type="text/javascript" >
                                        $(document).ready(function() {     
                                            $(".customDatePicker").datepicker({ dateFormat: 'dd/mm/yy' }).val();                                
                                        });       
                                        </script>
                                        <apex:pageBlock mode="maindetail">
                                            <div id="IdServiceFrm">                                                        
                                                <apex:outputPanel rendered="{!if(selectedAssist=='Academic Services',true,false)}">
                                                    <c:AcademicServices objCase="{!objCase}" attachment="{!objAttach}" />                                                                           
                                                </apex:outputPanel>                                                 
                                                <apex:outputPanel rendered="{!if(selectedAssist=='Faculty Services',true,false)}">
                                                    <c:FacultyServices objCase="{!objCase}" attachment="{!objAttach}"/>
                                                </apex:outputPanel>
                                            </div>                                                                                        
                                            <br/>                                                
                                        </apex:pageBlock>
                                    </apex:outputPanel>                                    
                                </apex:outputPanel>                                      
                            </apex:outputPanel>                                
                        </div>
                    </div>
                </div>               
            </div>
            <!--Main container -->
        </div><!-- /#right-panel -->            
        <!-- Right Panel --> 
        <!--  -->
        <apex:actionFunction name="saveCase" action="{!saveCase}"/>
        <apex:actionStatus id="IdStatus">
            <apex:facet name="start">
                <div class="loadingBG" >
                    &nbsp;
                </div>  
                <div class="loadingBGP" >                        
                    <i class="fa fa-spinner fa-spin" style="font-size:48px;color:#fbb601;"></i>                        
                </div>            
            </apex:facet>           
        </apex:actionStatus>
    </apex:form>                
</body>
<script>           
function checkValidation(){
    var isValid = true;
    var requiredField = $('.requiredInput').find('input, select');
    $(requiredField).each(function(){            
        var isNullBlank = $(this).val();
        if(isNullBlank==''){                
            $(this).addClass('customValildationError');                
        }else{
            $(this).removeClass('customValildationError');
        }
    });
    $(requiredField).each(function(){            
        var isNullBlank = $(this).val();
        if(isNullBlank==''){                
            isValid = false;                
        }
    });
    if(isValid){
        console.log('form is valid');
        saveCase();
    }else{
        console.log('form is invalid');
    }
}    
</script>    

<div style="margin-bottom: 15px;background: #23466c;color: #fff;text-transform:capitalize;padding: 0.5em;">
    <h6>Academic Service</h6>
</div>
<!-- Static Field  Section -->
<div class="form-control-label form_label">        
    <apex:outputText value="Assistance Type" styleClass="form-control-label"/>
</div>
<div class="form-control-label">
    <apex:inputField styleClass="form-control" value="{!objCase.HLP_Assist_Type__c}" required="true"/>
</div>
<div class="form-control-label form_label">        
    <apex:outputText value="Service Type" styleClass="form-control-label"/>
</div>
<div class="form-control-label">
    <apex:inputField styleClass="form-control" value="{!objCase.HLP_Category__c}" required="true"/>
</div>
<div class="form-control-label form_label">        
    <apex:outputText value="Sub Category" styleClass="form-control-label"/>
</div>
<div class="form-control-label">
    <apex:inputField styleClass="form-control" value="{!objCase.HLP_Sub_Category__c}" required="true"/>
</div>
<div class="form-control-label form_label">        
    <apex:outputText value="Attendance Exemption From Date" styleClass="form-control-label"/>
</div>
<div class="form-control-label">
    <apex:inputField id="datetimepicker1" styleClass="form-control customDatePicker" value="{!objCase.Attendance_Exemption_From_Date__c}" />    
</div>
<div class="form-control-label form_label">        
    <apex:outputText value="Attendance Exemption To Date" styleClass="form-control-label"/>
</div>    
<div class="form-control-label">
    <apex:inputField styleClass="form-control customDatePicker" value="{!objCase.Attendance_Exemption_To_Date__c}"/>
</div>
<div class="form-control-label form_label">        
    <apex:outputText value="Subject" styleClass="form-control-label"/>
</div>
<div class="form-control-label">
    <apex:inputField styleClass="form-control" value="{!objCase.Subject}" required="true"/>
</div>    
<!-- attachment section -->
<div class="form-control-label">        
    <apex:outputText value="Attachment" styleClass="form-control-label"/>
</div>
<div class="form-control-label">                                                        
    <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>        
</div>
<!-- submit button -->
<div class="form-control-label" style="margin-bottom: 20px;    padding-top: 25px;">    
    <apex:commandButton styleClass="btn btn-primary  saveBtn" style="float:right;" onclick="checkValidation(); return false;" value="Submit"/>
</div>    

< !--- Apex Method ----->

public pageReference saveCase(){
        System.debug('Save Mehtod start');
        try{
            //-- create new case
            insert objCase;
            System.debug('New Case : '+ objCase);
            IsCreated = true;
            //-------- for attachment upload ----------            
            System.debug('Attach body : '+objAttach.Body);
            if(objCase.Id!=null && objAttach.Body!=null){ 
                objAttach.ParentId = objCase.Id;
                insert objAttach;
            }             
            objCase = new Case();
            objAttach = new Attachment();
        }catch(Exception ex){
            objAttach = new Attachment();
            System.debug('Case Error : '+ ex.getMessage());
        }
        return null;
    }
  • Can you make your problem more specific? It is very hard to understand your problem with such long code snippets. – sanket kumar Sep 7 '18 at 5:54
  • Thank you Sanket for quick response. the problem is when i click the submit button i am creating a new case with attachment. the case created successfully but file not attached. attachment body is null every time. – GmDeepakChounan21 Sep 7 '18 at 6:08
0

You need to do few changes in your code. The way you were doing is referring to a null value.

In your Controller, define a few variables as follows:-

public Blob AttchBody {get;set;}
public String AttchDesc {get;set;}
public String AttchName {get;set;}
Public Attachment attch {get;set;}

and In your page change the apex input field as follows:-

<apex:inputText value="{!AttchName}" label="name" />
<apex:inputFile value="{!AttchBody}" fileName="{!AttchName}"  />

In your controller,

    try{
          insert objCase;
          IsCreated = true; 
          if(objCase.Id!=null && AttchBody!=null){ 
          attch = new Attachment(ParentId=objCase.Id,Description=AttchDesc,Name=AttchName,Body=AttchBody);
          insert attach;
         }  

Hope it helps you.

  • Thank you Sanket, I have updated my code with this. but still getting the same problem. – GmDeepakChounan21 Sep 10 '18 at 9:09

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.