5 Replies Latest reply on Oct 18, 2019 11:53 AM by Srini Veeravalli

    HOW TO: Create a SOSL search service for ICRT

    New Member

      I have created an Apex service step that can be used to perform SOSL searches in Salesforce.com from ICRT. This can be used when:

      • You need more sophisticated search capabilities then the SOQL based query that ICRT provides.
      • You need to search encrypted fields. SOQL can not query encrypted fields. SOSL can.

       

      Sample code for a SOSL search of the Contact object is below. Once you compile it in Salesforce.com. you need to synchronized it with ICRT in the Guides Setup page (All Tabs -> Guides Setup).

      guides setup.jpg

       

      Note: The output of the service step below shows as a singular Contact record in ICRT. In order to display a list of contact is ICRT, you have to first create a temporary List of Contacts variable. Then you have to map it to the Search Result variable in an Assignment Step.

      /**

      *  This class implements a Web Service that can be invoked as an Automated Step from a Salesguide

      *  The interface supports two operations

      *     describe : returns service metadata for inclusion as a service step in the guide designer

      *     invoke : implements the service call at runtime

      */   

      global without sharing class icrtStep_ContactSearch implements icrt.IAeAutomatedStepService {

       

       

          // keep a static copy of the service description as this will not change    

          private static icrt.AeServiceDescription sDescription;

          static {

             sDescription = new icrt.AeServiceDescription();

             // namespace for this class

             sDescription.namespaceQualifier = icrt.AeServiceDescription.getNamespacePrefixForClass('icrtStep_ContactSearch');

             // name of this class

             sDescription.name = 'icrtStep_ContactSearch';

             // service name displayed to users

             sDescription.displayName = 'Contact Search';

             // description of service displayed to users

             sDescription.description = 'Performs a search of Contact records using SOSL';

            

             // add input parameters

             sDescription.addInputParameterDesc('Search Field', 'string', true);

            

             // Add search output parameter

             icrt.AeServiceDescription.AeServiceParameterDesc refParam = sDescription.addOutputParameterDesc('Search Results', 'reference');

             refParam.addParameterOption('referenceTo','Contact');

            

          }

         

          // Runtime service invocation   

          global icrt.AeServiceResponse invoke(icrt.AeServiceRequest aRequest) {

            

             boolean isSuccessful = false;

            

             // get a map of name/value pairs from request

             Map<String,Object> params = aRequest.getTypedParameterMap(sDescription.getInputParameterMap());

            

             // get parameters

             String searchField = (String) params.get('Search Field');

            

             System.debug('****searchField : ' + searchField);

            

             // create a response

             icrt.AeServiceResponse response = new icrt.AeServiceResponse();

            

             // verify that record type was sent

             if (searchField == null || searchField == '') {

           

                 response.addErrorInfo('Search Field is a required field.');

                

                 return response;

             }

            

             try {

            

                 // actual SOSL search

                 List<List<SObject>> searchList = [FIND :searchStr IN ALL FIELDS RETURNING Contact];

                           

                 // check in records returned

                 if (searchList == null || searchList.size() == 0) {

                    

                     response.addErrorInfo('No records were returned for these search parameters');

                 }

                 else {

                           

                     List<Contact> contactList = (List<Contact>) searchList[0];              

                     System.debug('****contactList: ' + contactList);

                    

                     String contIdLstStr;

                    

                     // loop through result to add to response list

                     for (Contact C: contactList) {

                    

                         System.debug('****Contact Id: ' + C.Id);

                        

                         if (contIdLstStr == null) {

                             contIdLstStr = C.Id;

                         }

                         else {

                             contIdLstStr += ';' + C.Id;

                         }

                     }

                     System.debug('****contIdLstStr : ' + contIdLstStr);

                    

                     response.addParameter('Search Results', contIdLstStr );

                 }

             }

             catch (Exception ex) {

            

                 response.addErrorInfo(ex.getMessage());

             }

       

       

             return response;

          }

         

          // Returns service metadata for inclusion in guide designer

          global icrt.AeServiceDescription describe() {

             return sDescription;

          }

      }