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.
  • do you want to add all fields and there value in where condition? – Ratan Paul Jan 30 '16 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 Kahn Jan 30 '16 at 3:24
  • you can get the all sobject fields.. but for value you need to query – Ratan Paul Jan 30 '16 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 Kahn Jan 30 '16 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? – Kiran Machhewar Jan 30 '16 at 7:21

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)';

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.

| improve this answer | |
  • 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 '16 at 15:13

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.