0

I have one Custom Lookup Component I want this Field to be set automatically when I'm Passing the Id but it is not happening.

Here you go

<aura:attribute name="selectedLookUpRecord" type="Account" default="{}"/>

<aura:handler name="init" action="{!c.quotebody}" value="{!this}"/> //Using this Here I'm loading with data but custom lookup field one is not taking 

 <c:customLookup aura:id="custlookid" objectAPIName="account" IconName="standard:account" selectedRecord="{!v.selectedLookUpRecord}" />

How I'm passing Value to the Custom lookup on Controller here it is:

cmp.set("v.selectedLookUpRecord.Id",quotedetailes1.Account_Name__c);

In the backend, value is storing in the attribute but in the user interface side lookup field is empty.

Here is the image for reference:

enter image description here

It is empty like above field image this I'm passing the value also.

enter image description here

I want the field to be filled like this automatically.

0

You have to make a query in doinit method and set the selectedRecord object instance based on result.

Add below methods in Helper of CustomLookup

clearSelection : function(component, event, helper) {
      var pillTarget = component.find("lookup-pill");
     var lookUpTarget = component.find("lookupField"); 

     $A.util.addClass(pillTarget, 'slds-hide');
     $A.util.removeClass(pillTarget, 'slds-show');

     $A.util.addClass(lookUpTarget, 'slds-show');
     $A.util.removeClass(lookUpTarget, 'slds-hide');

     component.set("v.SearchKeyWord",null);
     component.set("v.listOfSearchRecords", null );
     component.set("v.selectedRecord", {} );   
},doInit : function(component, event, helper) {

    var selectedRecord = component.get("v.selectedRecord");
    //searchText = event.getParam("defaultValue");
    if(!$A.util.isEmpty(selectedRecord )) {
        this.clearSelection(component, event, helper);

        var objectAPIName = component.get("v.objectAPIName");


        var action = component.get('c.getDefaultValue');

        action.setParams({
            recordId: selectedRecord.Id,
            ObjectName: objectAPIName

        });

        action.setCallback(this, function (a) {

            if (a.getState() === 'SUCCESS') {
                console.log( a.getReturnValue());
                    component.set("v.selectedRecord" , a.getReturnValue()); 

                var forclose = component.find("lookup-pill");
                   $A.util.addClass(forclose, 'slds-show');
                   $A.util.removeClass(forclose, 'slds-hide');

                var forclose = component.find("searchRes");
                   $A.util.addClass(forclose, 'slds-is-close');
                   $A.util.removeClass(forclose, 'slds-is-open');

                var lookUpTarget = component.find("lookupField");
                    $A.util.addClass(lookUpTarget, 'slds-hide');
                    $A.util.removeClass(lookUpTarget, 'slds-show');  
            } else if (a.getState() === "ERROR") {
                var errors = a.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +
                            errors[0].message);
                        var message = "Error message: " + errors[0].message;

                    }
                } else {
                    console.log('Unknown error');
                    var message = 'Unknown error';

                }
            }
        });

        $A.enqueueAction(action);
    }
},

and Apex controller code should be like below

 @AuraEnabled(cacheable=true)
public static  sObject  getDefaultValue(String recordId, String ObjectName) {


    List < sObject > returnList = new List < sObject > ();

    // Create a Dynamic SOQL Query For Fetch Record List with LIMIT 5   
    String sQuery =  'select id, Name from ' +ObjectName + ' where Id = :recordId order by createdDate DESC limit 5';
     return Database.query(sQuery);

}

enter image description here

| improve this answer | |
  • Hey This will automatically set the value to the field of a custom lookup – VINOD Jan 8 at 10:20
  • Yes, that what do you want Right? – Nigam Goyal Jan 8 at 10:35
  • No It's not working, Just can you show me sample output image – VINOD Jan 8 at 11:49
  • You can see in the answer, I have already added the output image – Nigam Goyal Jan 8 at 11:52
  • But for me, it's not coming to See Actually I want to set that value dynamically – VINOD Jan 8 at 12:35

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.