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
vijay kumar kvijay kumar k 

can i edit or override the lookup list

suppose consider account and contact objects.  in creation of contact their shows account lookup field ,press the lookup icon shows the all accounts list in new window.my question is it is possible to edit that list. Like that list shows who doesn't have contacts or who have less than some vale of recors(take 10),these names only display that list.By using this list only select one account and create a contact. 
please help me this.

thanks in advance
Best Answer chosen by vijay kumar k
Newbie__SalesforceNewbie__Salesforce
Hi vijay

First you need to create a number field on account object to store count of contacts associated with it. You can do this by writing a trigger can refer the following code
 
trigger CountNumberOfContacts on Contact (after delete, after insert, after undelete, after update) {

	List<Contact> lstContacts;
    if (Trigger.isDelete) {
        lstContacts = Trigger.old;
    } else {
        lstContacts = Trigger.new;
    }

    Set<ID> accountIds = new Set<ID>();
    for (Contact con : lstContacts) {
            accountIds.add(con.AccountId);
    }
    
    List<Account> accountsToUpdate = new List<Account>(SELECT Id, Name, Number_of_Contacts__c, ([SELECT Name FROM Contacts) FROM Account where Id in accountIds]);
    for(Account acct : accountsToUpdate){
        List<Contact> contactList = acct.Contacts;
        if(contactList!=null){
            acct.Number_of_Contacts__c = Integer.valueOf(contactList.size());
        }
    }
    update accountsToUpdate;
}

 
Second create a lookup filter to display only accounts with number of contacts less than 10. Use Number_of_Contacts__c field created on account to compare count. 

Lookup filter queries refer this link - https://resources.docs.salesforce.com/200/latest/en-us/sfdc/pdf/salesforce_filtered_lookups_cheatsheet.pdf
Let me know if u face any issue for the same

All Answers

Newbie__SalesforceNewbie__Salesforce
Hi vijay

First you need to create a number field on account object to store count of contacts associated with it. You can do this by writing a trigger can refer the following code
 
trigger CountNumberOfContacts on Contact (after delete, after insert, after undelete, after update) {

	List<Contact> lstContacts;
    if (Trigger.isDelete) {
        lstContacts = Trigger.old;
    } else {
        lstContacts = Trigger.new;
    }

    Set<ID> accountIds = new Set<ID>();
    for (Contact con : lstContacts) {
            accountIds.add(con.AccountId);
    }
    
    List<Account> accountsToUpdate = new List<Account>(SELECT Id, Name, Number_of_Contacts__c, ([SELECT Name FROM Contacts) FROM Account where Id in accountIds]);
    for(Account acct : accountsToUpdate){
        List<Contact> contactList = acct.Contacts;
        if(contactList!=null){
            acct.Number_of_Contacts__c = Integer.valueOf(contactList.size());
        }
    }
    update accountsToUpdate;
}

 
Second create a lookup filter to display only accounts with number of contacts less than 10. Use Number_of_Contacts__c field created on account to compare count. 

Lookup filter queries refer this link - https://resources.docs.salesforce.com/200/latest/en-us/sfdc/pdf/salesforce_filtered_lookups_cheatsheet.pdf
Let me know if u face any issue for the same
This was selected as the best answer
vijay kumar kvijay kumar k
Thank you Newbie,
thank you for clarify my dought ,i tried it and your replay is working successfully.
Kate RobinKate Robin
Thanks alot newbie_salesforce for the clarification.
Regards, ​https://inedutor.com