0

I had createed the component to upload the files. When i am using the component in salesforce it is uploading the files into the contentversion but when i am using these component in production after uploading the files files are visiible in files and attachment component but it is not uploading the files in contentversion. I had used SOQL query to fetch the file from contentversion by filtering record based on firstpublishedlocationid but is retrieving zero result

Below is the component code

<aura:component controller = "UploadeFilesForAuditRequestController" implements='force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction'
                        access='global' >
     <aura:attribute name="recordId" type="Id" />

<lightning:fileUpload label='Upload Multiple files'
                               multiple='true'
                              accept='.pdf, .png'
                              recordId='{!v.recordId}'
                              aura:id='multipleUpload'
                             onuploadfinished='{!c.handleUploadFinished}' />

</aura:component>

Below is the Controller code

({
    handleUploadFinished: function (component, event) {
        // Get the list of uploaded files
        // Get the list of uploaded files
        var uploadedFiles = event.getParam("files");
        // 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 uploaded successfully!"
        });
        toastEvent.fire();



        var action = component.get("c.createPdf");


        action.setParams({
            parentId : component.get("v.recordId") 
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {

                alert("From server: " + response.getReturnValue());

            }
            else
            {
                alert("response unsuccessfull");
            }
        });



        var dismissActionPanel = $A.get("e.force:closeQuickAction");
        dismissActionPanel.fire();








        $A.enqueueAction(action);
        $A.get("e.force:refreshView").fire();
    }






})

Below is the Apex Class code

public class UploadeFilesForAuditRequestController {
    @AuraEnabled
    public static String createPdf(Id parentId)
    {

        System.debug('Parent id' +parentId);
             RecordType deskRecordId =  [select Id ,Name from RecordType where Name =: 'Desk Audit eFile'];
             RecordType assetRecordId =  [select Id ,Name from RecordType where Name =: 'Asset Management Audit'];

     List <Audit_Project__c> auditFields = [select Total_number_of_invalid_efiles_uploaded__c, Request_Type__c, Total_number_of_valid_efiles__c, Total_number_of_efiles_uploaded__c ,   PropertyCase__c, Property_Staff_Name__c, Id from  Audit_Project__c where Id =: parentId   ];
     List<ContentDocument> listToDelete = new List<ContentDocument>();

     if(HelperClass.firstRun)
     {
          List <ContentVersion> contentVersions = [select Id, LastModifiedById, PathonClient, VERSIONDATA , Title, ContentDocumentId from ContentVersion where FIRSTPUBLISHLOCATIONID =:parentId ];

         if(contentVersions != null)
         {

             for(Audit_Project__c auditProcess : auditFields )
             {

             for(ContentVersion cv : contentVersions)
             {
                 if(!cv.Title.contains('Error'))
                 {

                 String fileName;

                 fileName = cv.PathOnClient;
                 String[] fileTitle = fileName.split('');System.debug('Content version ------' + cv.PathOnClient);  



                 if(fileName.contains('_')){
                     Integer lastIndex = fileName.lastIndexOf('_');

               String householdLastName  = fileName.substring(0, lastIndex);
                     String unit = fileName.substring(lastIndex + 1,fileName.lastIndexOf('.'));

                     if(householdLastName != null && unit != null){
                         System.debug('householdlastname = '+householdLastName);
                         System.debug('Unit ='+unit);
                         Case obj = new Case(); 
                         obj.Effective_Date_Certification__c = System.today();
                         obj.Origin = 'Email';
                         obj.PropertyCase__c= auditProcess.PropertyCase__c;
                         obj.Income_Set_Aside__c = 0;

                         obj.Certified_Income__c = 0;
                         obj.Household__c = householdLastName ;
                         obj.Property_Staff_Name__c = auditProcess.Property_Staff_Name__c;
                         obj.Unit__c = unit; 
                         obj.Audit_Request_Name__c = auditProcess.Id;

                        obj.Status = 'Awaiting Assignment';
                         if(auditProcess.Request_Type__c == 'Asset Management Audit'){
                             obj.RecordTypeId = assetRecordId.Id;    
                             obj.Application_Type__c = 'Asset Management Audit';
                         } else if(auditProcess.Request_Type__c == 'Desk Audit eFile'){
                             obj.RecordTypeId = deskRecordId.Id;
                             obj.Application_Type__c = 'Desk Audit eFile';
                         }
                         System.debug('file contain household');
                         insert obj;
                        System.debug('object inserted');

                         ContentVersion cvInsert = new ContentVersion();

                         cvInsert.PathOnClient = cv.PathOnClient;

                         cvInsert.Title = cv.Title;
                         cvInsert.FirstPublishLocationId = obj.Id;
                         cvInsert.VersionData = cv.VersionData;
                                                  System.debug('Going to insert in content version');

                         insert cvInsert;
                         System.debug('pdf created');
                         auditProcess.Total_number_of_valid_efiles__c =    auditProcess.Total_number_of_valid_efiles__c + 1; 
                         listToDelete.add(new ContentDocument(
                             Id = cv.ContentDocumentId 
                         ));

                     }
                     else{


                         String nameWithoutExt = fileName;
                         cv.Title =  'ERROR_' +  nameWithoutExt ;

                         update cv;
                         System.debug('Error file added');
                         auditProcess.Total_number_of_invalid_efiles_uploaded__c = auditProcess.Total_number_of_invalid_efiles_uploaded__c +1;


                     }
                 }else{


                     String nameWithoutExt = fileName;
                     cv.Title =  'ERROR_' +  nameWithoutExt ;

                     update cv;
                                              System.debug('Error file added');

                     auditProcess.Total_number_of_invalid_efiles_uploaded__c = auditProcess.Total_number_of_invalid_efiles_uploaded__c +1;


             }
                     }
             }
                   auditProcess.Total_number_of_efiles_uploaded__c = auditProcess.Total_number_of_valid_efiles__c + auditProcess.Total_number_of_invalid_efiles_uploaded__c;
 }
 delete listToDelete; 
         HelperClass.firstRun = false;              
         }
         }
               System.debug('Now going to return true');

     return 'true';


        }
}
0

Every file that exist in Salesforce have at least one content Version record. Now, You are doing query on Content Version as below:-

List <ContentVersion> contentVersions = [select Id, LastModifiedById, PathonClient, VERSIONDATA , Title, ContentDocumentId from ContentVersion where FIRSTPUBLISHLOCATIONID =:parentId ];

The above query does not returning any record because When users uploaded the file from file component, the FIRSTPUBLISHLOCATIONID is set to the user's id who is uploading the file but not to the recordId you have passed through the component markup.

You need to change your query. Instead of querying the ContentVersion, Query the contentDocumentLink which represents the link between a Salesforce CRM Content document or Salesforce file and where it's shared.

List <ContentDocumentLink> ListOfContentDocumentLink = [SELECT ContentDocument.title FROM ContentDocumentLink WHERE LinkedEntityId =:parentId ];

References:-

  1. ContentDocumentLink
  2. ContentVersion
| improve this answer | |
  • Logs were not generated in the production so now i can't find the problem. – Vijay Yadav Dec 24 '19 at 12:29
  • By changing the query i cant find the pathonclient – Vijay Yadav Dec 24 '19 at 12:29
  • ContentDocumentlink has a relationship with ContentVersion. ContentDocumentId is common in both the object.You can re-query the contentVersion once you have the ContentDocumentlink. – sanket kumar Dec 24 '19 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.