Community Tip - You can change your system assigned username to something more personal in your community settings. X
This script will get all contacts (optionally limited to a particular organization) and check whether there is a DeviceContact associated with it. If there is no DeviceContact (meaning it is not associated with a device), it deletes the contact.
Note - It is worthwhile to test this script by commenting out the contact.delete() line first and reviewing which contacts will be deleted. Also, this script works by finding all contacts, therefore it is not recommended to run the script repeatedly within a short period of time.
Parameter:
import net.sf.json.JSONObject
import com.axeda.drm.sdk.device.DeviceFinder
import com.axeda.drm.sdk.device.Device
import com.axeda.drm.sdk.device.ModelFinder
import com.axeda.drm.sdk.device.Model
import com.axeda.drm.sdk.data.CurrentDataFinder
import com.axeda.drm.sdk.data.DataValue
import net.sf.json.groovy.JsonSlurper
import com.axeda.drm.sdk.contact.Contact
import com.axeda.drm.sdk.contact.ContactFinder
import com.axeda.drm.sdk.contact.Location
import com.axeda.drm.sdk.contact.LocationFinder
import com.axeda.drm.sdk.contact.OrganizationFinder
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.contact.Organization
import com.axeda.drm.sdk.contact.DeviceContact
import com.axeda.drm.sdk.contact.ContactMethodType
import com.axeda.drm.sdk.contact.DeviceContactFinder
import groovy.json.*
import com.axeda.drm.sdk.scripto.Request
import com.axeda.common.sdk.id.Identifier
/**
* ContactDelete.groovy
* -----------------------
*
* Finds all contacts, then finds the device contact for each contact. If null, deletes the contact.
*
* @params
* organizationName (OPTIONAL) Str - limit the contact deletion to an organization
*
*
* @author Sara Streeter <sstreeter@axeda.com>
*/
/**
* initialize our global variables
* json = the contents of our response
* infoString = a stringBuilder used to collect debug information during the script
* contentType = the content type we will return
* scriptname = The name of this Script, used in multiple places
*/
def json = new groovy.json.JsonBuilder()
def infoString = new StringBuilder()
def contentType = "application/json"
def scriptName = "ContactDelete.groovy"
def root = ["result":["deleted":[]]]
def timings = [:]
timings.contactFinding = 0
timings.contactIterating = 0
wholestart = System.currentTimeMillis()
final Context CONTEXT = Context.getSDKContext()
try {
def params = Request?.parameters?.size() > 0 ? Request?.parameters : parameters
ContactFinder cfinder = new ContactFinder(CONTEXT)
def start = System.currentTimeMillis()
def organization
if (params.organizationName != null && params.organizationName != ""){
OrganizationFinder oFinder = new OrganizationFinder(CONTEXT)
oFinder.setName(params.organizationName)
organization = oFinder.find()
if (organization){
cfinder.setOrganization(organization)
}
}
List<Contact> contacts = cfinder.findAll()
timings.contactFinding += System.currentTimeMillis()-start
root.result.contactSize = contacts.size()
start = System.currentTimeMillis()
contacts.each{ contact ->
DeviceContactFinder dcfinder = new DeviceContactFinder(CONTEXT)
dcfinder.setContactId(contact.id)
def dc = dcfinder.findAll()
if (dc.size() == 0){
root.result.deleted << [
id: contact.id.value,
firstName: contact.firstName,
lastName: contact.lastName,
organization: contact.organization?.name
]
contact.delete() // comment out this line to check which contacts will be deleted first.
}
}
timings.contactIterating += System.currentTimeMillis()-start
} catch (Exception e) {
processException(scriptName,json,e)
}
finally {
timings.wholescript = System.currentTimeMillis() - wholestart
root += [timings: timings]
}
return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(root).toString(2)]
/*
Processes the contents of an Exception and add it to the Errors collection
@param json The markup builder
*/
private def processException(String scriptName, JsonBuilder json, Exception e) {
// catch the exception output
def logStringWriter = new StringWriter()
e.printStackTrace(new PrintWriter(logStringWriter))
logger.error("Exception occurred in ${scriptName}: ${logStringWriter.toString()}")
/*
Construct the error response
- errorCode Will be an element from an agreed upon enum
- errorMessage The text of the exception
*/
json.errors {
error {
message "[${scriptName}]: " + e.getMessage()
timestamp "${System.currentTimeMillis()}"
}
}
return json
}