I need to escape comma in Column values while Creating CSV file in LWC. I tried with replacing comma with Keywords but still when converting string into blob it splits value with comma. I'm sending JSON String from LWC which I need to convert in CSV.
.cls
@AuraEnabled
public static string saveTemplate(String jsonBody){
try {
string message = 'Subcontract Task Template Updated Successfully!';
List<Object> dataList = (List<Object>)JSON.deserializeUntyped(jsonBody);
List<String> csvRowValues = new List<String>();
for(Object obj : dataList){
Map<String,Object> mpStrObj = (Map<string,Object>)obj;
String category = String.valueOf(mpStrObj.get('Category')) != null ? String.valueOf(mpStrObj.get('Category')) : '';
String taskName = String.valueOf(mpStrObj.get('TaskName')) != null ? String.valueOf(mpStrObj.get('TaskName')) : '';
String assignedTo = String.valueOf(mpStrObj.get('AssignedTo')) != null ? String.valueOf(mpStrObj.get('AssignedTo')) : '';
String priority = String.valueOf(mpStrObj.get('Priority')) != null ? String.valueOf(mpStrObj.get('Priority')) : '';
String status = String.valueOf(mpStrObj.get('Status')) != null ? String.valueOf(mpStrObj.get('Status')) : '';
String dueDate = String.valueOf(mpStrObj.get('DueDate')) != null ? String.valueOf(mpStrObj.get('DueDate')) : '';
String startDate = String.valueOf(mpStrObj.get('StartDate')) != null ? String.valueOf(mpStrObj.get('StartDate')) : '';
String endDate = String.valueOf(mpStrObj.get('EndDate')) != null ? String.valueOf(mpStrObj.get('EndDate')) : '';
String description = String.valueOf(mpStrObj.get('Description')) != null ? String.valueOf(mpStrObj.get('Description')) : '';
String notes = String.valueOf(mpStrObj.get('Notes')) != null ? String.valueOf(mpStrObj.get('Notes')) : '';
String rowStr = category + ',' + taskName + ',' + assignedTo + ',' + priority + ',' + status + ',' + dueDate + ',' + startDate + ',' + endDate + ',' + description.escapeCsv() + ',' +notes.escapeCsv();
system.debug('Rowstr : '+rowStr);
String temp = handleCommas(rowStr);
csvRowValues.add(temp);
}
String csvColumnHeader = 'Category,Task Name,Assigned To,Priority,Status,Due Date,Start Date,End Date,Description,Notes\n';
String csvFile = csvColumnHeader + String.join(csvRowValues,'\n');
csvFile = csvFile.replaceAll(':quotes:', '').replaceAll(':comma:', ',');
Blob body = blob.valueOf(csvFile);
Document docId = [SELECT Id FROM Document WHERE Name = 'Subcontract Task Template'];
document doc=new document();
doc.Body= body;
doc.Id = docId.Id;
update doc;
return message;
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
public static String handleCommas(String csvLine){
String prevLine = csvLine;
Integer startIndex;
Integer endIndex;
while(csvLine.indexOf('"') > -1){
if(startIndex == null){
startIndex = csvLine.indexOf('"');
csvLine = csvLine.substring(0, startIndex) + ':quotes:' + csvLine.substring(startIndex+1, csvLine.length());
}else{
if(endIndex == null){
endIndex = csvLine.indexOf('"');
csvLine = csvLine.substring(0, endIndex) + ':quotes:' + csvLine.substring(endIndex+1, csvLine.length());
}
}
if(startIndex != null && endIndex != null){
String sub = csvLine.substring(startIndex, endIndex);
sub = sub.replaceAll(',', ':comma:');
csvLine = csvLine.substring(0, startIndex) + sub + csvLine.substring(endIndex, csvLine.length());
startIndex = null;
endIndex = null;
}
}
return csvLine;
}