5

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!

6
  • in init method you can call a apex method and get the Userinfo.getUserId() and set that value to task ownerId
    – Ratan Paul
    Aug 3 '16 at 15:46
  • have you tried creating the <force:inputField /> dynamically using the $A.createComponent by specifying the appropriate Id in the value attribute?
    – Praveen
    Aug 3 '16 at 15:50
  • I did try setting it via an apex controller class, but same issue. That's why I went to a static id for testing.
    – Brett
    Aug 3 '16 at 15:52
  • It's not even letting me render a force:inputField dynamically. I was able to render a button, but not a force:inputField.
    – Brett
    Aug 3 '16 at 16:55
  • Maybe if you share the rest of your code, the code that sets the user, we can figure out where is the problem. Probably the issue resides there.
    – D. Cruz
    Aug 3 '16 at 17:26
0

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.

1
  • Pretty sure this won't work any more due to lockerservice, unfortunately (or fortunately if you are keen to keep things secure :) ) Jun 13 '18 at 8:11
-1

To set on the init function object values you should set it like the default json:

init: function(component, event, helper) {
    var strNewTask ='{ "sobjectType": "Task" , "OwnerId": "0054B000000XxeH" }';
    var newTask = JSON.parse(strNewTask);
    component.set("v.task", newTask);
}
1
  • 1
    I tried this and it did not work. If I remove the default from the attribute itself, it shows a giant red error message (Unfortunately, there was a problem. Please try again. If the problem continues, get in touch with your administrator with the error ID shown here and any other related details. An internal server error has occurred) and never even executes the init method. If I leave the default and run the code above it just renders an empty text input.
    – Brett
    Aug 4 '16 at 15:47

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.