function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Girish Reddy 52Girish Reddy 52 

I have created a lwc form using form form tag, here I want to assign a default user to user lookup field, How can I do it?

I want to assign a default user as logged-in user to the user lookup field on my LWC form. How can I do this? Can anyone please help me with this
CharuDuttCharuDutt
Hii Girish Reddy
You Can Use Below Code to Get Current logged In User For User Field On Form  Example
<template>
<lightning-card  variant="Narrow"  title="Hello" icon-name="standard:account">
    <lightning-record-edit-form object-api-name="Contact">
        <lightning-messages> </lightning-messages>
        <lightning-output-field field-name="OwnerId" value={user}>
        </lightning-output-field>
        <lightning-input-field field-name="FirstName"> </lightning-input-field>
        <lightning-input-field field-name="LastName"> </lightning-input-field>
        <lightning-input-field field-name="Email"> </lightning-input-field>
        <lightning-button
            class="slds-m-top_small"
            variant="brand"
            type="submit"
            name="update"
            label="Update"
        >
        </lightning-button>
    </lightning-record-edit-form>
</lightning-card>
</template>


JS

import { LightningElement } from 'lwc';
import USER_ID from '@salesforce/user/Id'; 
export default class CurrentLoggendInUser extends LightningElement {
user = USER_ID;
}
Please Mark It As Best Asnwer If It Helps
Thank You!
Girish Reddy 52Girish Reddy 52
Hi Charudutt,

I tried the code but I am using <lightning-record-form> & below is my code, I'm calling from Apex 
<template>    
    <lightning-card>
     
        <lightning-record-form
        object-api-name={objectApiName}
        fields={fields}
        onsuccess={handleSuccess}>
         <!-- below field I want to assign the default user -->
         <lightning-output-field field-name="BMCServiceDesk__FKInitiator__c" value={user}></lightning-output-field>
        </lightning-record-form>
       
    </lightning-card>
</template>

Here I want to assign BMCServiceDesk__FKInitiator__c as logged in user by default
mukesh guptamukesh gupta
Hi Girish,

Please follow below url for your solution:

https://jayakrishnasfdc.wordpress.com/2020/12/08/lightning-record-form-using-lwc/

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
CharuDuttCharuDutt
Hii Girish

lightning-record-form does not support prepopulating of field values when the form loads. To create a form that displays custom field values, use the lightning-record-edit-form component.

Please Close Your Query By Marking It As Best Answer If it Helps
Thank You!