I need a big help with my LWC Component, I'm using Dynamic SOQL but I need to show the Name of lookup field and even using __r it shows Id, here is my codes and the view :
Apex:
@AuraEnabled (cacheable=true)
public static String getRelatedFieldsAndRecords(String negotiationId){
Map<String, String> returnMap = new Map<String,String>();
//get fields from FieldSet
SObject sObj = (SObject)(Type.forName('Schema.'+ 'CLM_Representative__c').newInstance());
List<Schema.FieldSetMember> lstFSMember = sObj.getSObjectType().getDescribe().fieldSets.getMap().get('CLM_Representative').getFields();
//prepare SOQL query based on fieldAPIs
String query = 'SELECT ';
for(Schema.FieldSetMember f : lstFSMember) {
if(f.getFieldPath() == 'CLM_Account__c' || f.getFieldPath() == 'CLM_Contact__c') {
String str = f.getFieldPath().Substring(0,f.getFieldPath().length()-1);
query += str + 'r.Name, ';
}
else
{
query += f.getFieldPath() + ', ';
}
}
query += 'Id FROM ' + 'CLM_Representative__c' ;
query += ' WHERE CLM_Negotiation__c = ' + '\'' + negotiationId + '\'';
//execute query
List<CLM_Representative__c > lstRecords = Database.query(query);
String JSONStringRecords = JSON.serialize(lstRecords);
//prepare a map which will hold fieldList and recordList and return it
returnMap.put('FIELD_LIST', JSON.serialize(lstFSMember));
returnMap.put('RECORD_LIST', JSONStringRecords);
return JSON.serialize(returnMap);
}
JS:
connectedCallback(){
getFieldsAndRecords({ strObjectApiName: 'Contact',
strfieldSetName: 'CLM_Legal_Representative',
negotiationId: this.recordId})
.then(result=>{
let objStr = JSON.parse(result);
let listOfFields = JSON.parse(Object.values(objStr)[1]);
let listOfRecords = JSON.parse(Object.values(objStr)[0]);
let items = [];
listOfFields.map(element=>{
items = [...items ,{label: element.label, fieldName: element.fieldPath}];
});
this.columns = items;
this.records = listOfRecords;
this.totalRecords = listOfRecords.length;
this.pageSize = this.pageSizeOptions[0];
this.paginationHelper();
})
.catch(error =>{
console.log('error',error);
this.records = undefined;
})
getRelatedFieldsAndRecords({negotiationId: this.recordId})
.then(result=>{
let objSelectedStr = JSON.parse(result);
let listOfSelectedFields = JSON.parse(Object.values(objSelectedStr)[1]);
let listOfSelectedRecords = JSON.parse(Object.values(objSelectedStr)[0]);
let items = [];
listOfSelectedFields.map(element=>{
items = [...items ,{label: element.label, fieldName: element.fieldPath}];
});
this.selectedColumns = items;
this.selectedRecords = listOfSelectedRecords;
})
.catch(error =>{
console.log('error',error);
this.selectedRecords = undefined;
})
}
LWC Component: