Update Object Fields After an update of related Object

Bonjour,

Lorsque je mets à jour les attributs de l’objet "SitesCity (Name ou PostalCode par exemple), et que j’enregistre, cette mise à jour n’est pas prise en compte dans le summary de l’adresse géographique.

J’ai donc mis en place un hook preSave dans l’objet City

``
@Override
public String preSave() {

	super.postSave();
	String cityRowId = this.getField("row_id").getValue();
	aga = getGrant().getTmpObject("SitesGeographicalAddress");
	agaTool = new BusinessObjectTool(aga);
	try {
		List<String[]> rowsAga = agaTool.search(new JSONObject().put("sitesAgaCitId", cityRowId));
		for (int i = 0; i < rowsAga.size() ; i++) {
			String[] row = rowsAga.get(i);
			String rowId = row[aga.getFieldIndex("row_id")];
			agaTool.getForUpdate(rowId);
			agaTool.update();
			agaTool.validateAndSave();
		} 
	} catch (Exception e) {
		//To Complete
	}
	return "";
}``

Après cette modification, en mettant à jour les attributs de city, et en faisant un Save, le summary de l’adresse géographique se met aussi à jour mais seulement en passant par l’interface Simplicité.
Cependant, en faisant une modification avec un PUT utilisant les APIs, le Summary ne se met pas à jour.

Pour info, voici comment le champs Summary est obtenu:
``

/** Hook override */
@Override
public void postLoad() {
	getField("sitesAdrHelper").setVisibility(ObjectField.VIS_FORM); // Helper field visible
	setTypeField("sitesAdrType");
	setTypeDefValue("GEOGRAPHICAL", true);
	setIdentifierField("sitesAdrIdentifier", "AGA", 8, /*"SITES_ADR_SEQUENCE"*/null);
}

/** Hook override */
@Override
public String preSave() {
	StringBuilder p = new StringBuilder();
	for (ObjectField f : getFields())
		if (f.getFullInput().startsWith("sitesAga") && !f.isEmpty() && f.getType()!=ObjectField.TYPE_ID && f.getType()!=ObjectField.TYPE_GEOCOORDS)
			p.append(p.length()==0 ? "" : ", ").append(f.getValue());
	ObjectField f = getField("sitesAdrSummary");
	f.setValue(p.substring(0, Math.min(p.length(), f.getSize())));
	return null;
}

``

Y a-t il une explication à ceci?

En vous remerciant

Une mise à jour via la UI ou via API ou autre passe par les mêmes couches du moteur Simplicite, donc dans les deux cas ça passe bien par votre hook preSave (d’ailleurs votre code devrait plutôt se trouver dans un postSave car là vous mettez à jour des infos dans un objet lié avant de savoir si l’objet d’origine a bien été mis à jour)

A mon avis vous devez avoir une exception, regardez les logs, mettez si besoin des try/catch additionnels dans vos hooks et, surtout, un AppLog.error(null, e, getGrant()); dans le catch sinon vous ratez peut être des choses.

En faisant les modifications suivantes:

  • Hook postSave

  • Remplacer le
    agaTool.getForUpdate(rowId); agaTool.update();agaTool.validateAndSave();
    Par aga.select(rowId);agaTool.validateAndSave();|
    Le problème est disparu ;)

``

@Override

public String postSave() {
	super.postSave();
	String cityRowId = this.getField("row_id").getValue();
	aga = getGrant().getTmpObject("SitesGeographicalAddress");
	agaTool = new BusinessObjectTool(aga);
	try {
		List<String[]> rowsAga = agaTool.search(new JSONObject().put("sitesAgaCitId", cityRowId));
		for (int i = 0; i < rowsAga.size() ; i++) {
			String[] row = rowsAga.get(i);
			String rowId = row[aga.getFieldIndex("row_id")];
			aga.resetFilters();
			aga.select(rowId);
			//agaTool.getForUpdate(rowId);
			//agaTool.update();
			agaTool.validateAndSave();
		} 

	} catch (Exception e) {
		//To Complete
	}
	return "";
}

``

Merci :)

Attention, le select (ou get) refait un select en base et passe par les hooks pre/postSelect.

La différence avec les getFor..., qui font la même chose, c’est l’appel des hooks init... (ex: le getForUpdate appelle le initUpdate).

Dans votre cas, s’il n’y a rien dans vos init... ou dans vos pre/postSelect (ou si vous voulez explicitement ne pas faire ce qui s’y trouve) vous pourriez simplifier/optimiser votre code en faisant:

for (String[] row : agaTool.search(...)) {
    aga.setValues(row, true);
    // aga.setFieldValue(..., ...)
    aga.validateAndSave();
}
1 Like