2

This question already has an answer here:

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'

}"/>

marked as duplicate by Basti, Oleksandr Berehovskyi, battery.cord, Santanu Boral, glls Jan 9 '18 at 16:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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
   @AuraEnabled
    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}"/>
</aura:component>

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());
         }
      });
       $A.enqueueAction(action);
     }
 })

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 – William Young 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 – GastonProbo Jan 9 '18 at 14:28
  • 'ContactId': '{!v.ContactId}' give an error, how can we change it? – GastonProbo Jan 9 '18 at 21:41
  • Just relaized you don't need quotes there .Updated my answer – Mohith Shrivastava Jan 9 '18 at 23:24

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