Create a Global Function for salesMLQuerySimilarRecords API

Here's how you create a global function for the salesMLQuerySimilarRecords API:

  1. Continue in the active sandbox and navigate to Configurations > Application Composer > Common Setup > Global Functions.
  2. Click the global function icon.
  3. On the Create Global Function page, enter a Function Name. For example, invokeSalesMLSimilarRecordsQueryAPI.

    Ensure that you use same global function name in the Groovy for the formulae type field.

  4. Select String for Returns.
  5. Add these parameters:
    • Name: useCaseCode Type: String.
    • Name: objectPKName Type: String.
    • Name: recordPKValue Type: Long (Integer).

    Create a global function for the similar accounts query API.
  6. Add this Groovy code in the Edit Script field:
    def inputMap = [:];
    def accountNames = "";
    def debugLog = "";
    if(recordPKValue != null) {
      try {
        debugLog += "Primary Key:"+recordPKValue;
        def wsRequest = adf.webServices.salesMLQueryAPISimilarRecords;
        def httpHeaders = ['Content-Type': 'application/json', 'Accept-Encoding': 'text/plain']
        wsRequest.requestHTTPHeaders = httpHeaders;
        inputMap.put("UseCaseCode",  useCaseCode);
        inputMap.put("QueryMetadata",  "whereClause:".concat(objectPKName).concat("=").concat(recordPKValue.toString()).concat(";numberOfRows:5"));
        def partyIds = "";
        debugLog += "GOING TO INVOKE SALESML API: ";
        def wsResponse = wsRequest.POST(inputMap);
        debugLog += "SALESML API INVOKED: ";
        for(similarAccount in wsResponse) {
          if(partyIds == "") {
            partyIds += similarAccount.PARTYID;
          } else {
            partyIds += ","+similarAccount.PARTYID;
          }
        }
        if(partyIds != "") {
          debugLog += "START ACCOUNTS GET PREP: ";
          def wsRequest2 = adf.webServices.Acconts_GET;
          def httpHeaders2 = ['REST-Framework-Version': '2'];
          wsRequest2.requestHTTPHeaders = httpHeaders2;
          def queryParams = ['q':'PartyId IN ('+partyIds+')'];
          wsRequest2.dynamicQueryParams = queryParams;
          debugLog += "GOING TO INVOKE ACCOUNTS GET: ";
          def accounts = wsRequest2.GET();
          debugLog += "INVOKED ACCOUNTS GET: ";
          def count = 0;
          for(account in accounts.items) {
            if(count == 0) {
              accountNames += account.OrganizationName;
            } else {
              if(count<5){
                accountNames += ",\n"+account.OrganizationName;
              } else {
                break;
              }
            }
            count++;
          } 
        }
      } catch (e) {
        debugLog += e.getMessage();
        accountNames = debugLog;
      }
    } else {
      accountNames = "Not Available";
    }
     
    if(accountNames == "") {
      accountNames = "Not Available";
    }
     
    return accountNames;
    
  7. Click Validate.
    Enter the Groovy script for the gobal function.
  8. Click Save and Close.
    The Groovy may show some warnings. Ignore those warnings and click Yes to save the script.
    Ignore the warnings and save the script.