0

component

<aura:component controller="AudioController">
    <aura:attribute name="setMeOnInit" type="String" default="default value" />
    <aura:handler name="init" value="{!this}" action="{!c.getRecURL}"/>
    <h1>{!v.setMeOnInit}</h1>
</aura:component>

Server controller

public class AudioController {

 @AuraEnabled
    public static String getRecURLdata() {
        Contract c = [SELECT Id, recURL__c FROM Contract WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        return 'hello from cont';
    }

}

Client controller

({
    "getRecURL" : function(cmp) {
        var action = cmp.get("c.getRecURLdata");

            action.setCallback(this, function(response) {
                cmp.set("v.setMeOnInit", response.getReturnValue());
            });

        $A.enqueueAction(action);
    }
})

These are mostly just test values. I'm trying to get "hello from cont" to show up in the component

3
  • 1
    you do have another questions with answers "pending" any reason you havent accepted any in your other post? additionally, the soql you are attempting, is not the way to pass parameters from your lignting component/controller to your apex class. you will need to create a context aware component should you need to pass a record ID
    – glls
    Jul 6, 2018 at 14:09
  • you migth also want to consider adding error/exception handling to your code + valdiate if your apex method is being called at all. A simple debug log or checking logs should do the trick
    – glls
    Jul 6, 2018 at 14:10
  • You would have got a null pointer expcetion in your apex controller at first place and that should have explained.
    – Jayant Das
    Jul 6, 2018 at 14:24

1 Answer 1

4

Your code was failing because ApexPages can only be used from Visualforce page context.

If you actually need Id from SObject in your controller, then what I would suggest is to use is to use force:hasRecordId. More on this interface you can read here. Then your code would look like this. component.cmp:

<aura:component controller="AudioController" implements="force:hasRecordId">
    <aura:attribute name="setMeOnInit" type="String" default="default value" />
    <aura:handler name="init" value="{!this}" action="{!c.getRecURL}"/>
    <h1>{!v.setMeOnInit}</h1>
</aura:component>

AudioController.cls:

public class AudioController {

    @AuraEnabled
    public static String getRecURLdata(final Id recordId) {
        if (recordId.getSObjectType() == null) {
            throw new AuraHandledException('RecordId is null');
        }
        if (recordId.getSObjectType() != Contract.getSObjectType()) {
            throw new AuraHandledException('Wrong SObject Type. Please use this component on Contract record page');
        }
        Contract c = [SELECT Id, Name FROM Contract WHERE Id = :recordId];
        return 'hello from cont';
    }

}

componentController.js:

({
    "getRecURL" : function(component) {
        var action = component.get("c.getRecURLdata");
        action.setParams({
            recordId: component.get('v.recordId')
        })

        console.log('Hello World');
        action.setCallback(this, $A.getCallback(function(response) {
            let state = response.getState();
            if (state === 'SUCCESS') {
                component.set("v.setMeOnInit", response.getReturnValue());
            } else {
                console.error(response.getError()[0].message);
            }
        }));

        $A.enqueueAction(action);
    }
})

And you will need to write tests for AudioController and can pass recordId directly as a parameter. And won't need to manage Page parameters in your tests, which will lead to easier to read and maintain code.

0

You must log in to answer this question.

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