[SalesForce] How to return fields to webservice response

When the user enters his name from third party application, The user needs to get all the records(Account(name,Phone,email),Contact(firstname,Lastname),Lead(email,mobile number)) matching the text entered by him.

I have used sosl for this purpose.I am able to get name of all accounts,contacts,leads.

I need to show the respective fields(Account(name,Phone,email),Contact(firstname,Lastname),Lead(email,mobile number)) of different objects.

Here follows my Webservice code:

global class exposewsdl{
global class requestKT{
    webservice string inputName;

}
global class responseOfWs{
    webservice String [] accounts = new List<String>();
    webservice String [] contacts = new List<String>();
    webservice String [] leads = new List<String>();
    webservice string errorFound;
}
webservice static responseOfWs getCaseDetailsFromcsNum(requestKT csNum){
    try{
        List<Lead> leadList =New List<Lead>();
         List<contact> conList= New List<contact>();
         List<account> accList=New List<account>();

         String searchStr1 = '*'+csNum.inputName+'*';
         String searchQuery = 'FIND \'' +searchStr1  + '\' IN ALL FIELDS RETURNING  Account (Name),Contact(name),Lead(name)';
        List<List <sObject>> searchList = search.query(searchQuery);
        accList = ((List<Account>)searchList[0]);
        conList  = ((List<contact>)searchList[1]);
        leadList = ((List<Lead>)searchList[2]);
        responseOfWs res = new responseOfWs();
        for (Integer i = 0; i<accList.size(); i++) {
        res.accounts.add(accList[i].name);
        }    
        for (Integer i = 0; i < conList.size(); i++) {
         res.contacts.add(conList[i].name);
        }
        for (Integer i = 0; i < leadList.size(); i++) {
        res.leads.add(leadList[i].name);
        }
        return res;
    }catch(Exception e){
        responseOfWs res = new responseOfWs();
        res.errorFound = e.getMessage();
        return res;
    }
}

}

Please me to show these field values also. Thanks

Best Answer

Your query should read like this:

String searchQuery = 'FIND \'' +searchStr1  + '\' IN ALL FIELDS RETURNING  Account 
    (Name,Phone,Email),Contact(FirstName,Lastname),Lead(Email,MobilePhone)';

You'll then need to make the appropriate adjustments in your code when you iterate on what's returned for each object to pull out each of the fields for each object recordId.

Related Topic