You need to sign in to do that
Don't have an account?
Dynamic sObject Query
Hi,
I am trying to fetch the object name dynamically from the key prefix of current recordID. I am able to get the fields dynamically using a lightning component from every sobject. I tried to fetch the record names and I succeeded. From the same I trying to fetch the duplicate records upon clicking a button on the record page.
Scenario: Upon clicking the button on the record page, it should redirect to the other tab/window and display similar kinds of records with the record name. Here I wrote two SOQLs.
1. Fetch the record names
2. T fetch the duplicate records based on name.
Here, I wrote a soql query and it is returning the list of records available on the object in a lightning component. Now I want to store the record name in a variable. Here I am getting the error:
Here is the code I wrote:
public class LightningSchemaGlobalDescribe { @AuraEnabled public static list<sobject> fetchlables(String recordIdOrPrefix){ List<String> objFields = new List<String>(); list<string> fields = new list<string>(); list<string> fieldvalues = new list<string>(); list<sobject> records; String objectName = ''; list<Sobject> lists; 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(); String getRecordNameQuery= 'Select Id,name from '+ objectName +' WHERE ' +' id = :recordIdOrPrefix '; // lists= Database.query(getRecordNameQuery); // system.debug(lists); String sObjectName = objectName; Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName); SObject s = t.newSObject(); system.debug(s); //we are storing names of the Object lists= Database.query(getRecordNameQuery); system.debug(lists); string recordname=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); system.debug('editableFields::' + editableFields); system.debug('objFields::' + objFields); string fieldnamess = 'SELECT '+ objFields + ' FROM '+ objectName + ' WHERE Name LIKE %'+lists ; list<Sobject> listoffields = Database.query(fieldnamess); system.debug(listoffields); // system.debug('Fields:::'+objFields); } } } } } } catch(Exception e){ //print the error message System.debug(e); } return lists; } }I am getting the following error:
Please help me with this
Greetings!
This should not be causing the issue but still I would request to try with the Name instead of name to store the list of names in String.
Also,I would suggest you to create an array of Strings as the lists can contain more than one record.
Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.
Warm Regards,
Shirisha Pathuri
I got the error. Now I have another issue is, I am trying to display the duplicate record names on a component based on the current record name. For that, I wrote the following code
Using Application Events, I am trying to display the duplicate record names on a lightning component similar to the current record name.
Here is my component
When I click on the Merge duplicate button, it should redirect to the other component and display the duplicate record names like Chantal Smith.
This is my event: NavEvent
This is my Component1: NavigateCmp1 which should redirect to the other component:
Controller:: Helper:
Component 2: NavigateCmp2
I really don't understand to write a method on the controller to display the duplicate record name. Here is the controller
The Component2Event is displaying the current record name based on the current record ID My issue is with the duplicaterecords method. I could not able to figure out how to write the method to display the duplicate records.
Please help me out in writing the method to display the duplicate records