function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Anspaugh 6Jonathan Anspaugh 6 

Field update on file upload

I am working on a file upload that will change 2 fields on the record when a document is uploaded. Right now I need to change the status and the date received fields when the file is uploaded through a visualforce page.

Currently my apex class is as follows:
public with sharing class cc_AppChecklist_List{ 

    public List<Application_Checklist__c> Records {get; set;} 
    String AppID = ApexPages.currentPage().getParameters().get('appid');
    
    public cc_AppChecklist_List(){ 
        Records = [SELECT Id, Name, CC_Document_Name__c, CC_Document_Status__c, CC_Date_Received__c,CC_Date_Requested__c, CC_Due_Date__c FROM Application_Checklist__c WHERE Application_CC__c = :AppID];    
    }
    
    //system.debug('-----ChecklistID')
    // Upload Code
    public Id itemId {get; set;}
    public Application_Checklist__c item {get; set;}
    public SObject obj {get; set;} 
    public Attachment file {
        get {
            system.debug('-----Item ID 1 ----- ' + itemId);
            if(file == null) {
                file = new Attachment();
            }
            return file;
        } 
        set;
    }
    private Boolean isOld {get; set;}

    public PageReference uploadFile() {
        PageReference pr = null;
        try {
            system.debug('-----ChecklistID ----- '+ itemId);
            file.ParentId = itemId;
            insert file;
            
            if(isOld) {
                item.CC_Date_Received__c = Date.today();
                item.CC_Document_Status__c = 'Received';
                update item;
            } else {
               obj.put('CC_Date_Received__c', Date.today());
               obj.put('CC_Document_Status__c','Received');
               update obj;
            } 
          
            pr = Page.cc_App_Status;
        } catch (Exception ex) {
            cc_App_BaseController.addError(Label.Upload_Attachment_Error);
            System.debug(ex.getMessage());
        } finally {
            file = null;
        }
        return pr;
    }
}

and the Visualforce page is as follows:
 
<apex:page showHeader="false" sidebar="false" controller="cc_AppChecklist_List" standardStylesheets="false" html-lang="en-US"> 

<style>
    .content a {
        color: #FFF !important;
        margin-top: 4%;
        font-size: 12px;
     }
</style>   
   
   
    <c:cc_App_Header />
    <c:cc_App_TopNav activeSection="Personal" />

    <div class="gridlock gridlock-16 appPage">
        <div class="page page_container clearfix"> 
            <div class="row page_row">
                <div class="content page_content max-push-1 max-14 desktop-push-1 desktop-14 tablet-full mobile-full margined">
                    
                    <div class="row page_row content_block background">
                        <div class="desktop-12 tablet-6 mobile-6 min-full left" >
                            <h1 style="margin: 0;">My Application Checklist Items</h1>
                        </div>
                        
                        <apex:pageBlock Id="Block" title=""> 
                            <apex:pageBlockTable value="{!Records}" var="Record" style="width:100%"> 
                                <apex:column style="width:35%"> 
                                    <apex:facet name="header">Checklist ID</apex:facet> 
                                    <apex:outputText value="{!Record.Id}"/> 
                                </apex:column> 
                                <apex:column style="width:35%"> 
                                    <apex:facet name="header">Checklist Name</apex:facet> 
                                    <apex:outputText value="{!Record.CC_Document_Name__c}"/> 
                                </apex:column> 
                                <apex:column style="width:10%"> 
                                    <apex:facet name="header">Status</apex:facet> 
                                    <apex:outputText value="{!Record.CC_Document_Status__c}"/> 
                                </apex:column> 
                                <!-- apex:column > 
                                    <apex:facet name="header">Date Requested</apex:facet> 
                                    <apex:outputText value="{0, date, MMMM d','  yyyy}">
                                        <apex:param value="{!Record.CC_Date_Requested__c}" /> 
                                    </apex:outputText>
                                </apex:column> 
                                <apex:column > 
                                    <apex:facet name="header">Date Due</apex:facet> 
                                    <apex:outputText value="{0, date, MMMM d','  yyyy}">
                                        <apex:param value="{!Record.CC_Due_Date__c}" /> 
                                    </apex:outputText>
                                </apex:column --> 
                                <apex:column style="width:15%"> 
                                    <apex:facet name="header">Date Received</apex:facet> 
                                    <apex:outputText value="{0, date, MMMM d','  yyyy}">
                                        <apex:param value="{!Record.CC_Date_Received__c}" /> 
                                    </apex:outputText>
                                </apex:column>
                                <apex:column style="width:40%"> 
                                    <apex:facet name="header">Action</apex:facet> 
                                    <apex:form >
                                         <apex:inputFile title="file_upload" value="{!file.body}" fileName="{!file.name}" id="file_upload" onchange="return checkFileSize(this);" rendered="{!Record.CC_Document_Status__c =='Required'}" />
                                         <apex:commandLink styleClass="btn app-btn-success" value="Upload" action="{!uploadFile}" rendered="{!Record.CC_Document_Status__c =='Required'}" >
                                             <apex:param name="FiletoUpload" value="{!Record.Id}" assignTo="{!itemId}"/>
                                         </apex:commandLink>
                                    </apex:form> 
                               </apex:column> 
                               
                            </apex:pageBlockTable> 
                        </apex:pageBlock> 
                    </div>
                    

                </div>
            </div>
        </div>
    </div>
   
    <!-- <c:App_Footer /> -->
</apex:page>

The goal is that when a file is uploaded:
- Attach the file to the record (individual checklist item)
- Update status to "Received"
- Set a Date Received of date of upload


Any advice since the upload currently works, the issue is getting the set of fields to update.