0

I have a requirement to create a new To Do record with autopopulating the Ownerid field to the logged in user. I have created a lightning component and trying to assign the returntype value from apex controller to the aura attribute. The value is being assigned to the aura attribute but not to the lookup field.here is my code

Apex controller

 public class CreateMeetingController {
  @AuraEnabled
    public static String getUserId(){
        return UserInfo.getUserId();
    }
  }

lightning component

    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global"  controller="Controller" >

<aura:attribute name="isOpen"           type="boolean" default="true" />
<aura:attribute name="newToDo"          type="Task"     />
<aura:attribute name="duedate"          type="DateTime" />
<aura:attribute name="subject"          type="String"   />
<aura:attribute name="assignTo"         type="String"   />
<aura:attribute name="relatedTo"        type="String"   />
<aura:attribute name="name"             type="String"   />
<aura:attribute name="status"               type="String"   />

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
     <div class="slds-m-around--xx-large">

    <aura:if isTrue="{!v.isOpen}">
    <div role="dialog" tabindex="-1" aria-labelledby="header99" class="slds-modal slds-fade-in-open ">
            <div class="slds-modal__container">
                <div class="slds-modal__header">
                    <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeModel}">
                        X
                        <span class="slds-assistive-text">Close</span>
                    </button>
                    <h2 id="header99" class="slds-text-heading--medium">Create To Do</h2>
                </div>
               <div class="slds-modal__content slds-p-around--medium">
                  <div class="slds-form--stacked">

                         <lightning:input aura:id="subjectInput" label="Subject" value="{! v.subject }" class="form-control"
                                      required="true"/>

                         <ui:inputDate aura:id="iddueDate" label="Due Date" class="form-control"
                                          value="{!v.duedate}" displayDatePicker="true" required="true"/>


                         <div class="form-group">

                            <c:inputLookup  value="{!v.name}"
                                        label="Name"
                                        pluralLabel="Contacts"
                                        sObjectAPIName="Contact"
                                        instanceId="relatedToContact"
                                        iconName="standard:contact"
                                        listIconClass="slds-icon-standard-contact"
                                        class="slds-size--1-of-1" />
                        </div>

                         <div class="form-group">
                                                                                                  <c:inputLookup  value="{!v.relatedTo}"
                                                    label="Related to Account"
                                                    pluralLabel="Account"
                                                    sObjectAPIName="Account"
                                                    iconName="standard:account"
                                                    listIconClass="slds-icon-standard-account"
                                                    class="slds-size--1-of-1" />
                        </div>

                      <div class="form-group">
                                                           <c:inputLookup  value="{!v.assignTo}"
                                        instanceId="UserId"
                                        label="Assigned To"
                                        pluralLabel="Users"
                                        sObjectAPIName="User"
                                        iconName="standard:user"
                                        listIconClass="slds-icon-standard-user"
                                        class="slds-size--1-of-1" />
                        </div>
                       <div class="form-group">
                           <ui:inputSelect label="Status " class="dynamic" aura:id="InputSelectDynamic" value="{!v.status}" />
                        </div>


                    </div>
                </div>
                <!--###### MODAL BOX FOOTER Part Start From Here ######-->
                <div class="slds-modal__footer">
                    <button class="slds-button slds-button--neutral" onclick="{!c.saveRecord}">Save</button>
                    <button class="slds-button slds-button--neutral" onclick="{!c.closeModel}">Cancel</button>
                </div>
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>
        <!--###### MODAL BOX Part END Here ######-->

    </aura:if>
</div>
  </aura:component>

Controller.js

   ({
doInit : function(component, event, helper) {
    component.set("v.isOpen", true);
           var action= component.get("c.getUserId");
    action.setCallback(this, function(response) {
          var state = response.getState();
        console.log('state-------->'+state);
        console.log('component.isValid()-------->'+component.isValid());
        if(component.isValid() && state === "SUCCESS"){
        var assignTo  = response.getReturnValue();
        console.log('assignTo------->'+assignTo);
            component.set("v.assignTo",assignTo);
            var userid= component.get("v.assignTo");
            console.log('userid-------------<'+userid);
        }
    });
    $A.enqueueAction(action);

}
   })

Debug log for controller.js

      state-------->SUCCESS
      component.isValid()-------->true
      assignTo------->0055B000000NSeLQAW
      userid-------------<0055B000000NSeLQAW

If you see from debug log the value is assigned to the attribute (userid) but in component its not prepopulating the value for the field with InstanceId: 'UserId'.

  • component code looks ok, how do you know the userId is not populated to the sub-component? please share it's code – Itai Shmida Oct 26 '17 at 6:01
  • Fixed the issue, I see that we cannot display the prepopulated value on the UI of lookup field. So I have used the <aura:if > tags to check if the param is empty o not and handled it by displaying the prepopulated value as an output text with a close button which onclick clears the param stored value. – consultantsfdc Oct 27 '17 at 15:28
0

So finally I could find a solution for my question. Here is what I implemented, Hope it is useful for anyother resources. I realized that we cannot display the prepopulated value on the UI of lookup field. So I have used the tags to check if the param is empty or not and handled it by displaying the prepopulated value as an output text with a close button which onclick clears the param stored value.Here is the updated code :

Lightning Component:

             <div class="form-group">
                    <span class="{! empty(v.assignTo2) ? '' : 'slds-hide' }">
                            <c:inputLookup  value="{!v.assignTo}"
                                    instanceId="UserId"
                                    label="Assigned To"
                                    pluralLabel="Users"
                                    sObjectAPIName="User"
                                    iconName="standard:user"
                                    listIconClass="slds-icon-standard-user"
                                    class="slds-size--1-of-1" />
                      </span>
                          <p class="{! empty(v.assignTo2) ? 'slds-hide' : 'slds-show' }">
                                    <div class="slds-form-element">
                                        <label class="slds-form-element__label" >Assigned To</label>
                                    </div>
                                    {!v.assignTo2}
                                    <lightning:buttonIcon class="slds-m-left_x-small"
                                        size="xx-small"
                                        iconName="utility:close"
                                        onclick="{!c.handleclearassignTo2}"
                                       alternativeText="Clear assignTo2 value " />
                                </p>

                    </div>

Added a new attribute AssignTo2--stores the prepopulated value in the doInit method of controller. AssignTo ---stores the value selected on UI.

Hope this helps! Cheers!

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.