Announcement

Collapse
No announcement yet.

Checking if duplicate before save

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking if duplicate before save

    Hello,

    My $name is equal to two different fields. I am still able to save duplicated, it looks like API Before Save Script is executed before Before Save Custom Script​.
    What am I doing wrong?

    'MyEntityType' should be this replaced with name of entity ? if is not im receivieng error 500​​

    Field name is not required and not visible in form.

    Before Save Custom Script​
    name = string\concatenate(taxYearRelated, ' - ', monthRelated)


    API Before Save Script
    if (entity\isNew() && !recordService\skipDuplicateCheck()) {
    $id = record\findOne('MyEntityType', null, null, 'name=', name);

    if ($id) {
    recordService\throwDuplicateConflict($id);
    }
    }​


    Please help.

  • #2
    Can't give you a solution, but why don't you just use the build-in Duplicate field checker that is added in v8? Save you the hassle of writing formula nowadays.

    But I see that you doing a concatenate, so perhaps that the reason why you need to use Formula?

    Comment


    • #3
      Thank you, I found build-in duplicate field checke, but when my $name is created by script "name = string\concatenate(taxYearRelated, ' - ', monthRelated)", duplicate checker doesn't work.

      Comment


      • #4
        Originally posted by diilmac View Post
        Hello,

        My $name is equal to two different fields. I am still able to save duplicated, it looks like API Before Save Script is executed before Before Save Custom Script​.
        What am I doing wrong?

        'MyEntityType' should be this replaced with name of entity ? if is not im receivieng error 500​​

        Field name is not required and not visible in form.

        Before Save Custom Script​
        name = string\concatenate(taxYearRelated, ' - ', monthRelated)


        API Before Save Script
        if (entity\isNew() && !recordService\skipDuplicateCheck()) {
        $id = record\findOne('MyEntityType', null, null, 'name=', name);

        if ($id) {
        recordService\throwDuplicateConflict($id);
        }
        }​


        Please help.
        Hey diilmac ​,

        The reason why it doesn't work is because the name is not constructed yet therefore the name value is null at this stage as it is not provided by UI, in order to solve this issue, here is what you need to use in API Before Save Script, a simple trick will work:
        PHP Code:
        if (entity\isNew() && !recordService\skipDuplicateCheck()) {
            
        $id record\findOne('MyEntityType'nullnull'name='string\concatenate(taxYearRelated' - 'monthRelated));

            if (
        $id) {
                
        recordService\throwDuplicateConflict($id);
            }
        }


        // To forbid bypass duplicate checking, you can use this piece of code, which will throw an error when the user click create
        if (recordService\skipDuplicateCheck()) {
        recordService\throwForbidden("No duplicate check bypass allowed.");
        }
        ​ 

        And yes (MyEntityType) should be replaced with the name of the entity.

        This way you are checking against what makes the name field meaning the composition of the two fields provided by the UI. This should do the job.

        Hope this helps
        Last edited by rabii; 09-28-2023, 03:28 PM.
        Rabii
        Web Dev

        Comment

        Working...
        X