0

I am facing the problem that I cannot run the code because there are no triggers to run it. I need the pdf file to be previewed in the module window. Tried through conectedCallback and wire, it didn't work. Here is my code.

export default class ProposalPreviewPDF extends LightningElement {
@api parameter1;//taking from parent html
@api parameter2;


@wire (generateProposal, {proposalId: '$parameter1'})
    proposals(result){
        if (result) {
            this.parameter2 = '/sfc/servlet.shepherd/document/download/' + result;
            ///sfc/servlet.shepherd/document/download/' 
        }     
}}//paremeter2 I am transferring to htlm file

/------------------------------------------------------------------/

public with sharing class proposalPreviewPDFController {

@AuraEnabled(cacheable=false)
public static String generateProposal(Id proposalId)
{
    System.debug('here' + proposalId);
    Blob body;
    PageReference pageRef = new PageReference('/apex/TemplateForProposal');  
    pageRef.getParameters().put('proposalId', proposalId );// - параметр будет передан в контроллер VF страницы 

    
    
    if(!Test.isRunningTest())
    {
        body = pageRef.getContentAsPDF();
    }
    else
    {
        body = blob.valueOf('Test');

    }
    System.debug(body);
    ContentVersion cont = new ContentVersion();
    cont.Title = 'Title for this contentVersion';
    cont.PathOnClient = 'file.pdf';
    cont.VersionData = body;
    cont.Origin = 'H';

    insert cont; 
    System.debug(cont); 

    Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cont.Id].ContentDocumentId;

    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = conDocId;
    cdl.LinkedEntityId = proposalId;
    cdl.ShareType = 'V';
    insert cdl; 
    System.debug(cdl.Id);

    return cdl.ContentDocumentId;
}

}

Сan someone help?

1

1 Answer 1

0

You need to be addressing the data portion of the response. Additionally, since you are creating records in your APEX call, you cannot use @wired (why you are getting the DML Limit error of 1 record). So what we needed to do is create a getter and setter for the @api decorator... and call the apex method in the setter.

import { LightningElement, api } from "lwc";
import generateProposal from @salesforce/apex/proposalPreviewPDFController.generateProposal;

export default class ProposalPreviewPDF extends LightningElement{
    _parameter1;  //hold current value to see if you should recall apex
    parameter2;
    error; //you should handle the error state. 

    @api 
    get parameter1() {
        return this._parameter1;
    }
    set parameter1(value) {
        if(this._parameter1 !== value) {
            this._parameter1 = value;
            generateProposal({ proposalId: value })
                .then((result) => {
                    this.parameter2 = '/sfc/servlet.shepherd/document/download/' + result;
                    this.error = undefined;
                })
                .catch((error) => {
                    this.parameter2 = undefined;
                    this.error = error;
                }) 
        }
    } 
}

Looking at the apex method, it needs to be decorated with @AuraEnabled, but not @AuraEnabled(cacheable = true) //don't use cacheable = true if you are updating records, or you will get a DML Limit Error

public with sharing class proposalPreviewPDFController {

@AuraEnabled
public static String generateProposal(Id proposalId)
{
//code
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – David Reed
    Nov 10, 2021 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .