+ Start a Discussion
Raghavendra M 13Raghavendra M 13 

Am getting below error when am trying to rename my attachment name using standard lightning upload attachment code

User-added image
Apex Controller:
public with sharing class UploadimagesController {
@AuraEnabled
    public static void getfilenamesupdated(list<string> contdocumntidlst)
    {
            list<ContentDocument> condoclst = new list<ContentDocument>();
            condoclst = [select id,Title,FileType from ContentDocument where id in: contdocumntidlst];
            for(ContentDocument cd : condoclst)
    {
               cd.Title = cd.Title+'_'+'reqirdextension';   
    }
  update condoclst;
    }
}
Component:
<aura:component controller="UploadimagesController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
 <aura:attribute name="contentdocumentlist" type="List"/>
    <aura:attribute name="recordId" type="Id" description="Record to which the files should be attached" />
            <lightning:fileUpload label="Add attachment"           
                                  multiple="true"          
                                  accept=".jpg, .png"          
                                  recordId="{!v.recordId}"            
                                  onuploadfinished="{!c.handleUploadFinished}"/>
 
</aura:component>
Controller:
({
        handleUploadFinished: function (cmp, event) {
    // Get the list of uploaded files
    var uploadedFiles = event.getParam("files");
    var idslst = component.get("v.contentdocumentlist");
    for (var i= 0; i< uploadedFiles.length;i++)
   {
        
       idslst.push(JSON.stringify(uploadedFiles[i].documentId));
   }

   componenet.set("v.contentdocumentlist",idslst);
 
    var action = component.get("c.getfilenamesupdated");
            action.setParams({
                contdocumntidlst : component.get("v.selectedsobjct"),
               
            });
            action.setCallback(this, function(actionResult) 
            {
                var state = actionResult.getState();
                if (state === "SUCCESS") 
                {
                     // show success message – with no of files uploaded
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({
    "title": "Success!",
    "type" : "success",
    "message": uploadedFiles.length+ "files has been updated successfully!"
    });
    toastEvent.fire();
    
    $A.get("e.force:refreshView").fire();
    
    // Close the action panel
    var dismissActionPanel = $A.get("e.force:closeQuickAction");
    dismissActionPanel.fire();   
                }
            });
            $A.enqueueAction(actionResult);       
   
   
    },

})
Narender Singh(Nads)Narender Singh(Nads)
Hi Raghavendra,

In your JS controller, handleUploadFinished function is passing cmp and event i.e:  handleUploadFinished: function (cmp, event) 
But in your controller logic you are using component.

Change the parameter to component in  handleUploadFinished  function, like this:
 handleUploadFinished: function (component , event) 


Mark it as the best answer if it helps.
Thanks.
Raghavendra M 13Raghavendra M 13
Now am getting this error
Narender Singh(Nads)Narender Singh(Nads)
Use this as your JS controller:
 
Controller:
({
        handleUploadFinished: function (component, event) {
    // Get the list of uploaded files
    var uploadedFiles = event.getParam("files");
    var idslst = component.get("v.contentdocumentlist");
    for (var i= 0; i< uploadedFiles.length;i++)
   {
        
       idslst.push(JSON.stringify(uploadedFiles[i].documentId));
   }

   component.set("v.contentdocumentlist",idslst);
 
    var action = component.get("c.getfilenamesupdated");
            action.setParams({
                contdocumntidlst : component.get("v.selectedsobjct"),
               
            });
            action.setCallback(this, function(actionResult) 
            {
                var state = actionResult.getState();
                if (state === "SUCCESS") 
                {
                     // show success message – with no of files uploaded
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({
    "title": "Success!",
    "type" : "success",
    "message": uploadedFiles.length+ "files has been updated successfully!"
    });
    toastEvent.fire();
    
    $A.get("e.force:refreshView").fire();
    
    // Close the action panel
    var dismissActionPanel = $A.get("e.force:closeQuickAction");
    dismissActionPanel.fire();   
                }
            });
            $A.enqueueAction(actionResult);       
   
   
    },

})

 
Narender Singh(Nads)Narender Singh(Nads)
Please mark a best answer if it helps you so that others with similar issue can benefit from this post.

Thanks
Raghavendra M 13Raghavendra M 13
User-added image
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Use this:
Controller:
({
        handleUploadFinished: function (component, event) {
    // Get the list of uploaded files
    var uploadedFiles = event.getParam("files");
    var idslst = component.get("v.contentdocumentlist");
    for (var i= 0; i< uploadedFiles.length;i++)
   {
        
       idslst.push(JSON.stringify(uploadedFiles[i].documentId));
   }

   component.set("v.contentdocumentlist",idslst);
 
    var action = component.get("c.getfilenamesupdated");
            action.setParams({
                contdocumntidlst : component.get("v.selectedsobjct"),
               
            });
            action.setCallback(this, function(response) 
            {
                var state = response.getState();
                if (state === "SUCCESS") 
                {
                     // show success message – with no of files uploaded
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                    "title": "Success!",
                    "type" : "success",
                    "message": uploadedFiles.length+ "files has been updated successfully!"
                     });
                toastEvent.fire();
    
     $A.get("e.force:refreshView").fire();
    
    // Close the action panel
    var dismissActionPanel = $A.get("e.force:closeQuickAction");
    dismissActionPanel.fire();   
                }
            });
            $A.enqueueAction(action);       
   
   
    },

})

 
Raghavendra M 13Raghavendra M 13
That controlling throwing the above error
Narender Singh(Nads)Narender Singh(Nads)
Which error you are getting now?
Raghavendra M 13Raghavendra M 13
User-added image
Narender Singh(Nads)Narender Singh(Nads)
Did you saved your code? Because the code I sent has no variable called 'actionResult'.
Also while working with Lightning components, you have to refresh the component 2-3 times so that the component code gets refreshed.
Raghavendra M 13Raghavendra M 13
your controller is working but attachment name is not saving in a given format?