Can anyone please help me in this?
My initial Use Case Scenario was to display a list of records in a lightning component, and providing the ability for users to update the same records. This was a simple implementation by using an aura attribute with type as list of sobject records.
But Later the ask changed to show the same records in a categorized manner, say all records of Type A must appear first, followed by all records of Type B, much like a summarized report where we create a grouping based on some field.
To achieve that, I had to create a wrapper class in my controller, and instead of using List of sObject, I had to use List of Wrapper class in my Lightning component to display the records accordingly.
The display part was over. I achieved it. Yay!
But the issue that came up was editing the records. The records were no longer updating.
For updation, On the click of Save button, I pushed the List of Wrapper back to the controller for updation. But, there is some unknown Exception firing up each time I click on Save button for updation of records in the Apex Class.
When I debug the List returned by the component from the controller, it appears to be in a suspicious format.
Can anyone please help me in an approach for updating the records. My code is as below :
Lightning Component Code :
//DocList1 is the attribute for type List of wrapper class and used to display the records.
<aura:attribute name="DocList1" type="DocumentListController.DocumentListWrapper[]" description="store wrapper class records list"/>
// Js Controller : Save is the method that runs on Save button click
({
initRecords: function(component, event, helper) {
// call the apex class method and fetch sub stage of loan appliction record
alert('Modification in progress');
var action = component.get("c.fetchSubStage");
action.setParams({
'loanId': component.get("v.recordId")
});
action.setCallback(this, function(response) {
var state = response.getState();
//alert(state);
if (state === "SUCCESS") {
var capturedResponse = response.getReturnValue();
console.log('Response received'+capturedResponse);
console.log(JSON.stringify(capturedResponse));
// set SubStage attribute with return value from server.
component.set("v.subStage", capturedResponse);
}
});
$A.enqueueAction(action);
// call the apex class method and fetch account list
var action = component.get("c.fetchDocuments");
action.setParams({
'loanId': component.get("v.recordId")
});
action.setCallback(this, function(response) {
var state = response.getState();
//alert(state);
if (state === "SUCCESS") {
var storeResponse = response.getReturnValue();
console.log('List received'+storeResponse);
//component.set("v.DocList", storeResponse);
console.log(JSON.stringify(storeResponse));
// set AccountList list with return value from server.
component.set("v.DocList1", storeResponse);
}
});
$A.enqueueAction(action);
},
Save : function(component, event, helper) {
//console.log('DocList'+component.get("v.DocList1")[0].lstDocumentCheckList[0].Id);
var action = component.get("c.saveLoan");
action.setParams({
"listOfDoc": component.get("v.DocList1")
});
action.setCallback(this, function(response) {
//alert('12121');
if (response.getState() === "SUCCESS" && component.isValid()) {
component.set("v.DocList1", response.getReturnValue());
component.set("v.showSaveCancelBtn",false);
}
//window.location.reload(true);
});
$A.enqueueAction(action);
}
})
And finally the controller class goes as follows : //saveLoan is the method for updating the records, but the Unknown Exception is thrown in Salesforce, Right after the System.debug statement for displaying the list.
public class DocumentListController {
// method for fetching the Sub Stage of the Loan Application record.
@AuraEnabled
public static String fetchSubStage(Id loanId) {
String subStage;
List<Loan_Application__c> lstLoanApplication;
if(String.isNotBlank(loanId)) {
lstLoanApplication = [SELECT ID, Sub_Stage__c FROM Loan_Application__c WHERE ID = :loanId LIMIT 1];
if(!lstLoanApplication.isEmpty()) {
subStage = lstLoanApplication[0].Sub_Stage__c;
}
}
System.debug('Debug Log for subStage'+subStage);
return subStage;
}
// method for fetch account records list
@AuraEnabled
public static List <DocumentListWrapper> fetchDocuments(Id loanId) {
//Map of Contact ID to Contact Name
Map<String, List<Document_Checklist__c>> mapContactNametoDocChecklist = new Map<String, List<Document_Checklist__c>>();
List < Document_Checklist__c > returnList = new List < Document_Checklist__c > ();
List < Document_Checklist__c > lstOfDoc;
List<DocumentListWrapper> lstDocumentWrapper = new List<DocumentListWrapper>();
List<Loan_Application__c> lstLoanApplication = [SELECT ID, Sub_Stage__c FROM Loan_Application__c WHERE ID = :loanId LIMIT 1];
String subStage;
String fieldsToQuery;
if(!lstLoanApplication.isEmpty()) {
subStage = lstLoanApplication[0].Sub_Stage__c;
}
System.debug('Debug Log for subStage'+subStage);
if(subStage == 'Application Initiation') {
//fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Original_Seen_and_Verified__c';
fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Original_Seen_and_Verified__c';
}
else if(subStage == 'File Check') {
fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, File_Check_Completed__c';
}
else if(subStage == 'COPS:Data Maker') {
fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Document_Collection_Mode__c, Screened__c, Sampled__c';
}
else if(subStage == 'COPS:Data Checker') {
fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Document_Collection_Mode__c, Screened__c, Sampled__c';
}
else if(subStage == 'Scan: Data Maker') {
//fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Scan_Check_Completed__c, Document_Collection_Mode__c, Screened__c, Sampled__c';
fieldsToQuery = 'Id, Contact_Name__c, Status__c, Scan_Check_Completed__c, Document_Collection_Mode__c, Screened__c, Sampled__c';
}
else if(subStage == 'Scan: Data Checker') {
fieldsToQuery = 'Id, Contact_Name__c, Document_Type__r.name, Document_Master__c, Document_Master__r.Name, Status__c, Scan_Check_Completed__c, Document_Collection_Mode__c, Screened__c, Sampled__c';
}
System.debug('Debug Log for fieldsToQuery'+fieldsToQuery);
if(String.isNotBlank(fieldsToQuery)) {
String query = 'SELECT '+fieldsToQuery+' FROM Document_Checklist__c where Loan_Applications__c =\''+loanId+'\' ORDER BY Contact_Name__c NULLS FIRST';
System.debug('Debug Log for query'+query);
lstOfDoc = Database.query(query);
}
else {
lstOfDoc = [select id,Contact_Name__c,Loan_Contact__r.name,Document_Type__r.name,Document_Master__c,Document_Master__r.name,Loan_Engine_Mandatory__c,Express_Queue_Mandatory__c,Status__c,Original_Seen_and_Verified__c,File_Check_Completed__c,Scan_Check_Completed__c from Document_Checklist__c where Loan_Applications__c =:loanId];
}
//lstOfDoc = [select id,Contact_Name__c,Loan_Contact__r.name,Document_Type__r.name,Document_Master__c,Document_Master__r.name,Loan_Engine_Mandatory__c,Express_Queue_Mandatory__c,Status__c,Original_Seen_and_Verified__c,File_Check_Completed__c,Scan_Check_Completed__c from Document_Checklist__c where Loan_Applications__c =:loanId];
if(!lstOfDoc.isEmpty()) {
for (Document_Checklist__c doc: lstOfDoc) {
returnList.add(doc);
/*System.debug('Debug Log for doc.Document_Type__r.name'+doc.Document_Type__r.name);
System.debug('Debug Log for Document_Master__c'+doc.Document_Master__r.Name);
System.debug('Debug Log for Document_Master__c'+doc.Contact_Name__c);*/
if(String.isNotBlank(doc.Contact_Name__c)) {
if(!mapContactNametoDocChecklist.containsKey(doc.Contact_Name__c)) {
mapContactNametoDocChecklist.put(doc.Contact_Name__c,new List<Document_Checklist__c>());
}
mapContactNametoDocChecklist.get(doc.Contact_Name__c).add(doc);
}
else {
if(!mapContactNametoDocChecklist.containsKey('Not Specified')) {
mapContactNametoDocChecklist.put('Not Specified',new List<Document_Checklist__c>());
}
mapContactNametoDocChecklist.get('Not Specified').add(doc);
}
}
for(String strContactName : mapContactNametoDocChecklist.keySet()) {
lstDocumentWrapper.add(new DocumentListWrapper(strContactName, mapContactNametoDocChecklist.get(strContactName)));
}
}
System.debug('Debug Log for lstDocumentWrapper'+lstDocumentWrapper);
//For Testing Purpose
for(DocumentListWrapper objDocumentListWrapper : lstDocumentWrapper) {
System.debug('Key:'+objDocumentListWrapper.strContactName);
System.debug('Value:'+objDocumentListWrapper.lstDocumentCheckList);
}
//return returnList;
return lstDocumentWrapper;
}
// method for fetch picklist values dynamic
@AuraEnabled
public static List < String > getselectOptions(sObject objObject, string fld) {
system.debug('objObject --->' + objObject);
system.debug('fld --->' + fld);
List < String > allOpts = new list < String > ();
// Get the object type of the SObject.
Schema.sObjectType objType = objObject.getSObjectType();
// Describe the SObject using its object type.
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
// Get a map of fields for the SObject
map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
// Get the list of picklist values for this field.
list < Schema.PicklistEntry > values =
fieldMap.get(fld).getDescribe().getPickListValues();
// Add these values to the selectoption list.
for (Schema.PicklistEntry a: values) {
allOpts.add(a.getValue());
}
system.debug('allOpts ---->' + allOpts);
allOpts.sort();
return allOpts;
}
@AuraEnabled
public static list<Attachment> getAttachment(Id parentId)
{
return [SELECT ParentId, Name, Id, ContentType, BodyLength FROM Attachment where ParentId =: parentId];
}
@AuraEnabled
public static List <DocumentListWrapper> saveLoan(List <DocumentListWrapper> listOfDoc) {
System.debug('list-->'+listOfDoc.size());
System.debug('Debug Log for param list'+listOfDoc);
List<Document_Checklist__c> lstDocchkLst = new List<Document_Checklist__c>();
if(!listOfDoc.isEmpty()) {
for(DocumentListWrapper objDocumentListWrapper : listOfDoc) {
//lstDocchkLst.addAll(objDocumentListWrapper.lstDocumentCheckList);
system.debug(objDocumentListWrapper.lstDocumentCheckList);
}
}
System.debug('Debug Log for lstDocchkLst'+lstDocchkLst);
if(!lstDocchkLst.isEmpty()) {
Database.Saveresult[] updateList = Database.update(lstDocchkLst,false);
for (Database.SaveResult sr : updateList) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully updated Document Checklist. Record ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Fields that affected this error: ' + err.getFields());
}
}
}
}
return listOfDoc;
//return null;
}
@AuraEnabled
public static List<ID> queryAttachments (Id propertyId) {
List<ContentDocumentLink> links;
List<ContentVersion> lstContentVersion;
Set<Id> setContentVersionId = new Set<Id>();
String objectAPIName = '';
String keyPrefix = '';
System.debug('Debug Log for propertyId'+propertyId);
keyPrefix = String.valueOf(propertyId).substring(0,3);
for( Schema.SObjectType obj : Schema.getGlobalDescribe().Values() ){
String prefix = obj.getDescribe().getKeyPrefix();
if(prefix == keyPrefix){
objectAPIName = obj.getDescribe().getName();
break;
}
}
System.debug('Debug Log for objectAPIName:'+objectAPIName);
if(String.isNotBlank(objectAPIName)) {
if(objectAPIName == 'Document_Checklist__c') {
links = [select id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId=:propertyId];
System.debug('Debug Log for links'+links.size());
System.debug('Debug Log for links'+links);
if (links.isEmpty()) {
return null;
}
Set<Id> contentIds = new Set<Id>();
for (ContentDocumentLink link :links) {
contentIds.add(link.ContentDocumentId);
}
System.debug('Debug Log for contentIds'+contentIds);
/*
lstContentVersion = [SELECT Id, Title FROM ContentVersion WHERE ContentDocumentId IN :contentIds AND IsLatest=true];
System.debug('Debug Log for lstContentVersion'+lstContentVersion);
if(!lstContentVersion.isEmpty()) {
for(ContentVersion objContentVersion : lstContentVersion) {
setContentVersionId.add(objContentVersion.Id);
}
}
System.debug('Debug Log for setContentVersionId'+setContentVersionId);
*/
List<Id> lstsettoList = new List<Id>(contentIds);
return lstsettoList;
}
else if(objectAPIName == 'Loan_Application__c') {
List<Document_Checklist__c> lstDocumentList = [SELECT Id FROM Document_Checklist__c WHERE Loan_Applications__c =: propertyId];
Set<Id> setDocumentListIds = new Set<Id>();
System.debug('Debug Log for lstDocumentList'+lstDocumentList.size());
if(!lstDocumentList.isEmpty()) {
for(Document_Checklist__c objDC : lstDocumentList) {
setDocumentListIds.add(objDC.Id);
}
}
System.debug('Debug Log for setDocumentListIds'+setDocumentListIds.size());
if(!setDocumentListIds.isEmpty()) {
links = [select id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId IN:setDocumentListIds];
if (links.isEmpty()) {
return null;
}
Set<Id> contentIds = new Set<Id>();
for (ContentDocumentLink link :links) {
contentIds.add(link.ContentDocumentId);
}
System.debug('Debug Log for contentIds'+contentIds);
List<Id> lstsettoList1 = new List<Id>(contentIds);
return lstsettoList1;
//return [SELECT Id, Title FROM ContentVersion WHERE ContentDocumentId IN :contentIds AND IsLatest=true];
}
}
}
return new List<Id>();
}
public class DocumentListWrapper {
@AuraEnabled public String strContactName;
@AuraEnabled public List<Document_Checklist__c> lstDocumentCheckList;
public DocumentListWrapper(String paramContactName, List<Document_Checklist__c> paramListDocChkLst) {
this.strContactName = paramContactName;
this.lstDocumentCheckList = paramListDocChkLst;
}
}
}
Screenshot of Exception :
The actual text of the debugged list is as follows (saveLoan method in Apex Class) :
(
{
lstDocumentCheckList=[{Id=a0Cp0000005z40BEAQ, Status__c=Uploaded, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40CEAQ, Status__c=Uploaded, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40DEAQ, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40EEAQ, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40FEAQ, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}],
strContactName=Not Specified
},
{
lstDocumentCheckList=[{Id=a0Cp0000005z43VEAQ, Contact_Name__c=Anil Suresh, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z43ZEAQ, Contact_Name__c=Anil Suresh, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z43YEAQ, Contact_Name__c=Anil Suresh, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z43XEAQ, Contact_Name__c=Anil Suresh, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z43WEAQ, Contact_Name__c=Anil Suresh, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}],
strContactName=Anil Suresh
},
{
lstDocumentCheckList=[{Id=a0Cp0000005z40GEAQ, Contact_Name__c=Turkish Pandey, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40HEAQ, Contact_Name__c=Turkish Pandey, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40IEAQ, Contact_Name__c=Turkish Pandey, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}, {Id=a0Cp0000005z40JEAQ, Contact_Name__c=Turkish Pandey, Status__c=Received, Scan_Check_Completed__c=true, Screened__c=false, Sampled__c=false}],
strContactName=Turkish Pandey}
)