Skip to main content
deleted 2154 characters in body
Source Link
user111471
user111471

Code:


 @AuraEnabled
    public static void savePDF(id recordId){
       
        Opportunity opps = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
        SendInvoice__c invoice=new SendInvoice__c();
        invoice.ConName__c=[select Title from ContentVersion where Title=: opps.Invoice_Number__c].id;
        if (invoice.ConName__c != null) {  
            Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: fileName].ContentDocumentId;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob; 
            cv.contentDocumentId = contentDocumentId;
            INSERT cv;
          
        } 
        
        else {
          Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob;
            INSERT cv;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
            Id opportunId = [SELECT Id FROM Opportunity WHERE id =: recordId].Id;
            ContentDocumentLink cdl = new ContentDocumentLink();
            cdl.ContentDocumentId = contentDocumentId;
            cdl.LinkedEntityId = opportunId;
            cdl.ShareType = 'V';
            INSERT cdl;   
        }
    }

Code:


 @AuraEnabled
    public static void savePDF(id recordId){
       
        Opportunity opps = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
        SendInvoice__c invoice=new SendInvoice__c();
        invoice.ConName__c=[select Title from ContentVersion where Title=: opps.Invoice_Number__c].id;
        if (invoice.ConName__c != null) {  
            Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: fileName].ContentDocumentId;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob; 
            cv.contentDocumentId = contentDocumentId;
            INSERT cv;
          
        } 
        
        else {
          Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob;
            INSERT cv;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
            Id opportunId = [SELECT Id FROM Opportunity WHERE id =: recordId].Id;
            ContentDocumentLink cdl = new ContentDocumentLink();
            cdl.ContentDocumentId = contentDocumentId;
            cdl.LinkedEntityId = opportunId;
            cdl.ShareType = 'V';
            INSERT cdl;   
        }
    }
Source Link
user111471
user111471

help please with if condition

On the Opportunity object, I have a Quick action that generates a pdf file using a ContentDocument (the else block) and creates a new version of the ContentDocument (the if block). I need the following condition: if the file already exists in the system with this name, create a new version of it.

I think we need to use soql query, extract name from ContentDocument, if there is such name, that is query != null, create its new version with if block, else create pdf file with such name.

I couldn't get soql query into if logic, so I created custom object on which I created field, trying to extract title from ContentVersion where Title Opportunity.Invoice_Number__c. (The file name is stored in Invoice_Number__c&Opportunity)

My code creates a new version if there's already a file with this name, BUT if there's no file with this name, it won't create me a ContentDocument giving me an error

message: "List has no rows for assignment to SObject"

stackTrace: "Class.controllerTest.savePDF: line 11, column 1"

Line: invoice.ConName__c=[select Title from ContentVersion where Title=: opps.Invoice_Number__c].id;

What am I missing?

Code:

 @AuraEnabled
    public static void savePDF(id recordId){
       
        Opportunity opps = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
        SendInvoice__c invoice=new SendInvoice__c();
        invoice.ConName__c=[select Title from ContentVersion where Title=: opps.Invoice_Number__c].id;
        if (invoice.ConName__c != null) {  
            Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Title =: fileName].ContentDocumentId;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob; 
            cv.contentDocumentId = contentDocumentId;
            INSERT cv;
          
        } 
        
        else {
          Opportunity opp = [SELECT Invoice_Number__c FROM Opportunity WHERE Id = :recordId];
            PageReference pdfPage = new PageReference('/apex/newPageVf');
            pdfPage.getParameters().put('Id', recordId);
            Blob pdfBlob = pdfPage.getContentAsPDF();
            String fileName = opp.Invoice_Number__c;
            ContentVersion cv = new ContentVersion();
            cv.Title = fileName;
            cv.PathOnClient = fileName + '.pdf';
            cv.VersionData = pdfBlob;
            INSERT cv;
            Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
            Id opportunId = [SELECT Id FROM Opportunity WHERE id =: recordId].Id;
            ContentDocumentLink cdl = new ContentDocumentLink();
            cdl.ContentDocumentId = contentDocumentId;
            cdl.LinkedEntityId = opportunId;
            cdl.ShareType = 'V';
            INSERT cdl;   
        }
    }