function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bond006bond006 

How to Query Field Id for a Custom Object using Apex

Hi,

I need to redirect a page and pre-populate a Field with a value. To do that I need the Id of that field so that I can give in URL by attaching CF to that. 
I need that value using Apex code. I do not want to Hard Code it. How can I do that?

For Example :
a0n/e?CF00NJ0000001hOvc=2000452&CF00NJ0000001hOvc_lkid=a04J000000AzvddIAB

 
Tejpal KumawatTejpal Kumawat
Hello Bond,

Can you try this code :
 
// get sobject id prefix
String prefix = Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix();
String idVal = null;
Map<String, String> params = ApexPages.currentPage().getParameters();
for (String key : params.keySet()) {
  if (key.startsWith('CF') && key.endsWith('lkid')) {
    String val = params.get(key);
    if (val.startsWith(prefix)) {
      idVal = val;
      break;
    }
  }
}

If this answers your question then hit Like and mark it as solution!
Tejpal KumawatTejpal Kumawat
Hello Bond,

For find record id :
 
string lkId = ''; // lkid from object above code
string recordId = ApexPages.currentPage().getParameters().get(lkId );
If this answers your question then hit Like and mark it as solution!