You need to sign in to do that
Don't have an account?
Lalitha Pavani Rallabandi 9
Dynamic Soql to fetch the fields dynamically
Hi,
My requirement is, I need a button on a record page to display the duplicate records with the current record name by on click.
Here is the code
Can some one please help me out on this
My requirement is, I need a button on a record page to display the duplicate records with the current record name by on click.
Here is the code
public class LightningSchemaGlobalDescribe { @AuraEnabled public static list<string> fetchlables(String recordIdOrPrefix){ List<String> objFields = new List<String>(); list<string> fields = new list<string>(); list<string> fieldvalues = new list<string>(); String objectName = ''; try{ String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3); Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); for(Schema.SObjectType stype : gd.values()){ Schema.DescribeSObjectResult r = stype.getDescribe(); String prefix = r.getKeyPrefix(); if(prefix!=null && prefix.equals(myIdPrefix)){ objectName = r.getName(); // here I am trying to get the record name dynamically String recordname= 'Select Name from '+ objectName+'where id=:'+recordIdOrPrefix; list<Sobject> lists= Database.query(recordname); if(lists.size()>0){ system.debug(lists); } string recordsname=lists.name; if(objectName!=null){ Map<String, Schema.SObjectField> efields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap(); list<string> editableFields = new list<string> (); for(schema.SObjectField editfieds :efields.values()){ schema.DescribeFieldResult fieldResult = editfieds.getDescribe(); if(fieldResult.isUpdateable() ){ editableFields.add(fieldResult.getName()); objFields = new List<String>(editableFields); // here i am trying to get the duplicate record names dynamically string fieldnamess = 'SELECT'+ objFields+'FROM'+ objectName + 'WHERE Name LIKE%'+ recordsname; list<Sobject> listoffields = Database.query(fieldnamess); system.debug(listoffields); } } } } } } catch(Exception e){ //print the error message System.debug(e); } return objFields; } }When I am trying to save the record getting error as:
Can some one please help me out on this
You may need to change line 37 to loop:
for (sobject obj : lists)
{
string recordsname=obj.name;
.....
}
and line 39: if(objectName!=null) should be move to after line 28. it should look like this:
objectName = r.getName();
if(objectName!=null)
{
.....
}
Thank you. I updated my code according to the suggestion like as follows
Still I am getting the following error:
objectName = r.getName();
if(objectName!=null){
// here I am trying to get the record name dynamically
String recordname= 'Select Name from '+ objectName+'where id=:'+recordIdOrPrefix;
list<Sobject> lists= Database.query(recordname);
for (sObject obj : lists) {
string recordsname=obj.name;
Map<String, Schema.SObjectField> efields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
list<string> editableFields = new list<string> ();
for(schema.SObjectField editfieds :efields.values()){
schema.DescribeFieldResult fieldResult = editfieds.getDescribe();
if(fieldResult.isUpdateable() ){
editableFields.add(fieldResult.getName());
objFields = new List<String>(editableFields);
// here i am trying to get the duplicate record names dynamically
string fieldnamess = 'SELECT'+ objFields+'FROM'+ objectName + 'WHERE Name LIKE%'+ recordsname;
list<Sobject> listoffields = Database.query(fieldnamess);
system.debug(listoffields);
}
}
}
}