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
jha pkjha pk 

display account list depends on country- Very Very Urgent

I have one custom object name "dedupe account" which is  in look up with standard custom object  Account . There are country custom picklist  field in Account object . I want to display all accounts w.r.to Country and also display only those account who is not associated to dedupe account. How can i do this using lightning  
Best Answer chosen by jha pk
sfdcMonkey.comsfdcMonkey.com
Here is the sample code
User-added image

apex class
public class accountListFromPicklistCtrl {
 @AuraEnabled
 public static List < String > getselectOptions(sObject objObject, string fld) {
  system.debug('objObject --->' + objObject);
  system.debug('fld --->' + fld);
  List < String > allOpts = new list < String > ();
  // Get the object type of the SObject.
  Schema.sObjectType objType = objObject.getSObjectType();
 
  // Describe the SObject using its object type.
  Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
 
  // Get a map of fields for the SObject
  map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
 
  // Get the list of picklist values for this field.
  list < Schema.PicklistEntry > values =
   fieldMap.get(fld).getDescribe().getPickListValues();
 
  // Add these values to the selectoption list.
  for (Schema.PicklistEntry a: values) {
   allOpts.add(a.getValue());
  }
  system.debug('allOpts ---->' + allOpts);
  allOpts.sort();
  return allOpts;
 }
    
    
    @AuraEnabled 
    public static List<account> getAccounts(String cont){
        List<Account> lstAccount = new List<Account>();
        
        for(account acc : [select id,Name,	Country__c,dedupe_account__c from account where dedupe_account__c = null AND Country__c =:cont]){
           lstAccount.add(acc); 
        }
        return lstAccount;
        
    }

}

Lightning component
<aura:component controller="accountListFromPicklistCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" />
    <aura:attribute name="ListOfAccount" type="List" />
    
     <div class="slds-p-around--large"> 
   <div class="slds-form-element">
      <label class="slds-form-element__label" for="select-01">Select Country</label>
      <div class="slds-select_container">
         <ui:inputSelect  aura:id="accCountryID" class="slds-select"  change="{!c.onPicklistChange}"/>
      </div>
   </div>
    </div>
      <div class="slds-p-around--large">
    <!--contacts table part--> 
   <table class="slds-table slds-table--bordered slds-table--cell-buffer">
      <thead>
         <tr class="slds-text-title--caps">
            <th>
               <span class="slds-truncate" title="Name">First Name</span>      
            </th>
            <th>
               <span class="slds-truncate" title="Country">Country</span>
            </th>
            <th>       
               <span class="slds-truncate" title="dedupe">dedupe account</span>
            </th>
            <th>       
                <span class="slds-truncate" title="createDep">Create Dedupe A/c</span>
            </th>
          
         </tr>
      </thead>
      <!--table body start, 
         Iterate contact list as a <tr>
         -->
      <tbody>
         <aura:iteration items="{!v.ListOfAccount}" var="acc">
            <tr>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.Name}"><a>{!acc.Name}</a></div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.LastName}">{!acc.Country__c}</div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.Department}">{!acc.dedupe_account__c}</div>
               </td>
               <td scope="row">
                   <div class="slds-truncate"> <lightning:button name="{!acc.Id}" variant="brand" label="New Dedupe" onclick="{! c.createDedupe }" /></div>
               </td> 
            </tr>
         </aura:iteration>
      </tbody>
   </table>
    </div>
    
</aura:component>
js controller
({
    doInit: function(component, event, helper) {
        helper.fetchPickListVal(component, 'Country__c', 'accCountryID');
 
    },
    onPicklistChange: function(component, event, helper) {
        // get the value of select option
        var country= event.getSource().get("v.value");
        var action = component.get("c.getAccounts");
        action.setParams({
            "cont": country
        });
      
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var allValues = response.getReturnValue();
               component.set("v.ListOfAccount",allValues);     
            }
        });
        $A.enqueueAction(action);
    
    },
    
    createDedupe : function(component, event, helper) {
        var whichOneAccount = event.getSource().get("v.name");
        console.log(whichOneAccount);
        
        var createRecordEvent = $A.get("e.force:createRecord");
          createRecordEvent.setParams({
             "entityApiName": "dedupe_account__c"
    });
    createRecordEvent.fire();
       
    }
    
})
js helper
({
    fetchPickListVal: function(component, fieldName, elementId) {
        var action = component.get("c.getselectOptions");
        action.setParams({
            "objObject": component.get("v.objInfo"),
            "fld": fieldName
        });
        var opts = [];
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var allValues = response.getReturnValue();
 
                if (allValues != undefined && allValues.length > 0) {
                    opts.push({
                        class: "optionClass",
                        label: "--- None ---",
                        value: ""
                    });
                }
                for (var i = 0; i < allValues.length; i++) {
                    opts.push({
                        class: "optionClass",
                        label: allValues[i],
                        value: allValues[i]
                    });
                }
                component.find(elementId).set("v.options", opts);
            }
        });
        $A.enqueueAction(action);
    },
    

})

NOTE : don't run with aura:application , add this component to .one app [Lightning experience] . if you have any isuue with it you can ask here
i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks [sfdcmonkey.com]
 

All Answers

jha pkjha pk
Thanks for your reply…requirement is below:- I want to display account according to country…e.g if I select india in country picklist drop down then only account who are associated with country india will display with no dedupe account. Dedupe account have lookup relationship with account . dedupe account have multiple account or may have not. some account don’t have dedupe account . So once you select country like india, US..China..etc… respective account will display . Suppose country India have 10 account ..but 6 account don’t associate with dedupe account . So once you select India in your country filter then it will display account of india with no dedupe account. Hope You understand the requirement . Let me know if you have any queries. Thanks, Praveen Jha
jha pkjha pk
Yes… VF coding
jha pkjha pk
Sorry …Lightning
sfdcMonkey.comsfdcMonkey.com
ok i got it, give me some time i will give you a sample lightning component for it.
thanks [sfdcmonkey.com]
jha pkjha pk
Thanks buddy..also one thing added once account display for respective country with no dedupe account there is one button “create dudupe account” so that we can create dedupe account for respective account who don’t have dedupe account. That will be great help Thanks
jha pkjha pk
When we click “Create dedupe account” button …page will navigate to dedupe account detail page. So that we can create dedupe account for those account who don’t have dedupe account.
sfdcMonkey.comsfdcMonkey.com
Here is the sample code
User-added image

apex class
public class accountListFromPicklistCtrl {
 @AuraEnabled
 public static List < String > getselectOptions(sObject objObject, string fld) {
  system.debug('objObject --->' + objObject);
  system.debug('fld --->' + fld);
  List < String > allOpts = new list < String > ();
  // Get the object type of the SObject.
  Schema.sObjectType objType = objObject.getSObjectType();
 
  // Describe the SObject using its object type.
  Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
 
  // Get a map of fields for the SObject
  map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
 
  // Get the list of picklist values for this field.
  list < Schema.PicklistEntry > values =
   fieldMap.get(fld).getDescribe().getPickListValues();
 
  // Add these values to the selectoption list.
  for (Schema.PicklistEntry a: values) {
   allOpts.add(a.getValue());
  }
  system.debug('allOpts ---->' + allOpts);
  allOpts.sort();
  return allOpts;
 }
    
    
    @AuraEnabled 
    public static List<account> getAccounts(String cont){
        List<Account> lstAccount = new List<Account>();
        
        for(account acc : [select id,Name,	Country__c,dedupe_account__c from account where dedupe_account__c = null AND Country__c =:cont]){
           lstAccount.add(acc); 
        }
        return lstAccount;
        
    }

}

Lightning component
<aura:component controller="accountListFromPicklistCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" />
    <aura:attribute name="ListOfAccount" type="List" />
    
     <div class="slds-p-around--large"> 
   <div class="slds-form-element">
      <label class="slds-form-element__label" for="select-01">Select Country</label>
      <div class="slds-select_container">
         <ui:inputSelect  aura:id="accCountryID" class="slds-select"  change="{!c.onPicklistChange}"/>
      </div>
   </div>
    </div>
      <div class="slds-p-around--large">
    <!--contacts table part--> 
   <table class="slds-table slds-table--bordered slds-table--cell-buffer">
      <thead>
         <tr class="slds-text-title--caps">
            <th>
               <span class="slds-truncate" title="Name">First Name</span>      
            </th>
            <th>
               <span class="slds-truncate" title="Country">Country</span>
            </th>
            <th>       
               <span class="slds-truncate" title="dedupe">dedupe account</span>
            </th>
            <th>       
                <span class="slds-truncate" title="createDep">Create Dedupe A/c</span>
            </th>
          
         </tr>
      </thead>
      <!--table body start, 
         Iterate contact list as a <tr>
         -->
      <tbody>
         <aura:iteration items="{!v.ListOfAccount}" var="acc">
            <tr>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.Name}"><a>{!acc.Name}</a></div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.LastName}">{!acc.Country__c}</div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!acc.Department}">{!acc.dedupe_account__c}</div>
               </td>
               <td scope="row">
                   <div class="slds-truncate"> <lightning:button name="{!acc.Id}" variant="brand" label="New Dedupe" onclick="{! c.createDedupe }" /></div>
               </td> 
            </tr>
         </aura:iteration>
      </tbody>
   </table>
    </div>
    
</aura:component>
js controller
({
    doInit: function(component, event, helper) {
        helper.fetchPickListVal(component, 'Country__c', 'accCountryID');
 
    },
    onPicklistChange: function(component, event, helper) {
        // get the value of select option
        var country= event.getSource().get("v.value");
        var action = component.get("c.getAccounts");
        action.setParams({
            "cont": country
        });
      
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var allValues = response.getReturnValue();
               component.set("v.ListOfAccount",allValues);     
            }
        });
        $A.enqueueAction(action);
    
    },
    
    createDedupe : function(component, event, helper) {
        var whichOneAccount = event.getSource().get("v.name");
        console.log(whichOneAccount);
        
        var createRecordEvent = $A.get("e.force:createRecord");
          createRecordEvent.setParams({
             "entityApiName": "dedupe_account__c"
    });
    createRecordEvent.fire();
       
    }
    
})
js helper
({
    fetchPickListVal: function(component, fieldName, elementId) {
        var action = component.get("c.getselectOptions");
        action.setParams({
            "objObject": component.get("v.objInfo"),
            "fld": fieldName
        });
        var opts = [];
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
                var allValues = response.getReturnValue();
 
                if (allValues != undefined && allValues.length > 0) {
                    opts.push({
                        class: "optionClass",
                        label: "--- None ---",
                        value: ""
                    });
                }
                for (var i = 0; i < allValues.length; i++) {
                    opts.push({
                        class: "optionClass",
                        label: allValues[i],
                        value: allValues[i]
                    });
                }
                component.find(elementId).set("v.options", opts);
            }
        });
        $A.enqueueAction(action);
    },
    

})

NOTE : don't run with aura:application , add this component to .one app [Lightning experience] . if you have any isuue with it you can ask here
i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks [sfdcmonkey.com]
 
This was selected as the best answer
jha pkjha pk
don't run with aura:application , add this component to .one app [Lightning experience] what is the meaning of this. How can I run this code and how to test this code to see output? I don’t have lightning exp. Please help here