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
venkatesh j 23venkatesh j 23 

Creating a record with attach file in single lightning component.how it is possible please let me know.

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;

        
      
    
    
       
    }

     }
Khan AnasKhan Anas (Salesforce Developers) 
Hi Venkatesh,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/214616/how-to-make-attachment-mandatory-specified-attribute-values

http://www.sfdcmonkey.com/2017/09/25/file-upload-lightning-component/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
venkatesh j 23venkatesh j 23
Hi Khan Anas  ,
 i am inserting single file but how to upload multiple files.

This is my code .how to make changes multiple file uploads.
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;
    }
 
   
}