How do you set a dynamic aura attribute default value like current userinfo instead of hardcoding. See below.

The harcoding accountid works, not dynamic contactid/not supported.

 <aura:attribute name="lookupcase" type="Case" default="{

    'sobjectType': 'Case',

    'ContactId': '{!v.userInfo.ContactId}',                                               

    'AccountId': '0012D000002oAkt'


There is no documented way to get the contactId by using the global variable .

One way to achieve this is by writing custom code like below

public with sharing class SimpleServerSideController {

  //Use @AuraEnabled to enable client- and server-side access to the method
    public static String getUserName() {
      return [Select Id, ContactId from User where Id =:userinfo.getUserId()].ContactId;

Custom Lightning code

<aura:component controller="SimpleServerSideController">
   <aura:attribute name="ContactId" type="String"/>
    <aura:attribute name="lookupcase" type="Case" default="{'sobjectType': 'Case','ContactId': {!v.ContactId}}"/>
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

JS code

doInit: function(cmp){
    var action = cmp.get("c.getUserName");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
            cmp.set("v.ContactId", response.getReturnValue());

If you want return the whole userinfo object then you can reference multiple fields .

  • fyi, there is an idea out there for the support of such $User variable like exists in VF: success.salesforce.com/ideaView?id=0873A000000E7lFQAS Jan 9 '18 at 1:55
  • get the error: Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}: Source Jan 9 '18 at 14:28
  • 'ContactId': '{!v.ContactId}' give an error, how can we change it? Jan 9 '18 at 21:41
  • Just relaized you don't need quotes there .Updated my answer Jan 9 '18 at 23:24

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