@Override
  public void execute(FunctionContext<Object> context) {
    List<Object> argsList = new ArrayList<>();
    Object[] argsArray;

    if (context.getArguments() instanceof Object[]) {
      argsArray = (Object[]) context.getArguments();
      argsList = Arrays.asList(argsArray);
    } else if (!((context.getArguments() instanceof Boolean)
        || (context.getArguments() instanceof Vector)
        || (context.getArguments() instanceof String))) {
      System.out.println("AddFreeItemToOrders : Invalid Arguments");
    }

    InternalCache cache;
    Region<Object, Order> region;
    try {
      cache = (InternalCache) CacheFactory.getAnyInstance();
      cache.getCacheConfig().setPdxReadSerialized(true);
      region = cache.getRegion("orders");
    } catch (CacheClosedException ex) {
      List<Object> vals = new ArrayList<>();
      vals.add("NoCacheFoundResult");
      context.getResultSender().lastResult(vals);
      throw ex;
    }

    String oql =
        "SELECT DISTINCT entry.key FROM " + SEPARATOR
            + "orders.entries entry WHERE entry.value.totalPrice > $1";
    Object[] queryArgs = new Object[1];
    queryArgs[0] = argsList.get(0);

    final Query query = cache.getQueryService().newQuery(oql);

    SelectResults<?> result;
    List<Object> keys = new ArrayList<>();
    try {
      result = (SelectResults<?>) query.execute(queryArgs);
      if (result != null) {
        keys.addAll(result);
      }
    } catch (FunctionDomainException e) {
      cache.getLogger()
          .info("Caught FunctionDomainException while executing function AddFreeItemToOrders: "
              + e.getMessage());
    } catch (TypeMismatchException e) {
      cache.getLogger()
          .info("Caught TypeMismatchException while executing function AddFreeItemToOrders: "
              + e.getMessage());
    } catch (NameResolutionException e) {
      cache.getLogger()
          .info("Caught NameResolutionException while executing function AddFreeItemToOrders: "
              + e.getMessage());
    } catch (QueryInvocationTargetException e) {
      cache.getLogger().info(
          "Caught QueryInvocationTargetException while executing function AddFreeItemToOrders"
              + e.getMessage());
    }

    // class has to be in classpath.
    try {
      Item it = (Item) (argsList.get(1));
      for (Object key : keys) {
        Object obj = region.get(key);
        if (obj instanceof PdxInstance) {
          PdxInstance pi = (PdxInstance) obj;
          Order receivedOrder = (Order) pi.getObject();
          receivedOrder.addItem(it);

          region.put(key, receivedOrder);
        }
      }

      context.getResultSender().lastResult("success");
    } catch (Exception e) {
      context.getResultSender().lastResult("failure");
    }
  }
Gemfire function to add free items in the existing order if the total price for that order is
greater then the argument
geode-web-api/src/integrationTest/java/org/apache/geode/rest/internal/web/controllers/AddFreeItemToOrders.java