-
-
Save dazoulay-simplicite/ee17577dc228babcd405b0fed165f80f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//===================================================== | |
// 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