如何解决如何在Dynamic Lightning:Datatable中修复URL字段,以使字段标签显示每个相关记录的名称?
我正在研究一个闪电组件,该组件将显示自定义,动态的“相关”记录列表,这些记录可用于在对象的列表中显示相关记录,该对象也是同一父级的子级。因此,例如,如果“帐户”是父记录,而“自定义对象1”和“自定义对象2”都是该帐户的子级,我想在“自定义对象1”上显示与同一帐户相关联的“自定义对象1”记录。自定义对象2闪电记录页面。我在LC画布和字段集中使用用户输入来分别定位记录和显示列。
我创建的组件的工作原理是可以查询并动态显示记录,列和字段值。我还想使用“名称”字段包含指向自定义对象1记录的可点击链接。但是,我似乎无法动态设置Name字段的typeAttribute以显示记录的标签。因为我将其分配为列标题的属性,所以它仅采用数组中的最后一个值。
是否可以将typeAttribute分配给记录响应,而不是列标题响应?
这是我的代码:
顶点类:
公共类DynamicRelatedListController {
@AuraEnabled
public static String fetchParent(String recId,String parentLookup,String objPageName) {
String strSOQL = 'SELECT Id,' + parentLookup + ' FROM ' + objPageName + ' WHERE Id = \'' + recId + '\' LIMIT 1 ' ;
List <SObject> currentRecord = new List <SObject>(Database.query(strSOQL));
system.debug('Database Result for fetch parent ' + strSOQL);
List <String> parentIds = new List<String>();
for(SObject cr : currentRecord) {
parentIds.add(String.valueOf(cr.get(parentLookup)));
}
String parentId = parentIds[0];
return parentId;
}
@AuraEnabled
public static List < SObject > fetchChildren(String objectName,String criteria,String parentFieldAPIName,String recId,String objPageName) {
String parentId = fetchParent(recId,parentLookup,objPageName);
// String prefix ='%' + GM_Util.findobjectNameFromrecordIdPrefix(parentId) + '%';
List < SObject > childRecordList = new List < SObject > ();
String strSOQL = 'SELECT Id,' + parentFieldAPIName + ' FROM ' + objectName + ' WHERE ' + parentFieldAPIName + ' = \'' + parentId + '\'';
if ( String.isNotBlank( criteria ) )
strSOQL += ' ' + criteria;
childRecordList = Database.query(strSOQL);
system.debug('Database Result for fetch children ' + strSOQL);
return childRecordList;
}
@AuraEnabled
public static DataTableDetails fetchRelatedRecs(String recId,String fieldSetName,String objectName,String objPageName,String parentLookup)
{
DataTableDetails dataTableDtls = new DataTableDetails();
List<GM_Util_Aura.FieldSetMemberWrapperClass> listofFieldSetMembers = new List<GM_Util_Aura.FieldSetMemberWrapperClass>();
if(fieldSetName != NULL && fieldSetName != ''){
listofFieldSetMembers = GM_Util_Aura.getFieldSetMemberjson(objectName,fieldSetName);
}
// List<GM_Util_Aura.FieldSetMemberWrapperClass> listofFieldSetMembers = GM_Util_Aura.getFieldSetMemberjson(objectName,fieldSetName);
List<String> WHERE_IN_LIST = new List <String>();
for (SObject crl: fetchChildren(objectName,criteria,parentFieldAPIName,recId,objPageName)) {
WHERE_IN_LIST.add(crl.Id);
}
String sql = 'SELECT Id ';
if(listofFieldSetMembers != null && listofFieldSetMembers.size() > 0){
for(GM_Util_Aura.FieldSetMemberWrapperClass fsmwc : listofFieldSetMembers){
dataTableDtls.fieldLabelsList.add(fsmwc);
sql = sql + ',' + fsmwc.fieldName;
}
}
sql += ' FROM ' + objectName +' WHERE Id IN : WHERE_IN_LIST' ;
System.debug('final sql statement (WHERE_IN_LIST): ' + sql);
dataTableDtls.returnedParentRecords = Database.query(sql);
System.debug('returned Parent Records are ' + Database.Query(sql));
// filteredResults = Database.query(sql);
System.debug('final filtered results are: ' + dataTableDtls);
System.debug('returned field labels are ' + listofFieldSetMembers);
return dataTableDtls;
}
public class DataTableDetails{
@AuraEnabled
public List<sObject> returnedParentRecords = new List<sObject>();
@AuraEnabled
public List<GM_Util_Aura.FieldSetMemberWrapperClass> fieldLabelsList = new List<GM_Util_Aura.FieldSetMemberWrapperClass>();
}
}
我的组件帮助程序和cmp代码:
({
getDataTable: function(component,event) {
console.log("entered Get Data Table");
var action = component.get("c.fetchRelatedRecs");
action.setParams({
recId: component.get("v.recordId"),objectName: component.get("v.ObjectAPIName"),fieldSetName: component.get("v.FieldSetAPIName"),objPageName: component.get("v.sobjectName"),parentFieldAPIName: component.get("v.ParentFieldAPIName"),parentLookup: component.get("v.ParentFieldLookupName"),criteria: component.get("v.criteria")
});
action.setCallback(this,function(response){
//console.log("this is the fieldSetName " + fieldSetName)
var state = response.getState();
if(state === 'SUCCESS'){
var fieldSetResponse = response.getReturnValue().fieldLabelsList;
var parentRecordsResponse = response.getReturnValue().returnedParentRecords;
console.log("Parent records length " + parentRecordsResponse.length);
console.log("Field Set Length " + fieldSetResponse.length);
console.log("here are the field labels before data transformation " + JSON.stringify(fieldSetResponse));
console.log("Here are all the related records before transformation " + JSON.stringify(parentRecordsResponse));
//this for loop changes all lookup fields to type 'url' for clickable links
for(var i = 0; i < parentRecordsResponse.length; i++ ) {
if(fieldSetResponse[i].fieldName.includes('__r.Name')) {
fieldSetResponse[i].type ='url';
//drop the .Name from the lookup field so that the lightning:datatable can find the field value in the parentRecordsResponse object
fieldSetResponse[i].fieldName = fieldSetResponse[i].fieldName.replace('.Name','') ;
//this for loop locates the key in the parentRecordsResponse object that matches the lookup field in the fieldSetResponse object.
for(var j = 0; j < parentRecordsResponse.length; j++) {
var arraykeys = Object.keys(parentRecordsResponse[j]);
var arrayvalues = Object.values(parentRecordsResponse[j]);
console.log("Array Keys in iteration "+ arraykeys[j]);
console.log("Array Values " + JSON.stringify(arrayvalues));
//this for loop locates the key in the parentRecordsResponse object that matches the lookup field in the fieldSetResponse object.
for(var h = 0; h <arraykeys.length; h++) {
console.log("Array Keys in iteration h "+ arraykeys[h]);
//fieldSetResponse[i]['typeAttributes'] = {label: arrayvalues[h].Name };
if(fieldSetResponse[i].fieldName === arraykeys[h]){
//assign the'type attributes' to the url field with the Name value of the lookup field
fieldSetResponse[i]['typeAttributes'] = {label: arrayvalues[h]['Name'] };
//transform the nested lookup field object to /recordId for the url
parentRecordsResponse[j][arraykeys[h]] = "/" + arrayvalues[h].Id ;
}
}
}
}
}
console.log("here are the field labels after data transformation " + JSON.stringify(fieldSetResponse));
console.log("Here are all the related records " + JSON.stringify(parentRecordsResponse));
//send the responses back to the controller and set them using the 'v.' keys
component.set("v.columnsHeader",fieldSetResponse);
component.set("v.listofRelatedRecords",parentRecordsResponse);
}
else if (state === 'ERROR'){
console.log('::::::::::::: ERROR :::::::::::::');
}
});
$A.enqueueAction(action);
}
})
<aura:component controller="DynamicRelatedListController" implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningquickaction,force:hasSObjectName" access="global">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="ObjectAPIName" type="String"/>
<aura:attribute name="FieldSetAPIName" type="String"/>
<aura:attribute name="ParentFieldAPIName" type="String"/>
<aura:attribute name="ParentFieldLookupName" type="String"/>
<aura:attribute name="Criteria" type="String"/>
<aura:attribute name="TableName" type="String"/>
<!-- <aura:attribute name="recordId" type="String" /> -->
<aura:attribute name="columnsHeader" type="List"/>
<aura:attribute name="listofRelatedRecords" type="Object"/>
<lightning:card aura:id="lightCard" class="slds-card_boundary" title="{!v.TableName}" iconName="standard:file">
<div style="overflow-x: auto;">
<lightning:datatable data="{!v.listofRelatedRecords}"
columns="{!v.columnsHeader}"
keyField="Id"
hideCheckBoxColumn="true"/>
</div>
</lightning:card>
</aura:component>
这就是最终结果的样子-请注意,除了“名称”字段的标签之外,所有值都不同。链接本身可以正常工作,并导航到适当的记录:
解决方法
我知道了。在基本级别上,此行缺少'fieldName'属性,该属性使用字段名称的API并应用值。
因此,这是:fieldSetResponse [i] ['typeAttributes'] = {label:arrayvalues [h] ['Name']}; 应该是这样的:fieldSetResponse [i] ['typeAttributes'] = {label:fieldName:{具有显示值的字段的API名称}};
即使如此,这也是不可能动态完成的,因为在这种情况下,当fieldName是查找字段本身时,它将显示记录ID。我可以传递一个硬编码的API名称,但是显然这会达到目的。
要解决此问题,我添加了一个通用标签(单击以查看记录),并在该目标对象上创建了一个公式字段,该公式字段拉入父记录名称并将其添加到我的字段集中。
这是一个已知问题:https://trailblazer.salesforce.com/ideaView?id=0873A000000lLXYQA2 这可以通过创建一个展平函数来解决(尽管我还没有尝试实现):https://developer.salesforce.com/forums/?id=9062I000000XtwnQAC
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。