Objects in SuprSend represent non-user entities such as organizations, teams , roles, and projects. Understand more about objects from our objects documentation

Upsert (create/update) an object

Object updating is an upsert function, meaning it would always override existing key values on further updates. Object id and type is mandatory to create object. You can optionally pass object properties (to use in template or workflow condition) or channel information (send notification on object channels) in the payload.

import org.json.JSONObject;
import suprsend.Suprsend;

public class ObjectEdit {
  public static void main(String[] args) throws Exception {
    objectEditInstance();
  }

  public static void getSuprClient() throws Exception {
    Suprsend suprsendClient = new Suprsend("_workspace_key_", "_workspace_secret_");
    return suprsendClient;
  }

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();
    // Instiantiate object
    String objectType = "departments";
    String objectId = "engineering";

    // Object Payload to upsert properties and communication channels
    JSONObject payload = new JSONObject()
      .put("k1", "value1");
      .put("$email", "devs@abc.com");

    JSONObject object = suprClient.objects.upsert(objectType, objectId, payload);
    System.out.println(object);
  }
}

Edit an object

There are 2 ways in which you can edit an object data.

  • Build edit payload yourself
  • Use helper methods provided by SDK (Recommended)

1. Build edit payload yourself

Use this to modify an object, typically for removing channels or unsetting properties. The payload will follow the same structure as the Object Edit API.

import org.json.JSONObject;
import suprsend.Suprsend;


public class ObjectEdit {

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();
    
    // Instiantiate object
    String objectType = "departments";
    String objectId = "engineering";

    // Object Payload
    JSONObject payload = new JSONObject().put("operations",
				new JSONObject[] { new JSONObject().put("$set", new JSONObject().put("k2", "value2")) });
    
		JSONObject object = suprClient.objects.edit(objectType, objectId, payload);
		System.out.println(object);
  }
}

2. Edit using helper methods [Recommended]

It is possible to use the SDK’s helper methods to perform edit operations on an object.

For this, first create object instance then call any of the helper methods mentioned below and finally save the changes.

import org.json.JSONObject;
import suprsend.Suprsend;
import suprsend.ObjectEdit;

public class ObjectEdit {

  public static void objectEditInstance() throws Exception {
    Suprsend suprsendClient = getSuprClient();

    //Fetch Object Instance
    String objectType = "departments";
    String objectId = "engineering";
    ObjectEdit object_ins = suprClient.objects.getInstance(objectType, objectId);

    // Call object update methods
    object_ins.set("company_name", "ABC company");
    object_ins.setTimezone("America/Los_Angeles");

    // Call edit api
    JSONObject res = suprClient.objects.edit(object_ins);
    System.out.println(res);
  }
}

List Objects

List objects for an object_type. You can also pass listing options in the payload which includes limit,before,after


String objectType = "__objecttype__";

// optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
HashMap<String, Object> opts = new HashMap<String, Object>() {
  {
    put("limit", 10);
    put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
  }
};


JSONObject res = suprClient.objects.list(objectType, opts);
System.out.println(res);

Get Object Details


String objectType = "__objecttype__";
String objectId = "__objectid__";

JSONObject res = suprClient.objects.get(objectType, objectId);
System.out.println(res);

Create Subscriptions


String objectType = "__objecttype__";
String objectId = "__objectid__";
JSONObject payload = new JSONObject().put("recipients",
    Arrays.asList("__recipient_id_1__", new JSONObject().put("distinct_id", "__recipient_id_2__"),
        new JSONObject().put("object_type", objectType).put("id", objectId)));

JSONObject res = suprClient.objects.createSubscriptions(objectType, objectId, payload);
System.out.println(res);

List Subscriptions


String objectType = "__objecttype__";
String objectId = "__objectid__";

// optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
HashMap<String, Object> opts = new HashMap<String, Object>() {
  {
    put("limit", 10);
    put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
  }
};

JSONObject res = suprClient.objects.getSubscriptions(objectType, objectId, opts);
System.out.println(res);

Remove Object Subscription


String objectType = "__objecttype__";
String objectId = "__objectid__";
JSONObject payload = new JSONObject().put("recipients",
    Arrays.asList("__recipient_id_1__", new JSONObject().put("distinct_id", "__recipient_id_2__"),
        new JSONObject().put("object_type", "__objecttype__").put("id", "__objectid_child_")));

JSONObject res = suprClient.objects.deleteSubscriptions(objectType, objectId, payload);
System.out.println(res);

Get list of objects subscribed by object

An object can subscribe to other objects. Use this method to get the list of all objects that the current object has subscribed to.


String objectType = "__objecttype__";
String objectId = "__objectid__";

// optional params to pass limit (default=10) and cursor pointer for fetching the next set of results
HashMap<String, Object> opts = new HashMap<String, Object>() {
  {
    put("limit", 10);
    put("after", "01HFS04E4J29KHPYRK7HT3YQQ5");
  }
};

JSONObject res = suprClient.objects.getObjectsSubscribedTo(objectType, objectId, opts);
System.out.println(res);

Delete Object


String objectType = "__objecttype__";
String objectId = "__objectid__";

JSONObject res = suprClient.objects.delete(objectType, objectId);
System.out.println(res);

Bulk Delete Object


String objectType = "__objecttype__";
JSONObject payload = new JSONObject().put("object_ids", Arrays.asList("__objectid__"));

JSONObject res = suprClient.objects.bulkDelete(objectType, payload);
System.out.println(res);