• venkatesh j 23
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
i am inserting single record with attch file in lightning component.but i am inserting multiple files in single record.please let me know.
please find my attachment let me know.
public class FileuploadForContact {
    
   @AuraEnabled
    public static Id saveChunk(Contact Con, String fileName, String base64Data, String contentType, String fileId) {
       
        insert Con;
        
        fileId = saveTheFile(Con.id, fileName, base64Data, contentType);
        return Id.valueOf(fileId);
    }
 
    public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
        base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
 
        Attachment oAttachment = new Attachment();
        oAttachment.parentId = parentId;
 
        oAttachment.Body = EncodingUtil.base64Decode(base64Data);
        oAttachment.Name = fileName;
        oAttachment.ContentType = contentType;
 
        insert oAttachment;
 
        return oAttachment.Id;
    }
 
   
}
Creating record with attach file in single lightning component.the record
  and acttach file are should be with the same id.how can achive this please let me know.
my code is;;
Component::
<aura:component controller="AttchfileData" implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
     <aura:attribute name="Attach" type="Custom_Attchment__c" 
                        default="{'sobjectType': 'Custom_Attchment__c'}"/>

    Name: <force:inputField value="{!v.Attach.Name}"/><br/><br/>
   Type: <force:inputField value="{!v.Attach.Type__c}"/><br/><br/>
    Description: <force:inputField  value="{!v.Attach.Description__c}"/><br/>
            <lightning:button variant="base" label="Save" onclick="{! c.handleClickk }"/>

         
   <!-- <lightning:fileUpload label="Upload File" 
                          multiple="true" accept=".pdf, .png ,  .docx" 
                          recordId="{!v.recordId}" 
                           disabled="false"
                          aura:id="multifileUpload" 
                          onuploadfinished="{!c.handleUploadFinished}" />-->
       <!--<aura:attribute name="edgeRecordId" type="String" description="Record to which the files should be attached" />
   <div class="wrapper">
        <lightning:fileUpload label="Upload Attachments" 
        multiple="true" 
        accept=".pdf, .png"
        recordId="{!v.edgeRecordId}" 
        onuploadfinished="{!c.handleAfterUpload}" />
    </div>-->
      <aura:attribute name="accept" type="List" default="['.pdf', '.png']"/>
    <aura:attribute name="multiple" type="Boolean" default="true"/>
    <aura:attribute name="disabled" type="Boolean" default="false"/>

    <lightning:fileUpload  name="fileUploader"
                           label= "Demo Upload"
                           multiple="{!v.multiple}"
                           accept="{!v.accept}"
                           disabled="{!v.disabled}"
                           recordId="abcd"
                           onuploadfinished="{! c.handleUploadFinished }"/>
         
</aura:component>

Controller js::
({
  handleClickk : function(component, event) {
    var newAcc = component.get("v.Attach");
    var action = component.get("c.Attinfo");
    action.setParams({ 
        "CustAt": newAcc
    });
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+name);
            }
        });
    $A.enqueueAction(action)
},

  
    handleUploadFinished: function (cmp, event) {
        // This will contain the List of File uploaded data and status
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);
    }
})

Apex controller;;;
public class AttchfileData {

  @AuraEnabled    
  /* public static void Attinfo(Custom_Attchment__c CustAt , String fileName, String base64Data, 
                               String contentType){
        //CustAt=New Custom_Attchment__c();
                         insert CustAt;
     Attachment    att = new Attachment();  
        att.parentId = CustAt.id;
        att.Body = EncodingUtil.base64Decode(base64Data);
        att.Name = fileName;
        att.ContentType = contentType;
        Insert att;
        
        
       
    }  */
 public static void Attinfo(Custom_Attchment__c CustAt,String fileName, String base64Data, String contentType ){
        //CustAt=New Custom_Attchment__c();
                         insert CustAt;
     string aa='this is siraj';
     
     Attachment    att = new Attachment();  
        att.parentId = CustAt.id;
        att.Body = Blob.valueOf(aa);
        att.Name = 'hi';
        att.ContentType = 'pdf';
        Insert att;

        
      
    
    
       
    }

     }
Creating record with attach file in single lightning component.the record
  and acttach file are should be with the same id.how can achive this please let me know.
my code is;;
Component::
<aura:component controller="AttchfileData" implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
     <aura:attribute name="Attach" type="Custom_Attchment__c" 
                        default="{'sobjectType': 'Custom_Attchment__c'}"/>

    Name: <force:inputField value="{!v.Attach.Name}"/><br/><br/>
   Type: <force:inputField value="{!v.Attach.Type__c}"/><br/><br/>
    Description: <force:inputField  value="{!v.Attach.Description__c}"/><br/>
            <lightning:button variant="base" label="Save" onclick="{! c.handleClickk }"/>

         
   <!-- <lightning:fileUpload label="Upload File" 
                          multiple="true" accept=".pdf, .png ,  .docx" 
                          recordId="{!v.recordId}" 
                           disabled="false"
                          aura:id="multifileUpload" 
                          onuploadfinished="{!c.handleUploadFinished}" />-->
       <!--<aura:attribute name="edgeRecordId" type="String" description="Record to which the files should be attached" />
   <div class="wrapper">
        <lightning:fileUpload label="Upload Attachments" 
        multiple="true" 
        accept=".pdf, .png"
        recordId="{!v.edgeRecordId}" 
        onuploadfinished="{!c.handleAfterUpload}" />
    </div>-->
      <aura:attribute name="accept" type="List" default="['.pdf', '.png']"/>
    <aura:attribute name="multiple" type="Boolean" default="true"/>
    <aura:attribute name="disabled" type="Boolean" default="false"/>

    <lightning:fileUpload  name="fileUploader"
                           label= "Demo Upload"
                           multiple="{!v.multiple}"
                           accept="{!v.accept}"
                           disabled="{!v.disabled}"
                           recordId="abcd"
                           onuploadfinished="{! c.handleUploadFinished }"/>
         
</aura:component>

Controller js::
({
  handleClickk : function(component, event) {
    var newAcc = component.get("v.Attach");
    var action = component.get("c.Attinfo");
    action.setParams({ 
        "CustAt": newAcc
    });
    action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+name);
            }
        });
    $A.enqueueAction(action)
},

  
    handleUploadFinished: function (cmp, event) {
        // This will contain the List of File uploaded data and status
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);
    }
})

Apex controller;;;
public class AttchfileData {

  @AuraEnabled    
  /* public static void Attinfo(Custom_Attchment__c CustAt , String fileName, String base64Data, 
                               String contentType){
        //CustAt=New Custom_Attchment__c();
                         insert CustAt;
     Attachment    att = new Attachment();  
        att.parentId = CustAt.id;
        att.Body = EncodingUtil.base64Decode(base64Data);
        att.Name = fileName;
        att.ContentType = contentType;
        Insert att;
        
        
       
    }  */
 public static void Attinfo(Custom_Attchment__c CustAt,String fileName, String base64Data, String contentType ){
        //CustAt=New Custom_Attchment__c();
                         insert CustAt;
     string aa='this is siraj';
     
     Attachment    att = new Attachment();  
        att.parentId = CustAt.id;
        att.Body = Blob.valueOf(aa);
        att.Name = 'hi';
        att.ContentType = 'pdf';
        Insert att;

        
      
    
    
       
    }

     }