import { LightningElement, api, wire, track } from 'lwc';
import retrieveContacts from '@salesforce/apex/downloadContcatApexClass.retrieveContactMethod';
export default class DownloadContacts extends LightningElement {
@api recordId;
@track contacts;
@track error;
@api invoke(){
retrieveContacts({ recordIdAccount: this.recordId })
.then((result) => {
console.log('line 12:' + result);
this.contacts = result;
this.error = undefined;
})
.catch((error) => {
this.error = error;
this.contacts = undefined;
});
var columnEnd = ',';
var lineEnd = '\n';
// Get the CSV header from the list.
var keys = new Set();
this.contacts.forEach(function (record) {
Object.keys(record).forEach(function (key) {
keys.add(key);
});
});
keys = Array.from(keys);
var csvString = '';
csvString += keys.join(columnEnd);
csvString += lineEnd;
for (var i = 0; i < this.contacts.length; i++) {
var counter = 0;
for (var sTempkey in keys) {
var skey = keys[sTempkey];
// add , after every value except the first.
if (counter > 0) {
csvString += columnEnd;
}
// If the column is undefined, leave it as blank in the CSV file.
var value = this.contacts[i][skey] === undefined ? '' : this.contacts[i][skey];
csvString += '"' + value + '"';
counter++;
}
csvString += lineEnd;
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_self';
//hiddenElement.download = component.get("v.fileName");
document.body.appendChild(hiddenElement); //Required for FireFox browser
hiddenElement.click(); // using click() js function to download csv file
}
}
}
I have to create lwc quick action to convert related contacts in to csv download. i had created lwc quick action button and retrieved the data needed.there is problem in converting to csv. I have tried everything .below is my code. thanks in advance for any help.please help.
ERROR :-Cannot read properties of undefined (reading 'forEach')
#LWC Components #LWC #LWC Component #Lightning Web Components #Lightning #LWC Development #Custom LWC #LWC Javascript Files
import { LightningElement, api, wire, track } from 'lwc';
import retrieveContacts from '@salesforce/apex/downloadContcatApexClass.retrieveContactMethod';
export default class DownloadContacts extends LightningElement {
@api recordId;
@track contacts;
@track error;
@api invoke(){
retrieveContacts({ recordIdAccount: this.recordId })
.then((result) => {
console.log('line 12:' + result);
this.contacts = result;
this.error = undefined;
var columnEnd = ',';
var lineEnd = '\n';
// Get the CSV header from the list.
var keys = new Set();
this.contacts.forEach(function (record) {
Object.keys(record).forEach(function (key) {
keys.add(key);
});
});
keys = Array.from(keys);
var csvString = '';
csvString += keys.join(columnEnd);
csvString += lineEnd;
for (var i = 0; i < this.contacts.length; i++) {
var counter = 0;
for (var sTempkey in keys) {
var skey = keys[sTempkey];
// add , after every value except the first.
if (counter > 0) {
csvString += columnEnd;
}
// If the column is undefined, leave it as blank in the CSV file.
var value = this.contacts[i][skey] === undefined ? '' : this.contacts[i][skey];
csvString += '"' + value + '"';
counter++;
}
csvString += lineEnd;
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_self';
//hiddenElement.download = component.get("v.fileName");
document.body.appendChild(hiddenElement); //Required for FireFox browser
hiddenElement.click(); // using click() js function to download csv file
}
})
.catch((error) => {
this.error = error;
this.contacts = undefined;
});
}
}