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

    if (context.getArguments() instanceof Boolean) {

    } else if (context.getArguments() instanceof String) {
      String arg = (String) context.getArguments();
    } else if (context.getArguments() instanceof Vector) {

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

    InternalCache cache = null;
    try {
      cache = (InternalCache) CacheFactory.getAnyInstance();
      cache.getCacheConfig().setPdxReadSerialized(true);
      region = cache.getRegion("orders");
    } catch (CacheClosedException ex) {
      vals.add("NoCacheFoundResult");
      context.getResultSender().lastResult(vals);
    }

    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 = null;
    try {
      result = (SelectResults) query.execute(queryArgs);
      int resultSize = result.size();

      if (result instanceof Collection<?>) {
        for (Object item : result) {
          keys.add(item);
        }
      }
    } catch (FunctionDomainException e) {
      if (cache != null) {
        cache.getLogger()
            .info("Caught FunctionDomainException while executing function AddFreeItemToOrders: "
                + e.getMessage());
      }

    } catch (TypeMismatchException e) {
      if (cache != null) {
        cache.getLogger()
            .info("Caught TypeMismatchException while executing function AddFreeItemToOrders: "
                + e.getMessage());
      }
    } catch (NameResolutionException e) {
      if (cache != null) {
        cache.getLogger()
            .info("Caught NameResolutionException while executing function AddFreeItemToOrders: "
                + e.getMessage());
      }
    } catch (QueryInvocationTargetException e) {
      if (cache != null) {
        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 (ClassCastException e) {

      context.getResultSender().lastResult("failure");
    } 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-assembly/geode-assembly-test/src/main/java/org/apache/geode/rest/internal/web/controllers/AddFreeItemToOrders.java