0

I have a very basic method that correctly returns the value of the Name field of my custom object. However, when I try to retrieve the value of a custom field I get isnull as the result. I checked the field level security and I have rights to the custom fields. If I use the query editor in the Developer Console and run the same query, I get the correct results.

@AuraEnabled
global static String getEventName(String eventRecordId)
{
    return [SELECT Event__c.Name FROM Event__c WHERE Id = 'a0M6A000004KFdtUAG' AND NetworkId__c = :netwrkId].Name;   
}

({
doInit : function(component, event, helper) {
    var action = component.get("c.getEventName");        
    action.setParams({
        eventRecordId: component.get("v.recordId")
    });        
    action.setCallback(this, function(response) {
        var testmk= response.getReturnValue();            
        alert('result is' + testmk);
        component.set("v.eventName", testmk);
    });
    $A.enqueueAction(action);
},

The following query is not working:

    @AuraEnabled
global static String getEventName(String eventRecordId)
{
    return [SELECT Subheadline__c FROM Event__c WHERE Id = 'a0M6A000004KFdtUAG' AND NetworkId__c = :netwrkId].Name;   
}
  • What is your query for retrieving the custom field? As of now the query is only to retrieve the name field and seems that's working fine for you. – Jayant Das Jan 23 at 20:20
  • 2
    Your second query is attempting to return .Name, rather than .Subheadline__c. – David Reed Jan 23 at 20:28
  • Are you sure if that's the right query? – Jayant Das Jan 23 at 20:30
  • David...although my method name says getEventName, I am trying to get it to work to return the subheadline. Do you see why it wouldn't return the value in the Subheadline__c field? Actually...now I see it!! Thank you! – Michele Kleinhomer Jan 23 at 20:30
  • You should never use hard-coded ids. – Adrian Larson Jan 23 at 21:56
2

You will need to modify your query to return the field Subheadline__c which is being queried:

// you need to return the retrieved field here
[SELECT Subheadline__c 
    FROM Event__c 
    WHERE Id = 'a0M6A000004KFdtUAG' AND NetworkId__c = :netwrkId].Subheadline__c;   

Your current query:

[SELECT Subheadline__c 
    FROM Event__c 
    WHERE Id = 'a0M6A000004KFdtUAG' AND NetworkId__c = :netwrkId].Name;   

will not work and will throw a runtime error as below, because you are trying to retrieve Name from a SOQL which is not part of the retrieved results.

SObject row was retrieved via SOQL without querying the requested field

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.