You need to sign in to do that
Don't have an account?
Jeremiah Cochran
dynamic SOQL query not returning data stored in field queried
Here is the context of what I'm trying to do. I have a lightning component that I want to be able to use on any object so I have added an attribute to the design file that allows a user to enter the API Name of the field that data should be retrieved from. I have a dynamic SOQL query that takes in variables for the sobject type and the field the user indicated. However, the returned result is the ID for the object record, not the data stored in the field. How do I get the actual data from the field?
Here is my code:
public with sharing class ProgressBarControler{ @AuraEnabled public static List<String> getProgress(Id recordId, String fieldName) { List<String> progress = new List<String>(); Id myId = recordId; system.debug('the record Id is '+myId); String objfieldName = fieldName; system.debug('the fieldName is '+objfieldName); String sObjName = myId.getSObjectType().getDescribe().getName(); system.debug('the sObjectName is '+sObjName); SObjectType sObjType = Schema.getGlobalDescribe().get(sObjName); // Create Dynamic Query Start .. String theQuery = 'SELECT '; // Insert field name variable into Query theQuery += objfieldName + ',' ; system.debug('the field name is '+objfieldName); // Trim last comma theQuery = theQuery.subString(0, theQuery.length() - 1); // Finalize query string theQuery += ' FROM '+sObjType+' LIMIT 1'; // Query End .. System.debug('theQuery = '+theQuery); Object o = Database.Query(theQuery); String returnValue = String.valueOf(o); progress.add(returnValue); System.debug('the return value = '+returnValue); return progress; } }
Hi All, Thanks for the suggestions. I was able to resolve the issues using the documentation found here:
After making those changes it works perfectly.https://opfocus.com/dynamic-field-binding-in-salesforce-lightning-experience/
Essentially I had to simplify my APEX controller and change it to return a sObject and in my Component Controller I had to use this:
All Answers
Seems good to me.
Thanks, One problem solved. The value is showing in the debug log now however it is not being set in the lightning component. Here is my code for that:
Here is the attribute that should set by the code above:
<aura:attribute name="progress" type="integer" default="0"/>
A list of strings cannot go into an Integer, which is what your component is set up to display. Either change the return type of the method or the type of "progress"
In your component, change the attribute to below:
@Ravi Dutt Sharma
When I try to make the changes you suggest I get this error: Method does not exist or incorrect signature: void add(String) from the type String
Hi All, Thanks for the suggestions. I was able to resolve the issues using the documentation found here:
After making those changes it works perfectly.https://opfocus.com/dynamic-field-binding-in-salesforce-lightning-experience/
Essentially I had to simplify my APEX controller and change it to return a sObject and in my Component Controller I had to use this: