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
mahesh p 12mahesh p 12 

How to attach uploaded documents to respective custom object record Id

Verification.cmp
<aura:component controller="VerificationFilesController" Implements="flexipage:availableForRecordHome,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes,force:lightningQuickAction, forceCommunity:availableForAllPageTypes" access="global" >
    
    <aura:method name="CustomerDetailsValidation" action="{!c.VerificationValidation}" access="public">
    	<aura:attribute name="isValidated" type="boolean" />
    </aura:method>
    
    <!--aura:attribute name="opportunity" type="Opportunity" default="{'sobjectType': 'Opportunity'}" access="private"/-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="force:refreshView" action="{!c.doInit}" />
    <aura:attribute name="recordError" type="String" access="private"/>
    <aura:attribute name="recordId" type="String"/> 
    <aura:attribute name="FileList" type="Object[]"/>
    <aura:attribute name="DocItemList" type="List" />
    <aura:attribute name="DocItemList1" type="List" />
    <aura:attribute name="documentType" type="String"/> 
    <aura:attribute name="isOpen" type="boolean" default="false"/>
    <aura:attribute name="showSpinner" type="boolean" default="false"/>
    <aura:attribute name="pictureSrc" type="String" 
                    default="https://s3-us-west-1.amazonaws.com/sfdc-demo/image-placeholder.png"/>
    <!--aura:attribute name="parentId" type="Id"/-->
    <aura:handler event="aura:systemError" action="{!c.showSystemError}"/>
    <aura:attribute name="stageName" type="String" default=""/>
    
    <div class="slds-box slds-theme--shade slds-scrollable_x slds-size_1-of-1"> 
        
        <div class="slds-p-bottom_small">
            <div class="slds-page-header ">
                <div class="slds-media">
                    <div class="slds-media__figure">
                        <lightning:icon iconName="standard:file" size="small" alternativeText="file uploaded"/>
                    </div>
                    <div class="slds-media__body">
                        <h1 class="slds-page-header__title" title="e-KYC">Documents Summary</h1>
                    </div>
                </div>
            </div>
        </div>
        <div>
            <!--img src="{!v.pictureSrc}"/>
            <button aura:id="viewImg" onclick="{!c.loadImage}" type="button">view Image</button-->
        </div>
        <table class="slds-table slds-table_bordered"> 
            <thead>
                <tr class="slds-text-heading--label">
                    <!--th scope="col"><div class="slds-truncate" title="Select Schemas">Choose Scheme</div></th-->
                    <th scope="col"><div class="slds-truncate" title="Document">Document</div></th>
                    <th scope="col"><div class="slds-truncate" title="Uploaded">Uploaded</div></th>
                    <th scope="col"><div class="slds-truncate" title="Approved">Approved</div></th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.DocItemList}" var="doc">
                    
                    <tr>
                        <td>
                            
                            <div class="slds-truncate slds-clearfix" name="Role" title="{!doc.docDesc}">
                                <aura:if isTrue="{!doc.mandatory}">
                                    <div class="slds-float_left">
                                        <p style="color:red;">*</p>
                                    </div>
                                </aura:if>
                                <div class="slds-float_none">
                                    <p>{!doc.docDesc}</p></div>
                            </div>
                            
                        </td>
                        <td class="slds-media__figure">
                            <aura:if isTrue="{!doc.uploaded}">
                                <div class="slds-media slds-media_center slds-has-flexi-truncate">
                                <lightning:icon iconName="action:approval" size="xx-small" />
                                </div>
                                <!--aura:set attribute="else">
                                <lightning:icon iconName="action:upload" size="xx-small" alternativeText="file Not uploaded yet"/>
                            </aura:set-->
                            </aura:if>
                        </td>
                        <td>
                            <!--aura:if isTrue="{!doc.reviewed}">
                                <div>
                                <aura:if isTrue="{!doc.approved}">
                                    <div class="slds-media slds-media_center slds-has-flexi-truncate">
                                    <lightning:icon iconName="action:approval" size="xx-small" />
                                    </div>
                                    <aura:set attribute="else">
                                        <div class="slds-media slds-media_center slds-has-flexi-truncate">
                                        <lightning:icon iconName="action:close" size="xx-small" alternativeText="Document Rejected"/>
                                        </div>
                                    </aura:set>
                                </aura:if>
                                    </div>
                            </aura:if-->
                            
                            <aura:if isTrue="{!and(equals(doc.approvedOrNot,'yes'),doc.reviewed)}">
                                    <div>
                                        <lightning:icon iconName="action:approval" size="xx-small" />
                                    </div>
                                </aura:if>
                                <aura:if isTrue="{!and(equals(doc.approvedOrNot,'no'),doc.reviewed)}">
                                    <div>
                                        <lightning:icon iconName="action:close" size="xx-small" alternativeText="Document Rejected"/>
                                    </div>
                                </aura:if>
                                <aura:if isTrue="{!and(equals(doc.approvedOrNot,'empty'),doc.reviewed)}">
                                    <div>
                                    </div>
                                </aura:if>
                        </td>
                    </tr>
                </aura:iteration>
                
                <!--aura:iteration items="{!v.DocItemList1}" var="doc1">
                     <tr>
                     <td>
      {!doc1.pankaj2016__Document_Name__c}                     	    
                     </td>
                      <td>
                        
                      </td>
                      <td>
                      </td>
                     </tr>
                </aura:iteration-->
            </tbody>
        </table>
            <div class="slds-col slds-size_1-of-1 slds-large-size_1-of-2 slds-p-around_x-small">
                  <lightning:input aura:id="uploadedDocument" type="file" label="Upload Document for Verification" name="file" 
                             onchange="{!c.openModal}"></lightning:input>
            </div>
            <div class="slds-col slds-size_1-of-1 slds-large-size_1-of-2 slds-p-around_x-small">
            	<c:CameraComponent myRecordId="{!v.recordId}" />
            </div>
    </div>
    <aura:if isTrue="{!v.isOpen}">
        <!--###### MODAL BOX Start From Here ######--> 
        <div role="dialog" tabindex="-1" aria-labelledby="header99" class="slds-modal slds-fade-in-open ">
            <div class="slds-modal__container">
                <!-- ###### MODAL BOX HEADER Part Start From Here ######-->
                <div class="slds-modal__header">
                    <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeModel}">
                        X
                        <span class="slds-assistive-text">Close</span>
                    </button>
                    <h2 id="header99" class="slds-text-heading--medium">Select Document Type</h2>
                </div>
                <!--###### MODAL BOX BODY Part Start From Here ######-->
                <div class="slds-modal__content slds-p-around--medium  slds-is-relative">
                    <aura:if isTrue="{!v.showSpinner}">
                        <div class="demo-only" style="height:6rem;">
                            <div role="status" class="slds-spinner slds-spinner_medium slds-spinner_brand">
                                <span class="slds-assistive-text">Loading</span>
                                <div class="slds-spinner__dot-a"></div>
                                <div class="slds-spinner__dot-b"></div>
                            </div>
                        </div>
                        <aura:set attribute="else">
                            <div class="slds-form-element__control">
                                <ui:inputSelect class="single" aura:id="selectDocType" change="{!c.handleFilesChange}">
                                    <ui:inputSelectOption text="Choose one.." value=""/>
                                    <aura:iteration items="{!v.DocItemList[0].docTypeList}" var="docType">
                                        <ui:inputSelectOption text="{!docType}" label="{!docType}"/>
                                    </aura:iteration>
                                </ui:inputSelect>
                            </div>
                        </aura:set>
                    </aura:if>
                    
                    <div class="slds-form-element">
                        
                    </div>
                </div>
                <!--###### MODAL BOX FOOTER Part Start From Here ######-->
                <div class="slds-modal__footer">
                    <button class="slds-button slds-button--neutral" onclick="{!c.closeModal}" >Cancel</button>
                </div>
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>
        <!--###### MODAL BOX Part END Here ######-->
    </aura:if>
</aura:component>
VerificationFilesController.apxc
public class VerificationFilesController {
    
    Public attachment objAttachment{get; set;}
    static List<String> docTypes = new List<String> { 'ID Proof', 'Bank Statements', 'Nationality Proof', 'Driver License' };
        // Public Verification_Documents__c objVerificationFiles{get; set;}
        Public VerificationFilesController(apexpages.standardcontroller stdCon){
            objAttachment = new Attachment();
            // objVerificationFiles = new Verification_Documents__c();
        }
    Public static List<String> getdocumentTypes(String oppId){
                List<String> fileList = new List<String>();
        
        System.debug('opp id for record type is ====  '+oppId);
        //List<Opportunity> opplist=[select RecordType.Name from Opportunity where Id=:oppId];
        //string recTypeId=opplist.get(0).RecordType.Name;
        Boolean flag=false;
            for(TypeWiseDocument__c documentDesc: [Select Document_Name__c 
                                                               from TypeWiseDocument__c ]){
                                                                   fileList.add(documentDesc.Document_Name__c);
                                                               }
        return fileList;
    }
    
    @AuraEnabled
    Public static List<DocumentItem> getVerificationFiles1(String oppId){
        // List<String> fileList = getdocumentTypes(oppId);
        
        List<String> fileList = new List<String>();
        Map<String,Boolean> mapMandatory=new Map<String,Boolean>();
        System.debug('opp id for record type is ====  '+oppId);
        //List<Opportunity> opplist=[select RecordType.Name from Opportunity where Id=:oppId];
        //System.debug('opplist size ====  '+opplist.size());
        //string recTypeId=opplist.get(0).RecordType.Name;
        //System.debug('Opportunity Record Type ====  '+recTypeId);
        Boolean flag=false;
            for(TypeWiseDocument__c documentDesc: [Select Document_Name__c, Mandatory__c 
                                                               from TypeWiseDocument__c ]){
                                                                   fileList.add(documentDesc.Document_Name__c);
                                                                   mapMandatory.put(documentDesc.Document_Name__c,
                                                                                    documentDesc.Mandatory__c);
                                                               }
        
        List<DocumentItem> docItems = new List<DocumentItem>();
        Map<String,Verification_Documents__c> MapAttchmentByType=new 
            
            Map<String,Verification_Documents__c>();
        for(Verification_Documents__c attch:[select Id, Name, docType__c, 
                                             Reviewed__c, Approved__c,
                                             AttachmentId__c, Comment__c
                                             from Verification_Documents__c
                                             where Opportunity__r.Id = :oppId]){
                                                 MapAttchmentByType.put(attch.docType__c, attch);
                                             } 
        system.debug('MapAttchmentByType-->'+MapAttchmentByType);
        
        List<DocumentItem> manDocItems = new List<DocumentItem>();
        List<DocumentItem> nonManDocItems = new List<DocumentItem>();
        
        for(String docuType:fileList){
            DocumentItem doc = new DocumentItem();
           /*
            if(mapMandatory.get(docuType)==true){
                doc.mandatory = true;
            } else {
                doc.mandatory = false;
            }
*/
            if(MapAttchmentByType.get(docuType)!=null){
                doc.uploaded = true;
                Verification_Documents__c existingAttchment = MapAttchmentByType.get
                    
                    (docuType);
                doc.docName = existingAttchment.Name;
                doc.reviewed = existingAttchment.Reviewed__c;
                doc.attId = existingAttchment.AttachmentId__c;
                doc.comment = existingAttchment.Comment__c;
                doc.approved = existingAttchment.Approved__c;
                System.debug('existingAttchment.Approved__c --> ' + existingAttchment.Approved__c);
                if(existingAttchment.Approved__c)
                {
  
                    doc.approvedOrNot = 'yes';
                } else {
                    doc.approvedOrNot = 'no';
                } 
                doc.docDesc = docuType;
                doc.docTypeList = fileList;
            }else{
                doc.approvedOrNot = 'empty';
                doc.uploaded = false;
                doc.docDesc = docuType;
                doc.reviewed = false;
                doc.docTypeList = fileList;
            }
            if(mapMandatory.get(docuType)==true){
                doc.mandatory = true;
                manDocItems.add(doc);
            } else {
                doc.mandatory = false;
                nonManDocItems.add(doc);
            }
            // docItems.add(doc);
        }
        
        docItems.addAll(manDocItems);
        docItems.addAll(nonManDocItems);
        
        return docItems ;
    }
    @AuraEnabled
    Public static List<DocumentItem> getVerificationFiles(String oppId){

        List<DocumentItem> docItems = new List<DocumentItem>();
        Map<String,Verification_Documents__c> MapAttchmentByType=new 
            
            Map<String,Verification_Documents__c>();
        for(Verification_Documents__c attch:[select Id, Name, docType__c, 
                                             Reviewed__c, Approved__c,
                                             AttachmentId__c, Comment__c
                                             from Verification_Documents__c
                                             where Opportunity__r.Id = :oppId]){
                                                 MapAttchmentByType.put(attch.docType__c, attch);
                                             } 
        system.debug('MapAttchmentByType-->'+MapAttchmentByType);
        
        for(String docuType:docTypes){
            DocumentItem doc = new DocumentItem();
            if(MapAttchmentByType.get(docuType)!=null){
                doc.uploaded = true;
                Verification_Documents__c existingAttchment = MapAttchmentByType.get
                    
                    (docuType);
                doc.docName = existingAttchment.Name;
                doc.reviewed = existingAttchment.Reviewed__c;
                doc.attId = existingAttchment.AttachmentId__c;
                doc.comment = existingAttchment.Comment__c;
                doc.approved = existingAttchment.Approved__c;
                doc.docDesc = docuType;
            }else{
                doc.uploaded = false;
                doc.docDesc = docuType;
                doc.reviewed = false;
            }
            docItems.add(doc);
        }
        return docItems;
    }
    
    @AuraEnabled
    public static Id saveTheFile(Id parentId, String fileName, String description, 
                                 String base64Data, String contentType) { 
                                     //insert & upsert logic
                                     System.debug('##########################Into FILE SAVING****************');
                                     System.debug(parentId+'   '+fileName);
                                     System.debug(description+'   '+contentType);
                                     long t1 = DateTime.Now().getTime();
                                     
                                     List<String> fileList = getdocumentTypes(parentId);
                                     System.debug('fileList >> '+fileList);
                                     boolean reset = false;
                                     Map<String,Verification_Documents__c> MapAttchmentByType=new 
                                         
                                         Map<String,Verification_Documents__c>();
                                     for(Verification_Documents__c attch:[select Id, Name, Reviewed__c,
                                                                          docType__c
                                                                          from Verification_Documents__c
                                                                          where Opportunity__r.Id = :parentId]){
                                                                              MapAttchmentByType.put(attch.docType__c, attch);
                                                                          }
                                     system.debug('MapAttchmentByType >> '+MapAttchmentByType);
                                     Verification_Documents__c associatedVd = new Verification_Documents__c();
                                     boolean deleteAtt = false;
                                     for(String docuType:fileList){
                                         
                                         system.debug('MapAttchmentByType docuType '+MapAttchmentByType.get(docuType));
                                         system.debug('description '+description);
                                         
                                          if(MapAttchmentByType.get(docuType)!=null && MapAttchmentByType.get
                                            
                                            (docuType).docType__c == description){
                                                system.debug('Attachment Id--> '+MapAttchmentByType.get(docuType).Id);
                                                associatedVd = MapAttchmentByType.get(docuType);
                                                Attachment existingAttchment = [SELECT Id from Attachment WHERE ParentId 
                                                                                
                                                                                = :associatedVd.Id];
                                                system.debug('existingAttchment Id '+existingAttchment.Id);
                                                system.debug('associatedVd Id '+associatedVd.Id);
                                                associatedVd.Reviewed__c = false;
                                                associatedVd.Approved__c = false;
                                                delete existingAttchment;
                                                deleteAtt = true;
                                                break;
                                            }
                                         

                                         /*
                                         if(MapAttchmentByType.get(docuType)!=null && MapAttchmentByType.get
                                            
                                            (docuType).docType__c == description){
                                                system.debug('Attachment Id--> '+MapAttchmentByType.get(docuType).Id);
                                                associatedVd = MapAttchmentByType.get(docuType);
                                                Attachment existingAttchment = [SELECT Id from Attachment WHERE ParentId 
                                                                                
                                                                                = :associatedVd.Id];
                                                system.debug('existingAttchment Id '+existingAttchment.Id);
                                                system.debug('associatedVd Id '+associatedVd.Id);
                                                associatedVd.Reviewed__c = false;
                                                associatedVd.Approved__c = false;
                                                delete existingAttchment;
                                                break;
                                            }else{
                                                associatedVd = new Verification_Documents__c();
                                                associatedVd.Reviewed__c = false;
                                                associatedVd.Name = fileName;
                                                associatedVd.docType__c = description;
                                                associatedVd.Opportunity__c = parentId;
                                                insert associatedVd; 
                                                break;
                                            }*/
                                     }
                                     
                                     if(!deleteAtt){
                                         associatedVd = new Verification_Documents__c();
                                         associatedVd.Reviewed__c = false;
                                         associatedVd.Name = fileName;
                                         associatedVd.docType__c = description;
                                         associatedVd.Opportunity__c = parentId;
                                         associatedVd.Document_Master__c = 'a016C0000017veSQAQ';
                                         insert associatedVd; 
                                         system.debug('associatedVd '+associatedVd.Id);
                                     }
                                     
                                     associatedVd.Name = fileName;     
                                     System.debug(DateTime.Now().getTime()-t1);
                                     long t2 = DateTime.Now().getTime();
                                     base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
                                     Attachment a = new Attachment();
                                     a.parentId = associatedVd.Id;
                                     a.Body = EncodingUtil.base64Decode(base64Data);
                                     a.Name = fileName;
                                     a.ContentType = contentType;
                                     a.Description = description;                 
                                     insert a;
                                     associatedVd.AttachmentId__c = a.Id;
                                     update associatedVd;
                                     system.debug('AttachmentId: '+a.Id);
                                     System.debug(DateTime.Now().getTime()-t2);
                                     
                                     for(Verification_Documents__c attch:[select Id, Name, Approved__c, Reviewed__c
                                                                          from Verification_Documents__c
                                                                          where Opportunity__r.Id = :parentId]){
                                                                              if(!attch.Approved__c && attch.Reviewed__c){
                                                                                  reset = true;
                                                                                  break;
                                                                              }   
                                                                          }
                                     
                                     if(!reset){
                                         Opportunity oppRec = new Opportunity();
                                         oppRec.Id = parentId;
                                         oppRec.Stage_Reset__c=false;
                                         update oppRec;
                                     }
                                     
                                     return a.Id;
                                 }
    
    @AuraEnabled
    public static void approveTheDoc(String approval, Id attachId, String comment, String oppId) { 
        //insert & upsert logic
        Boolean approvalVal;
        if(approval=='approve'){
            approvalVal = true;
        } else if(approval == 'reject') {
            approvalVal = false;
        }
        Attachment att = [select Id, ParentId from Attachment where Id = :attachId];
        Verification_Documents__c associatedVd = [select Id from 
                                                  Verification_Documents__c where Id = :att.ParentId];
        associatedVd.Reviewed__c = true;
        associatedVd.Approved__c = approvalVal;
        if(comment!=null || comment!=''){
            associatedVd.Comment__c = comment;
        }
        update associatedVd;
        
        //code to reset missing flag for blocking the missing popup
        Opportunity oppRec = new Opportunity();
            oppRec.Id = oppId;
        oppRec.Missing_Mandatory_Documents__c = false;
        update oppRec;
    }
    public class DocumentItem{
        @AuraEnabled public String docId {set;get;}
        @AuraEnabled public Boolean uploaded {set;get;}
        @AuraEnabled public Boolean reviewed {set;get;}
        @AuraEnabled public Boolean approved {set;get;}
        @AuraEnabled public String docDesc {set;get;}
        @AuraEnabled public String docName {set;get;}
        @AuraEnabled public String attId {set;get;}
        @AuraEnabled public String comment {set;get;}
        @AuraEnabled public Boolean mandatory {set;get;}
        @AuraEnabled public String approvedOrNot {set;get;}
        @AuraEnabled public List<String> docTypeList {set;get;}
    }
    
}
I have two custom objects Document master whose api name is TypeWiseDocument__c and verification documents  api name is Verification_Documents__c.These two custom objects have master-detail relationship.In my apex class i am hardcoding id to which the documents are getting attached under document master of verification documents.How can i attach respective uploaded documents to respective document master ids of verification documents related list