1

How to find field_name/api_name name for any object that contains a value?

I'm working on a object that has dozens of fields but I'm after finding out particular value belongs to which field name.

for an example:

I have a value ABC123 in MyCustomObject

one possible way I can do is something like this:

SELECT field_name FROM MyCustomObject WHERE field_name = 'ABC123' || field_name2 = 'ABC123' etc... but this is very tedious.
6
  • do you want to add all fields and there value in where condition?
    – Ratan Paul
    Jan 30, 2016 at 3:10
  • that's one way to do it but i have dozens of fields and what i'm looking is more like a generic so i can use for any other sobject.
    – Nick
    Jan 30, 2016 at 3:24
  • you can get the all sobject fields.. but for value you need to query
    – Ratan Paul
    Jan 30, 2016 at 3:26
  • yes for value i need to query but what about the fields do i need to write all dozens of fields as i have shown in my question. show what you thinking so i will get better understanding.
    – Nick
    Jan 30, 2016 at 3:28
  • In which data you are interested? Are you interested in matching records or the API names of fields where ABC123 would be found? Jan 30, 2016 at 7:21

1 Answer 1

2

Instead of making dynamic soql why don't you use SOSL.

try this query

List<Sobject> Results = [FIND 'ABC123' IN ALL FIELDS RETURNING customobject(Id, Name)];

or if you have dynamic value then

string searchQuery = 'FIND \'' +searchtext+'*\' IN ALL FIELDS RETURNING customobject(Id, Name)';
Search.query(searchQuery)

and you don't need to write all fields.

You can also find a good example Here. It also handle 2000 records limit in SOSL.

1
  • ohh yes.. I was thinking. He want to get all the fields values that why gave that ans.. Good catch @Tushar.. Yes this the best ans for question, +1 for you...
    – Ratan Paul
    Jan 30, 2016 at 15:13

You must log in to answer this question.

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