Skip to content

Instantly share code, notes, and snippets.

@dazoulay-simplicite
Last active Apr 6, 2021
Embed
What would you like to do?
//=====================================================
// In recent versions:
//=====================================================
try {
ObjectDB o = getGrant().getTmpObject("MyObject");
BusinessObjectTool ot = o.getTool();
// Get an existing record or an empty record based on functional keys filters
// False means no record was found => creation
if (!ot.getForUpdateOrCreate(new JSONObject() // or its alias getForUpsert
.put("myFunctionalKeyField1", ...)
.put("myFunctionalKeyField2", ...)
...
) {
// Set functional keys fields
o.setFieldValue("myFunctionalKeyField1", ...);
o.setFieldValue("myFunctionalKeyField2", ...);
...
}
// Do something, e.g. set values:
setFieldValue("myField1", ...);
setFieldValue("myField2", ...);
...
ot.validateAndSave();
} catch (Exception e) {
AppLog.error(e, getGrant());
// Do something with the exception
}
//=====================================================
// In olrder versions:
//=====================================================
try {
ObjectDB o = getGrant().getTmpObject("MyObject");
BusinessObjectTool ot = new BusinessObjectTool(o);
// Search using functional keys filters
List<String[]> rs = ot.search(new JSONObject()
.put("myFunctionalKeyField1", ...)
.put("myFunctionalKeyField2", ...)
...
); /* or
List<String[]> rs = ot.search(new HashMap<String, String>() {{
put("myFunctionalKeyField1", ...);
put("myFunctionalKeyField2", ...);
...
}}); */
if (rs.size() == 0) { // 0 found => create
ot.getForCreate(); // or o.resetValues(true);
// Set functional keys fields
o.setFieldValue("myFunctionalKeyField1", ...);
o.setFieldValue("myFunctionalKeyField2", ...);
...
} else if (rs.size() == 1) // 1 found = update
ot.getForUpdate(o.getRowId(rs.get(0))) // or o.setValues(rs.get(0), true);
else // Should never happen if search is done using all functional keys
throw new Exception(rs.size() + " records found");
// Do something, e.g. set other fields values:
setFieldValue("myField1", ...);
setFieldValue("myField2", ...);
...
ot.validateAndSave();
} catch (Exception e) {
AppLog.error(e, getGrant());
// Do something with the exception
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment