CMDBuild Forum

Register an import date during a file import job

Dear community or Technoteca engineers,

We have created an file import job and would like to have our “ImportDate” field to automatically receive a value of the date on which the import took place. To get this working, we have given the field additional properties in the “Auto value” field using this sniplet of JavaScript:
`
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = ‘0’ + dd;
}
if (mm < 10) {
mm = ‘0’ + mm;
}
var today = yyyy + ‘-’ + mm + ‘-’ + dd;
api.setValue(today);

`
With this script we have configured the field in such a way that it receives a standard date value. But to our surprise, this only works for manual input. This value is not being set during a file import.

How can we configure our field properties so that it also fills in a date when we import a file as well?

Kind regards,

Frans Erich

I’ve goth this answer to this quesion:

the field features (validation rules, visibility, and auto-value) work only for manual inputs from the web interface.

To implement the same behavior for a job or APIs depends on the specific requirement.

In the following some suggestions.

  1. A SQL trigger on the DB that is the strongest method always applied in any case

  2. At the end of this post you find an example, To apply it on the DB you have to use the cmdbuild user, not the postgres (admin) one

  3. Please, be always careful when you perform operations directly on the DB since those activities and the possible related issues are not covered by the maintenance

  4. Include also the date field within the import job template

  5. This way you can possibly set the current date directly in the template (but it depends on the import you configure)

  6. This strategy works only for the job and acts in parallel with respect to the auto-value that acts only for the interface

  7. Configure a service bus gate as an advanced descriptor that manages the import

  8. This strategy is extremely powerful and allows you to perform also complex operations

  9. You have to configure a YAML descriptor to manage it properly

I hope this can help.

Kind regards.

Alberto Pittolo

---------------trigger example-------------------------------------
CREATE OR REPLACE FUNCTION public.my_trigger_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.“CMDBUILD_FIELD_NAME” = current_timestamp;
RETURN NEW;
END;
$function$;

CREATE TRIGGER my_trigger_function
AFTER INSERT OR UPDATE
ON public.“CMDBUILD_CLASS_NAME”
FOR EACH ROW
EXECUTE FUNCTION my_trigger_function();

---------------end of trigger example-------------------------------------

1 Like