I am working on a Lightning Component and I am trying to refactor my code. I have four combo boxes set up to query junction objects. The selection for object one determines what fills the list for Object Two, and two's selection determines what fills three, etc.

I currently have four versions of a method call used to make an SOQL query against three different custom junction objects. I would like to make it more generic and pass the customObject__c type as a parameter so I can use the same method, but tell it which object in the call, rather than hard code my query.

In my component I am using lightning combo boxes to display each list result:

<aura:attribute name="objOne" type="List" default="[]"/>
<lightning:combobox name="objOneSelect" label="Object One" placeholder="Select a Object" options="{! v.objOnes}" onchange="{! c.handleObjOne }"/>

and three more for objects 2 - 4.

In the controller I have a call to the helper:

handleObjOne: function (component, event, helper) {
    var selectedOptionValue = event.getParam("value");
    var params = ({mySelection : selectedOptionValue});

The helper uses the following:

   callServer : function(component,method,attributeName,params) {
    var action = component.get(method);
    if (params) {

        var state = response.getState();
        if (state === "SUCCESS") { "logic to fill value and label in option list"}

Currently, the call only takes the selectOptionValue and uses it in the query on my getList class. I can pass the necessary object__c and object___r info as strings then build a query string, but I am stuck at how to get the actual 'Type' passed. My query is:

List<objTwo__c> objectList = [SELECT Name FROM objTwo__c WHERE Id IN (SELECT objTwo__c FROM oneTwoJunction__c WHERE objOne__r.Name LIKE :mySelection)];
List<String> objTwoNames = new List<String>();
    for(objTwo__c two : objectList ){

The first line makes the query, and I tried using List but I get an error: Invalid loop variable type expected SObject was objTwo__c. The loop is used to parse out the name from the object list, but it doesn't accept sObject, either.

I've tried using the Schema, and getGlobalType, but it seems I must cast the newObject as the type I need, which defeats my purpose.

Is there a way to pass type as a parameter?

  • Not sure I follow your question but if you are going to abstract the object type you need to return List<SObject> instead of a typed list. Hard to say the best way to proceed since we do not know how you are using it or how you are populating the objOne. Do the objects have the same fields? If not you could define a wrapper that has a list of fields for the Subject that can be used in the component. You can describe on the SObject and get that information. It just depends on how you want to use the information
    – Eric
    Sep 6 '18 at 2:38

Here is how you can build a query using a string, and then iterate on the result.

For you, you would concatenate the sObject name to the string to query one of your four objects.

I'm assuming that all four objects have a 'Name' field. If they don't it complicates matters, as you have to do a typeForName and a getName call on the sObjectType.

Here is a simple example (verified):

public Set<String> queryObj(String sObjName){

    String query = 'SELECT Id, Name FROM ' + sObjName + ' LIMIT 10';
    sObject[] objList = Database.query(query);
    Set<String> objNames = new Set<String>();
    for (sObject obj : objList ){
    return objNames;
  • Thank you. This works "much" better than the convoluted method I was trying to get data out of the object. I have much to learn in SOQL. Thanks!
    – Bradley S
    Sep 6 '18 at 14:43
  • Just make sure you wrap it in a try/catch block in case someone passes in and sObjectName like 'Case' which doesn't have a Name attribute :) Sep 6 '18 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.