[SalesForce] How to set a default value on lightning component force:inputField which is a lookup

I have a lightning component (Summer 16) which contains a "Task" attribute as follows:

    <aura:attribute name="task" type="Task" default="{ 'sobjectType': 'Task' }"/>

Further down the page, I have a force:inputField to allow the user to select the owner of the task as follows:

    <force:inputField value="{!v.task.OwnerId}" aura:id="OwnerId" />

This works great and allows me to select a user the lightning way.

Simple User List

However, I'd like to default this to the current user. I have tried setting it in the init event with no luck. The init event is firing, and I have retrieved the value after setting it and it is there. Static user id used for testing purposes only and it is a valid user id in our org.

    init: function(component, event, helper) {
        var tska = component.get("v.task");
        tska.OwnerId = '0054B000000XxeH';
        component.set("v.task", tska);
    }

I have also tried this code in the init method with no luck:

    component.find("OwnerId").set("v.value", "00561000001ZISx");

Now, if I create the attribute as follows, it does show it correctly:

    <aura:attribute name="task" type="Task" default="{ 'sobjectType': 'Task' , 'OwnerId': '0054B000000XxeH' }"/>

Default User

I tried setting the default dynamically with no luck. Any idea if this is possible? Thanks!

Best Answer

Both the OwnerId and the Owner need to be populated...

var newTask = { 
    'sobjectType' : 'Task', 
    'OwnerId' : '0054B000000XxeH', 
    'Owner' : { 
         'sobjectType' : 'User',
         'Id' : '0054B000000XxeH',
         'Name' : 'Brett'
    }
 };

but you also need to manually set some values on the force:inputField to get it to display the Pill (this assumes the force:inputField has an aura:id="taskOwnerId")...

var valuesOwner = [{
                type : 'User',
                id: newTask.OwnerId,
                label: newTask.Owner.Name, 
                icon : {
                    url:'/img/icon/t4v35/standard/user_120.png',
                    backgroundColor:'65CAE4',
                    alt:'User'
                }, 
                record: newTask.Owner,
                placeHolder: 'Search Users'
            }];
component.find("taskOwnerId").get("v.body")[0].set("v.values", valuesOwner); 

I'm not sure if this is supported but its working for me.

Related Topic