0

I have a context aware (hasRecordId) Lightning component that can be dropped on an undetermined custom object record page. I need the component to grab the recordId, then search for that recordId across all fields of other custom objects. These might be lookup or master-detail, undetermined.

Example schema: let's say my customer has built a recruiting app, with objects including Position, Applicant, and JobApplication. There's a field on JobApplication for Position, and another field for Applicant. But unknown whether it's lookup or master-detail, or maybe just a text field. (I know this is vague, but I won't know what custom schema my customers are using. I'll only be able to dynamically derive recordId and and the custom object to search.)

If I drop my component on an Applicant record page, I want it to search JobApplications -- all fields -- for that recordId. I've been assuming that SOSL is the right approach, since I don't know the fields to search. But... I've discovered that recordId isn't treated as text, so you can't SOSL search on it.

I thought about using schema describe to dynamically pull all the field names from the custom object I'm searching, then do a SOQL search, but a custom object could have hundreds of fields, and that seems like a really bad approach.

I'm aware that Salesforce makes a leap between an Id and the Name sometimes. Like if you link records by Id, it will actually display the Name, not the id. Maybe I'll try using the recordId to pull the record's Name, then SOSL on that?

Any ideas?

0

OK, I think I have it, if I allow myself not to search ALL fields. I'm going to assume that if the record id of type Object A appears on an Object B record, the field name will be Object A. That would be true for Master-Detail, Lookup, and would be the logical name for any other linkage. (I know there will be exceptions, but better to make if work for most than not work for any.)

So, I can derive the name of Object A from its record id using this:

String sObjName = recordId.getSObjectType().getDescribe().getName();

I have already derived Object B's name from the custObj aura:attribute, and passed it to APEX. So now I can build a query string like this:

String qr = 'SELECT Id, Name, lastModifiedDate FROM ' + custObj + ' WHERE ' + sObjName + '=\'' + recordId + '\'';

I also pulled the recordName from the recordId, and tried using that in a SOSL search of all fields, but the name doesn't serve as a proxy for the Id as I had hoped it might.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.