0

I am using the wire service to query several fields and assign to a list. I am trying to create a PDF using jsPDF and I need to create another list that only contains the fields that I will display in the PDF. So in the code below, data has about 20 fields. I only want to show 5 of those fields in my pdf. I assume that the list that I use to create a table using jsPDF can only have the fields I want in the PDF. How can I copy "data" to only include the 5 fields I want to display in the PDF rather than the 20 fields that exist in the variable data

groupstructures(result) {

  //if we have data returned
if (result.data && result.data.length) {
            //assign the data to the data variable - used as the source for the main datatable
          this.data = result.data;

Thanks!!!

2 Answers 2

1

Definitely not the slickest solution, but the desired values can be dynamically configured. If you know fields that you want to have in the pdf file its better to use @sfdcfox solution

    const fieldsToReturn = ['Id', 'Name'];

    const minimizedRecords = result.data.map(item => {
        return Object.keys(item).reduce( (previousValue, currentValue) => {
            if (fieldsToReturn.includes(currentValue)) {
                previousValue[currentValue] = item[currentValue];
            }
            return previousValue;
        }, {});
    })
1

You use the Array.prototype.map function, with some simple destructuring for legibility.

this.data = result.data.map((datum) => {
  let {a, b, c} = datum;
  return {a, b, c};
});

This will output a copy of the original array with a copy of the desired elements in that array.

It may not be necessary for jsPDF to do this anyways, but I'm not familiar enough with its capabilities to offer an alternative other than directly answering your question.

3
  • Looking at the Array.prototype.map it looks like I can modify the data. I'm trying to only copy certain fields. As an example, If data has id, name and groupnumber.. I only want the copy to have id and name. Is that possible?
    – Frank
    Apr 27, 2021 at 19:12
  • 1
    @Frank You're not modifying, you're copying. The original data is left as is, you can store it in a different variable, etc. Given my example, above: this.data = result.data.map((datum) => { let {id, name} = datum; return {id, name}; });
    – sfdcfox
    Apr 27, 2021 at 19:16
  • The issue I am now having is that when one of the fields has a null value, I get an error, " [getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings.] " It seems that the wire service does not return that field for a row when the value is null. thanks!
    – Frank
    May 3, 2021 at 16:04

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