0

I can't get a lightning component that references User.ContactId to compile. Here's my code:

TestComponent.cmp

<aura:component controller="TestController">

</aura:component>

TestController.apxc

public with sharing class TestController {
    public void testMethod1 () {
        User u = [SELECT ID, ContactId FROM User LIMIT 1];
    }
}

When I compile the Apex class, it compiles just fine. But when I subsequently compile the lightning component, I get this error:

Failed to save TestComponent.cmp: Invalid definition for null:TestController: SELECT ID, ContactId FROM User LIMIT 1 ^ ERROR at Row:1:Column:12 No such column 'ContactId' on entity 'User'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.: Source

I know it's ContactId because everything compiles when I remove that field from the query. Also, when I remove the controller field from the component, it compiles just fine.

I know User.ContactId exists, and the Apex part compiles fine. But for some reason, the lightning component thinks that User.ContactId doesn't exist.

Why is it doing this, and how can I fix it?

0

As you said that your controller is working fine so whenever you are adding {!User.ContactId} in your component you are getting an error.

if you want User contactId, you have to do like this:

solution 1

TestController.cmp:

<aura:component controller="TestController"  implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="userContactId" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div>

   User Contact ID :{!v.userContactId}
</div>

TestController.js:

({
doInit : function(component, event, helper) {
     var action = component.get("c.testMethod1");

    action.setCallback(this, function(response) {

        var userContactId = response.getReturnValue();
        component.set("v.userContactId", userContactId);
    })
    $A.enqueueAction(action);
}

})

TestController.apxc:

public class TestController {
    @AuraEnabled
    public static void testMethod1 () {
    Id contactId = [SELECT ID, ContactId FROM User LIMIT 1].contactId;
        system.debug('uu'+contactId);
}

}

solution 2

if you want without using apex class then also you can do that with this:

TestController.cmp: Component will be same only.

TestController.js:

({
doInit : function(component, event, helper) {
    var userId = $A.get("$SObjectType.CurrentUser");
    console.log('userId' ,JSON.stringify(userId.ContactId));
    var userRecord = JSON.stringify(userId);
    component.set("v.userContactId", userRecord.ContactId);
}

})

you can try any of above code.

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.