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
Lalitha Pavani Rallabandi 9Lalitha 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
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:
User-added image
Can some one please help me out on this
 
David Zhu 🔥David Zhu 🔥

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) 
{
.....
}

Lalitha Pavani Rallabandi 9Lalitha Pavani Rallabandi 9
Hi @David Zhu,
Thank you. I updated my code according to the suggestion like as follows
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();
                    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 record name dynamically
                    String recordname= 'Select Name from '+ objectName+'where id=:'+recordIdOrPrefix;
                    
                    list<Sobject> lists= Database.query(recordname);
                    system.debug(lists);
                     string recordsname;
                    for (sobject obj : lists)
                    {
                       recordsname=obj.name;
                     } 
                    // 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;
    }
    
    
    
}

Still I am getting the following error:
User-added image
David Zhu 🔥David Zhu 🔥
You may use the code below. To be honest, your code logic for building the return result is incorrect. You will have to figure it out by your own.

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