Trigger Update
note
This action only works for API keys that have a test prefix
To make integration easy, Complero provides an endpoint to create an Update for an existing Contact. The event contains the exact data transmitted, so they can fit to the exact test scenarios.
In the following example we will upload a contact, trigger an update for that contact and then get the latest events.
src/main/java/com/complero/examples/CheckForEvents.java
package com.complero.examples;
import com.complero.api.ContactsApi;
import com.complero.api.EventsApi;
import com.complero.api.OperationsApi;
import com.complero.api.models.*;
import com.complero.invoker.ApiClient;
import com.complero.invoker.ApiException;
import com.complero.invoker.ApiResponse;
import com.complero.invoker.auth.ApiKeyAuth;
import com.complero.invoker.auth.Authentication;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class CheckForEvents {
public static void main(String[] args) {
ApiClient apiClient = createApiClient(System.getenv("API_KEY"));
// this is the part where you query your own database Contacts
List<ContactUpload> contacts = collectContactsToUpload();
try {
String operationId = uploadContacts(contacts, apiClient);
Operation operation = waitForOperation(operationId, apiClient);
// in Production, you would now be waiting for people to fill out the webform,
// which would then trigger update/confirm events
// in Development you can simulate such update/confirm events via the
// triggerSandboxEvent endpoint
EventsApi eventsApi = new EventsApi(apiClient);
ApiResponse<WebResponse> response = triggerSandboxEvent(eventsApi);
// Sandbox Event was triggered, let's look at the response message
System.out.println(response.getData().getMessage());
List<ContactEvent> event = getLatestEvent(eventsApi);
// let's have a look at the returned event
System.out.println("Event Type: " + event.get(0).getType());
System.out.println(event.get(0).toString());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// We will demonstrate a failing upload because a validation error is returned
List<ContactUpload> contactsThatFailValidation = collectFailingValidationContactsToUpload();
try {
uploadContacts(contactsThatFailValidation, apiClient);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
static ApiResponse<WebResponse> triggerSandboxEvent(EventsApi eventsApi) throws ApiException {
TriggerPublicEvent triggerPublicEvent = new TriggerPublicEvent();
// Here we'll simulate the Contact updating their contact information
Contact contact1 = new Contact();
ContactSection contactSection = new ContactSection();
Address address = new Address();
List<String> emails = new ArrayList<>();
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setId("api-sandbox-1");
contact1.setEmployeeId("1");
address.setZipCode("53111");
address.setCountry("DE");
address.setCity("Bonn");
address.setStreet("Friedrichsstraße");
contactSection.addAddressesItem(address);
emails.add("edcba@gmail.com");
emails.add("google@gmail.com");
contactSection.setEmails(emails);
contact1.setSectionPrivate(contactSection);
triggerPublicEvent.setContact(contact1);
// Set type of the event (update,confirm)
// update if data was changed
// confirm if no data was changed but the Contact simply confirmed that the current data is correct.
triggerPublicEvent.setType(TriggerPublicEvent.TypeEnum.UPDATE);
// call API to trigger Sandbox Event
ApiResponse<WebResponse> response = eventsApi.triggerSandboxEventWithHttpInfo(triggerPublicEvent);
return response;
}
static List<ContactEvent> getLatestEvent(EventsApi eventsApi) throws ApiException {
// call API to retrieve the latest events
return eventsApi.getLatestEvents("update", "1");
}
static ApiClient createApiClient(String apiKey) {
ApiClient apiClient = new ApiClient();
// set auth
Authentication auth = apiClient.getAuthentications().get("ApiKeyAuth");
((ApiKeyAuth) auth).setApiKey("Bearer ".concat(apiKey));
return apiClient;
}
static List<ContactUpload> collectFailingValidationContactsToUpload() {
List<ContactUpload> uploadContacts = new ArrayList<>();
ContactUpload contact1 = new ContactUpload();
Section section = new Section();
List<Email> emails = new ArrayList<Email>();
contact1.setId("1-failing-validation");
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setEmployeeId("1");
emails.add(new Email().email("@gmail.com")); // <-- Invalid email
section.setEmails(emails);
contact1.setSectionPrivate(section);
uploadContacts.add(contact1);
return uploadContacts;
}
static List<ContactUpload> collectContactsToUpload() {
List<ContactUpload> uploadContacts = new ArrayList<>();
ContactUpload contact1 = new ContactUpload();
Section section = new Section();
Address address = new Address();
List<Email> emails = new ArrayList<>();
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setId("api-sandbox-1");
contact1.setEmployeeId("1");
address.setZipCode("53111");
address.setCountry("DE");
address.setCity("Bonn");
address.setStreet("Friedensplatz");
section.addAddressesItem(address);
emails.add(new Email().email("abcde@gmail.com"));
emails.add(new Email().email("gmail@gmail.com"));
section.setEmails(emails);
contact1.setSectionPrivate(section);
uploadContacts.add(contact1);
return uploadContacts;
}
static String uploadContacts(List<ContactUpload> contacts, ApiClient apiClient) {
ContactsApi contactsApi = new ContactsApi(apiClient);
// Uploading contacts
ApiResponse<Void> apiResponse;
try {
apiResponse = contactsApi.bulkUploadContactsWithHttpInfo(contacts);
} catch (ApiException e) {
throw new RuntimeException("Error when uploading contacts", e);
}
System.out.println("HTTP POST request succeeded! Status Code: " + apiResponse.getStatusCode());
System.out.println("Headers: " + apiResponse.getHeaders());
Map<String, List<String>> headers = apiResponse.getHeaders();
// retrieve operation-id from header, so we can check in on the ongoing
// operation
String operationID = headers.get("operation-id").get(0);
return operationID;
}
static Operation waitForOperation(String operationID, ApiClient apiClient) {
OperationsApi operationsApi = new OperationsApi(apiClient);
Operation operation;
System.out.println("OperationID: " + operationID);
boolean statusChecking = true;
int tries = 0;
do {
try {
// inspect the progress of the batch operation
operation = operationsApi.getOperation(operationID);
} catch (ApiException ex) {
throw new RuntimeException("Error during HTTP GET request: " + ex.getMessage());
}
System.out.println("HTTP GET request succeeded! Status Code: " + operation.getStatus());
switch (Objects.requireNonNull(operation.getStatus())) {
case "pending":
System.out.println("Status is 'pending'. Attempt number: " + ++tries);
sleep(3000);
break;
case "success":
System.out.println("Status is 'success'. Ending loop.");
statusChecking = false;
break;
case "error":
// api returned that an error occurred for the upload
throw new RuntimeException("Status is 'error'. Ending loop.");
default:
throw new RuntimeException("Unknown status: " + operation.getStatus());
}
} while (statusChecking);
return operation;
}
static void sleep(int ms) {
System.out.println("Repeating after " + ms + "ms...");
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
- Command
- Output
mvn clean install exec:java -Dexec.mainClass="com.complero.examples.CheckForEvents"
...
OperationID: c7742f77-8025-4bc1-a3e3-49e668e7a23b
HTTP GET request succeeded! Status Code: pending
Status is 'pending'. Attempt number: 1
Repeating after 3000ms...
HTTP GET request succeeded! Status Code: success
Status is 'success'. Ending loop.
event triggered successfully
Event Type: update
class ContactEvent {
contact: class Contact {
birthday: 0001-01-01
company: null
data: {}
degree: null
documentation: class Documentation {
consents: []
}
employeeId: 1
firstName: Petra
id: api-sandbox-1
language: de_DE
lastName: Bauer
nameSuffix: null
nationality: null
position: null
salutation: null
sectionPrivate: class ContactSection {
addresses: [class Address {
addition: null
city: Bonn
country: DE
district: null
extra1: null
extra2: null
extra3: null
houseNumber: null
street: Friedrichsstraße
zipCode: 53111
}]
emails: [edcba@gmail.com, google@gmail.com]
faxNumbers: []
mobileNumbers: []
phoneNumbers: []
}
sectionWork: class ContactSection {
addresses: []
emails: []
faxNumbers: []
mobileNumbers: []
phoneNumbers: []
}
tags: []
title: null
website: null
}
contactId: api-sandbox-1
eventDocumentationUrl: null
eventId: 31c0bb6e-66d0-4a36-8a6f-52f3711b0210
eventTime: 2025-09-02T09:27:12Z
type: update
}
com.complero.invoker.ApiException: Message:
HTTP response code: 422
HTTP response body: {"message":"there was a problem with the validation of the following","validation_errors":{"1-failing-validation":{"email[0]":{"code":"v1c004","message":"email is not a valid"}}}}
HTTP response headers: {content-type=[application/json], date=[Tue, 02 Sep 2025 09:27:13 GMT], request-id=[YJnvNwDSqHeHlkGqRqKUUwWBGlXHTnqJ], strict-transport-security=[max-age=15724800; includeSubDomains], vary=[Accept-Encoding, Origin]}
at com.complero.invoker.ApiClient.handleResponse(ApiClient.java:1131)
at com.complero.invoker.ApiClient.execute(ApiClient.java:1044)
at com.complero.invoker.ApiClient.execute(ApiClient.java:1027)
at com.complero.api.ContactsApi.bulkUploadContactsWithHttpInfo(ContactsApi.java:187)
at com.complero.examples.CheckForEvents.uploadContacts(CheckForEvents.java:157)
at com.complero.examples.CheckForEvents.main(CheckForEvents.java:50)
at org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:371)
at org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:360)
at org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:280)
at java.base/java.lang.Thread.run(Thread.java:1447)
Error: Error when uploading contacts
...