0

I have created one search bar component which is working when search with contact name. Now i want to search contact along with account name. For eg. if contact name is 'Contact1' and account name is 'account1'. If i search as Contact1 account1 then it should display record with that search key. I have shared my code below-

Component code-

SearchBar.cmp-

<aura:component>

    <div>
        <input type="text" class="form-control"
                placeholder="Search" onkeyup="{!c.searchKeyChange}"/>
    </div>

</aura:component>

contactList.cmp-

<aura:component controller="ContactListController1">

    <aura:attribute name="contacts" type="Contact[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="c:SearchKeyChange" action="{!c.searchKeyChange}"/>
    <ul>
        <aura:iteration items="{!v.contacts}" var="contact">
            <li>
                <a href="{! '#/sObject/' + contact.Id + '/view'}">
                    <p>{!contact.Name}</p>
                    <p>{!contact.Phone}</p>
                </a>
            </li>
        </aura:iteration>
    </ul>

</aura:component>

contactListController-

({
    doInit : function(component, event) {
        var action = component.get("c.findAll");
        action.setCallback(this, function(a) {
            component.set("v.contacts", a.getReturnValue());
        });
        $A.enqueueAction(action);
    },
    searchKeyChange: function(component, event) {
    var searchKey = event.getParam("searchKey");
    var action = component.get("c.findByName");
    action.setParams({
      "searchKey": searchKey
    });
    action.setCallback(this, function(a) {
        component.set("v.contacts", a.getReturnValue());
    });
    $A.enqueueAction(action);
}
})

contactListController1.apex-

public class ContactListController1 {
 @AuraEnabled
    public static List<Contact> findAll() {
        return [SELECT id, name, phone FROM Contact LIMIT 50];
    }

    @AuraEnabled
    public static List<Contact> findByName(String searchKey) {
        String name = '%' + searchKey + '%';
        return [SELECT id, name, phone FROM Contact WHERE name LIKE :name LIMIT 50];
    }

    @AuraEnabled
    public static Contact findById(String contactId) {
        return [SELECT id, name, title, phone, mobilephone, Account.Name
                    FROM Contact WHERE Id = :contactId];
    }
}

Please help. Component screenshot- enter image description here

  • What is your question / problem? it is not clear – Itai Shmida Mar 14 '17 at 7:20
  • Actually in above example i can search with Contact name but if i want to search with contact name along with account name then how can i modify my query? I hope i make you understand. – rani rube Mar 14 '17 at 9:06
  • do you want to search on the contact and also on the account? or on the accounts within contacts? eventually what result do you want? contact list? account list? 2 lists? – Itai Shmida Mar 14 '17 at 11:53
  • I need contact list only when i search with contact name along with its account name. – rani rube Mar 15 '17 at 2:17
0

So if I understand you correctly, you want to get all contacts that have a specific name and that their account have a specific name, that can be achieved like this:

String searchAccount = 'myacc';
String searchContact = 'mycon';

String soqlQuery = 'SELECT Id, Name, phone FROM Contact WHERE AccountId IN (SELECT Id FROM Account WHERE name LIKE \'%' + searchAccount + '%\') AND name LIKE \'%' + searchContact + '%\'';

List<Contact> contacts = Database.query(soqlQuery);

In this way you get all contacts that have 'mycon' in their name, and that their parent account have 'myacc' in their account name.

The use of dynamic soql (Database.query) is to insert a variable into your LIKE statement.

Is that what you needed?

EDIT

to take search string from one string - use split:

String searchString = 'myacc mycon';
List<String> splitedString = searchString.split(' ');


String soqlQuery = 'SELECT Id, Name, phone FROM Contact WHERE AccountId IN (SELECT Id FROM Account WHERE name LIKE \'%' + splitedString[0] + '%\') AND name LIKE \'%' + splitedString[1] + '%\'';
| improve this answer | |
  • Thanks Itai for your reply. But i have only one search textbox where i am entering contact name and account name together. So how can i pass searchAccount and searchContact ? Please see screenshot of my component. I have edited my question. – rani rube Mar 15 '17 at 6:19
  • if the two are supposed to be separated with a space - just split the string value - see how I've edited my answer, but keep in mind that you need to supervise on your input so you have space as the delimiter and no more than one space entered in the search box – Itai Shmida Mar 15 '17 at 6:31

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.