3

We have a SOSL query that is searching multiple custom objects. The desired result is one search result pane that shows results from the multiple tables in one list. Because multiple custom objects are involved, I have had to create 'If' statements to inspect the results to ensure that the results are formatted properly. For the most part, the results are as desired by the solution architect.

enter image description here

However, there is a problem that I am having because the original architect has named some of the field names the same across multiple custom objects and they don't want them changed.

I will admit that I am very much a rookie when it comes to Salesforce Lightning custom objects (this is my first one) so I acknowledge the fact that there is probably a better way to do this. I would welcome input on how to fix my code to avoid the false duplicates through maybe more efficient SOSL Query and rendering code, or a way to stipulate which Custom object I am inspecting for specific fields. In our case, the 'OCS_Investigation' custom object has an 'Agent_First_name__c' and 'Agent_Last_Name__c' fields and the 'Participating_Agents__c' table also has the same field names. My suggestion to change the custom object field names to be unique has met with significant resistance because I am not the one responsible for the project.

Here is my SOSL Query:

public class OCS_Search_Controller {
    @AuraEnabled
    public static List<String> searchForIds(String searchText) {
        System.debug('The value of searchText is: '+ searchText);
    List<String> ids = new List<String>();                 //This code creates the holder for the found ids. 
    List<String> Creator = new List<String>();             
    //The following code will look for matches in the OCS_Investigation__c object for the search argument entered.     
    List<List<SObject>> results1 = [FIND :searchText IN ALL FIELDS  RETURNING OCS_Investigation__c(Name), Evidence__c(Name), Participating_Agents__c(Name), Property_Damage__c(Name), Subject__c(Name), Case_Chronology__c(Name), ContentVersion(title), ContentVersion(CreatedDate)];
    for (List<SObject> sobjs1 : results1) {
      for (SObject sobj1 : sobjs1) {
        ids.add(sobj1.Id);
      }
    }
        return ids;
  }
}

Here is the Code for the component that renders the results:

<aura:component implements="force:hasRecordId" access="global">
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>
    <force:recordData aura:id="recordLoader"
      recordId="{!v.recordId}"
      layoutType="FULL"
      targetRecord="{!v.record}"
      targetFields="{!v.simpleRecord}"
      targetError="{!v.recordError}"  />
    <!-- Display a header with details about the Investigation record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Type_of_Investigation__c))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Investigation Case id: {!v.simpleRecord.Name} <br/> Agent Name: {!v.simpleRecord.Agent_First_Name__c}_{!v.simpleRecord.Agent_Last_Name__c} <br/>  Case Title: {!v.simpleRecord.Case_Title__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>
    <!-- Display a header with details about the Chronology record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Activity__c))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Chronology Entry id: {!v.simpleRecord.Name} <br/> Activity Agent Name: {!v.simpleRecord.Activity_Agent_First_Name__c}_{!v.simpleRecord.Activity_Agent_Last_Name__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>
    <!-- Display a header with details about the Evidence record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Evidence_Record_Status__c))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Evidence entry id: {!v.simpleRecord.Name} <br/> Evidence Type: {!v.simpleRecord.Type_of_Evidence__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>    
    <!-- Display a header with details about the Participating Agent record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Participating_Agents__c.Name))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Participating Agent entry id: {!v.simpleRecord.Name} <br/> Agent Name: {!v.simpleRecord.Agent_First_Name__c}_{!v.simpleRecord.Agent_Last_Name__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>    
    <!-- Display a header with details about the Subject record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Type_of_Subject__c))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Subject entry id: {!v.simpleRecord.Name} <br/> Subject Type: {!v.simpleRecord.Type_of_Subject__c} <br/> Subject Name: {!v.simpleRecord.First_Name__c} _ {!v.simpleRecord.Last_Name__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>    
    <!-- Display a header with details about the Property Damage record -->
    <aura:if isTrue="{!not(empty(v.simpleRecord.Property_Description__c))}">
        <div class="slds-page-header" role="banner">
            <p class="slds-text-heading--label">Property Damage entry id: {!v.simpleRecord.Name} <br/> Control Form Provided: {!v.simpleRecord.CA_Board_of_Control_Form_Provided__c}</p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>    
    <!-- Display a header with details about the Attachment record -->
    <aura:if isTrue="{!(empty(v.simpleRecord.Name))}">
        <div class="slds-page-header" role="banner">
         <p class="slds-text-heading--label">Attachement Title:  {!v.simpleRecord.Title} <br/> Checksum: {!v.SimpleRecord.Checksum} <br/> ContentModifiedById: {!v.SimpleRecord.ContentModifiedById} <br/> ContentModifiedDate: {!v.SimpleRecord.ContentModifiedDate} <br/> Origin: {!v.SimpleRecord.Origin} <br/> ContentUrl: {!v.SimpleRecord.ContentUrl} <br/> Id: {!v.SimpleRecord.Id} <br/> CreatedById: {!v.SimpleRecord.CreatedById} <br/> CreatedByDate: {!v.SimpleRecord.CreatedByDate} <br/> Description: {!v.SimpleRecord.Description} <br/> ContentDocumentId: {!v.SimpleRecord.ContentDocumentId} <br/> ExternalDataSourceId: {!v.SimpleRecord.ExternalDataSourceId} <br/> ExternalDocumentInfo1: {!v.SimpleRecord.ExternalDocumentInfo1} <br/> ExternalDocumentInfo2: {!v.SimpleRecord.ExternalDocumentInfo2} <br/> FeaturedContentBoost: {!v.SimpleRecord.FeaturedContentBoost} <br/> FeaturedContentDate: {!v.SimpleRecord.FeaturedContentDate} <br/> FileExtension: {!v.SimpleRecord.FileExtension} <br/> FileType: {!v.SimpleRecord.FileType} <br/> FirstPublishedLocationId: {!v.SimpleRecord.FirstPublishedLocationId} <br/> IsDeleted: {!v.SimpleRecord.IsDeleted} <br/> IsLatest: {!v.SimpleRecord.IsLatest} <br/> LastModifiedById: {!v.SimpleRecord.LastModifiedById} <br/> LastModifiedDate: {!v.SimpleRecord.LastModifiedDate} <br/> IsMajorVersion: {!v.SimpleRecord.IsMajorVersion} <br/> NegativeRatingCount: {!v.SimpleRecord.NegativeRatingCount} <br/> OwnerId: {!v.SimpleRecord.OwnerId} <br/> PathOnClient: {!v.SimpleRecord.PathOnClient} <br/> PositiveRatingCount: {!v.SimpleRecord.PositiveRatingCount} <br/> PublishStatus: {!v.SimpleRecord.PublishStatus} <br/> RatingCount: {!v.SimpleRecord.RatingCount} <br/> ReasonForChange: {!v.SimpleRecord.ReasonForChange} <br/> ContentSize: {!v.SimpleRecord.ContentSize} <br/> SystemModStamp: {!v.SimpleRecord.SystemModStamp} <br/> TagCsv: {!v.SimpleRecord.TagCsv} <br/> VersionData: {!v.SimpleRecord.VersionData} <br/> VersionNumber: {!v.SimpleRecord.VersionNumber} <br/></p>
          <h1 class="slds-page-header__title slds-m-right--small  slds-truncate slds-align-left"><a href="{! $Site.siteUrlPrefix + '/one/one.app?source=alohaHeader#/sObject/' + v.simpleRecord.Id}">Go to details</a></h1>
        </div>
    </aura:if>    
    <!-- Display Lightning Data Service errors, if any -->
    <!-- <aura:if isTrue="{!not(empty(v.recordError))}">
      <div class="recordError">
        <ui:message title="Error" severity="error" closable="true">
          {!v.recordError}
        </ui:message>
      </div>
    </aura:if> -->
</aura:component>
1
  • Hi, Thats not SOQL, but SOSL. Commented May 24, 2018 at 18:19

1 Answer 1

1

Changing field API names is a nightmare. Those fields would be used in 1000's of classes, 3rd party integrations. Even if you managed to do that you have to create new fields and data load them. (You cannot rename a field in production, you have to create a new field, copy data from old field and then deploy your code which does not reference the old field and then delete that field). It can sometimes take 100 of manhours. No wonder there will be big resistance from your team.

You have to change your apex class and component to accomplish this. We would we using custom Apex wrapper to accomplish this. That Apex wrapper will contain the fields you need. Instead of using force:recordData we would use our own custom component to display response.

Apex class:

public class OCS_Search_Controller {


    public class ResposeWrapper{
        @AuraEnabled
        public String Id;

        @AuraEnabled
        public String ObjectName;

        @AuraEnabled
        public List<FieldClass> fieldMap; 

    }

    public class FieldClass{
        @AuraEnabled
        public String fieldName;
        @AuraEnabled
        public String fieldValue;

        public FieldClass(String fieldName, STring fieldValue){
            this.fieldName = fieldName;
            this.fieldValue= fieldValue;

        }

    }
    @AuraEnabled
    public static List<ResposeWrapper> searchForString(String searchText) {
        System.debug('The value of searchText is: '+ searchText);
    List<ResposeWrapper> responseList = new List<ResposeWrapper>();                 //This code creates the wrapper for the found records. 

    //The following code will look for matches in the OCS_Investigation__c object for the search argument entered.     
    List<List<SObject>> results1 = [FIND :searchText IN ALL FIELDS  RETURNING Account(Name,LastName), Contact(Name,AccountId) ];
    for (List<SObject> sobjs1 : results1) {
      for (SObject sobj1 : sobjs1) {
        ResposeWrapper rw=new ResposeWrapper();
        rw.Id=sobj1.Id;
        rw.ObjectName = ''+sobj1.getSObjectType();
         rw.fieldMap =new List<FieldClass>();
         Map<String,Object> fieldMap = sobj1.getPopulatedFieldsAsMap();
          for(String key : fieldMap.keySet()){
              FieldClass fc = new FieldClass(key,STring.valueOf(fieldMap.get(key)));
               rw.fieldMap.add(fc);

          }
        responseList.add(rw);
      }
    }
        return responseList;
  }
}

Component:

<aura:component controller="OCS_Search_Controller">
     <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:attribute name="responseObject" type="Object"></aura:attribute>

    <aura:iteration items="{!v.responseObject}" var="obj">
        <!-- Display names of all accounts -->
        {!obj.ObjectName}, {!obj.Id}
        <br></br>


        <aura:iteration items="{!obj.fieldMap}"  var="col">
            {!col.fieldName} : {!col.fieldValue}<br/>

    </aura:iteration>
        <hr></hr>
    </aura:iteration>
</aura:component>

Controller JS:

({
    doInit : function(component, event, helper) {

        var action=component.get("c.searchForString");
        console.log('Action set');
        action.setParams({searchText:'Jar'});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {

                var resp = response.getReturnValue();
                component.set("v.responseObject",resp);
                console.log(resp);
            }
        });
      $A.enqueueAction(action);

    }
})

Final OP:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .