Unique number

cancel
Showing results for 
Search instead for 
Did you mean: 
ratik_singhal
Established Member

Unique number

Jump to solution

 I want to create a property (d:int) which would unique across the repository. 

e.g User cannot enter a number which already exists. It would display error that 'Duplicate Number'

I have another alternative that I can go for an autogenerated number but the customer did not agree on this. 

1 Solution

Accepted Solutions
ratik_singhal
Established Member

Re: Unique number

Jump to solution

I have solved this issue in a very effective manner. I have overridden the "form.post.json.js" file. 

Basically, I have to validate this before DML perform. 

try
{
if(itemId == "xm:ratik" ){
     var luceneQuery = "@xm\\:ratik:24";
     var result = search.luceneSearch(luceneQuery);
     if(result != ""){
             status.setCode(409, "duplicate..");
     return;
}
}
persistedObject = formService.saveForm(itemKind, itemId, repoFormData);

}

I have also tried with constraint handler but Ajax request is the problem in constraint handler.

I hope that i was able to explain.

View solution in original post

7 Replies
angelborroy
Alfresco Employee

Re: Unique number

Jump to solution

If you want to set the restriction at repository layer, you can use a constraint

https://docs.alfresco.com/community/concepts/metadata-model-contraints.html

https://blog.arvixe.com/custom-constrains-in-alfresco-model/

You can also add a JavaScript validation for Share UI in Form Field Validation

https://docs.alfresco.com/community/concepts/dev-extensions-share-form-field-validation-handlers.htm...

Hyland Developer Evangelist
krutik_jayswal
Senior Member II

Re: Unique number

Jump to solution

Its bit difficult.One approach would be like below.

Create a behavior/policy in alfresco(OnCreateNode).This policy should search the document with the value user has entered in the form for your specific unique property.If document has been found.Throw exception.So the new document will not get create with duplicate value.

how to implement policies and behavior , you can find more details on below link.

Behaviours / Policies | Alfresco Documentation 

Implementing Custom Behaviors in Alfresco | ECMArchitect | Alfresco Developer Tutorials 

ratik_singhal
Established Member

Re: Unique number

Jump to solution

I have used this approach in another situation but difficult was that when we are throwing an error from Java. We are not able to show error on Share UI because we are not adding any new share UI. 

e.g - I am adding a new Field on 'Create Folder' form like UniqueNumber (d:int) of This Folder.  So If I handle this in behavior on update/create policy. In that case, I can not show error on UI. 

krutik_jayswal
Senior Member II

Re: Unique number

Jump to solution

If we don't handle the exception in back end , it will throw exception in share UI as well.The only thing is , its bit of an ugly message string.If that is what you are worried about than.You can customize the field and add validate button.On click of validate display error message which you would like to have.

ratik_singhal
Established Member

Re: Unique number

Jump to solution

This field is getting added by apply aspect. So it would be difficult to modify out of the box 'Save' functionality 

krutik_jayswal
Senior Member II

Re: Unique number

Jump to solution

You do not need to modify save functionality define the custom control for your field.You only need to customize field.Refer below link for more details.

Providing a custom form control | Alfresco Documentation 

ratik_singhal
Established Member

Re: Unique number

Jump to solution

I have solved this issue in a very effective manner. I have overridden the "form.post.json.js" file. 

Basically, I have to validate this before DML perform. 

try
{
if(itemId == "xm:ratik" ){
     var luceneQuery = "@xm\\:ratik:24";
     var result = search.luceneSearch(luceneQuery);
     if(result != ""){
             status.setCode(409, "duplicate..");
     return;
}
}
persistedObject = formService.saveForm(itemKind, itemId, repoFormData);

}

I have also tried with constraint handler but Ajax request is the problem in constraint handler.

I hope that i was able to explain.