0

Have a junction object called AccountPublisherAssociation that is a junction between an Account and a custom object called Publisher. I would like to write a query to display all the Publishers that have been selected for a particular account id.

The following SOQL gives me the result with the right record count but with empty values in the name field.

ApexController

@AuraEnabled
public static List < sObject > getCurrentRecords(String ParentId) {

List < sObject > lstOfRecords =
          [select Publisher__r.id, Publisher__r.Name  from AccountPublisherAssociation__c where Account__c :ParentId];


   return lstOfRecords;
}

Javascript Controller Call the function during init

doInit: function(component, event, helper) {
    //Call server controller
    //Set the existing records 

    var action = component.get('c.getCurrentRecords');
    action.setParams({
        "ParentId": component.get("v.recordId")
    });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var storeResponse = response.getReturnValue();

            if (storeResponse.length == 0) {
                component.set("v.Message", 'No Records Found...');
            } else {
                component.set("v.Message", '');
                // set searchResult list with return value from server.
            }
            component.set("v.selectedLookUpRecordsPublisher", storeResponse); 
        }
         if(state == "ERROR")
        {
            var errors = response.getError();
            console.log(errors);
        }
    });
    // enqueue the Action  
    $A.enqueueAction(action);
},

The above code works well if we directly query the publisher table.

This gives the accurate record count but with empty values i.e it gives 5 rows with empty values for the id and name fields. There are no errors on the developer console as well. Tried the below query with same results as well. I am not sure why the values are null but the count is accurate

[select id, Name  from Publisher__c where id in (select Publisher__c from AccountPublisherAssociation__c where Account__c = :accountId)];

Relevant Component Code

This a reusable component that shows a list of pills as the selected Publishers associated with an account.

<aura:component controller="tagListCtrl" Implements="flexipage:availableForRecordHome,force:hasRecordId" access="global">

<aura:attribute name="lstSelectedRecords" type="sObject[]" default="[]" description="Use,for store SELECTED sObject Records"/>

<aura:iteration items="{!v.lstSelectedRecords}" var="selectedRec">
                    <lightning:pill label="{!selectedRec.Name}" name="{!selectedRec.Id}" onremove="{! c.clear }">
                        <aura:set attribute="media">
                            <lightning:icon iconName="{!v.IconName}" size="x-small" alternativeText="icon"/>
                        </aura:set>
                    </lightning:pill>
                </aura:iteration> 

</aura:component>

Tried id, and Id but both result in the same issue.

7
  • 3
    Can the user running the query see the Publisher__c records? That's the only way the values could possibly end up null.
    – sfdcfox
    Aug 6, 2018 at 10:08
  • Yes. Working on the developer edition and I can see the all records with the login I have. This is a lighntning component that sits on the account page layout. If I do a direct query to select Publishers__c it works and shows me the records.
    – user275157
    Aug 6, 2018 at 10:23
  • 1
    Can you share more code context in the body of your question? The SOQL looks perfectly correct. How are you inspecting the return values to find these nulls?
    – David Reed
    Aug 6, 2018 at 13:17
  • Updated the code. I bind the return values to a component that I created which is a multi select box. The values are set just fine for any query other than the join query if I directly query publisher then the values get displayed on the UI. New to salesforce development. Not sure how I could debug the Apex controller code.
    – user275157
    Aug 6, 2018 at 15:39
  • Can you add the component markup where you're showing the values as well? I think the issue has to do with how you're traversing the relationship to get those values in the client, not your Apex.
    – David Reed
    Aug 6, 2018 at 15:40

1 Answer 1

1

I think this is a binding issue at the component level, not an issue with your SOQL or Apex.

Based on what you'd like to display, I think the right form of the query is the second one you've shown,

select id, Name  from Publisher__c where id in (select Publisher__c from AccountPublisherAssociation__c where Account__c = :accountId)

The fact that a console.log() in your client-side controller yields the correct values suggests that the issue lies with the value binding, and I would venture to speculate that the experimentation you've gone through to get here has simply produced some disconnect or inconsistency in how you're referring to the data.

As shown, your controller is populating the wrong attribute (selectedLookUpRecordsPublisher; should be lstSelectedRecords). Additionally, make sure your value bindings are not referring to a relationship path or intermediate sObject value, as would have been implied by the first form of your query.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .