How to not inset the duplicates into the database

Hi All,

code:-

!buttonArrayLayout(
buttons: {
a!buttonWidget(
label: "Create",
saveInto:{

a!forEach(items: local!filter,expression:a!save(local!items, append(local!items,{customerid: local!filter.customernumber[fv!index], tag: local!filter.tag.tagname[fv!index], portfolioname: local!filter.value[fv!index], portfolioid: local!filter.customerid[fv!index], tagid: local!filter.tag.tagid[fv!index]}))),

a!writeToDataStoreEntity(
dataStoreEntity: cons!portfoliomasterorg,
valueToStore: local!items
in the above code i am trying to insert the data into Db. how to not insert the duplicates into DB. Please help me to resolve the issue.
Thank you

  Discussion posts and replies are publicly visible

Parents Reply Children
  • Hi David,

    Thank you for your quick response......

    still, it is creating so many duplicates...

    a!gridField(
    label: "",
    /* Replace the dummy data with a query, rule, or function that returns a datasubset and uses fv!pagingInfo as the paging configuration. */
    data: todatasubset(local!Taglist, fv!pagingInfo ),
    columns: {
    a!gridColumn(
    label: "",
    sortField: "tagname",
    value: upper(fv!row.tagname),
    width: "AUTO"

    )
    },
    pageSize: 4,
    selectable: true,
    selectionStyle: "ROW_HIGHLIGHT",
    selectionValue: local!selection,
    selectionSaveInto: {
    local!selection,
    a!save(local!selectedEmployees, append(local!selectedEmployees, fv!selectedRows)),
    a!save(local!selectedEmployees,difference(local!selectedEmployees, fv!deselectedRows))
    /*a!save(local!selectedEmployees,local!selectedEmployees.GroupId : ri!groupid),*/
    /*updatearray(local!selectedEmployees, 1, local!selectedEmployees.GroupId = ri!groupid)*/
    }
    ),

    Please provide the another solution for this issue..

    thank you

  • when I am clicking on the first-time create button, it is not creating any duplicates, when i am clicking on the second time it not verifying whether the record is already present or not and not to insert the already existing data.

  • Without fully digesting all of your code, this can occur if you are not populating the unique ID values for existing rows before writing the CDT to the DB, so Appian does not realize this should not be a re-entry (update vs insert).  If there is no need to update entries, you would want to query for which values are already in the DB, and remove those from the CDT before persisting your data.

  • 0
    Certified Lead Developer
    in reply to Chris

    I think it would be WONDERFUL feature if Appian allowed a way to configure DB node or function to ONLY write or ONLY update, else throw error and pause the process / pink box.  The fact that it's determined at runtime, even though via very explicit and predictable rules, does lead many a developer to problems like this, and adds a bunch of time for all of us to trick it into only doing what we want.

  • Hi Chris,

    i have populated the unique id values before writing the CDT to the DB. ex;-(customerid:- 57331) if customer id already present in the DB, then no need to write that again into DB. Please provide the solution for above problem...

    thank you...

  • How to set the multiple columns as primary key while creating the CDT...

  • Appian doesn't support composite primary keys I think,

    So you can concat those columns in DB as a single column and make it primary.

  • 0
    Certified Lead Developer
    in reply to said0002

    In that situation I would deeply consider using a Stored Procedure to handle the database writes, and have IT worry about the Composite Keys and not Appian.

  • Is there any way that we can writr if condition on customer id and not enter repeating customerids......

    code:-

    !buttonArrayLayout(
    buttons: {
    a!buttonWidget(
    label: "Create",
    saveInto:{

    a!forEach(items: local!filter,expression:a!save(local!items, append(local!items,{customerid: local!filter.customernumber[fv!index], tag: local!filter.tag.tagname[fv!index], portfolioname: local!filter.value[fv!index], portfolioid: local!filter.customerid[fv!index], tagid: local!filter.tag.tagid[fv!index]}))),

    a!writeToDataStoreEntity(
    dataStoreEntity: cons!portfoliomasterorg,
    valueToStore: local!items
    i have tried but not able to get. Please provide the suggestion...
    thank you
  • 0
    Certified Lead Developer
    in reply to said0002

    If your intent is to make sure the 'customerId' field is unique, you should *REALLY* consider enforcing this constraint on the front-end instead of trying to bend over backwards and use DB write trickery and/or involve super complex stuff like composite primary keys, stored procedures, etc.

    Let's drill back up on this a bit.  Under what circumstances would Customer ID be potentially duplicate?  Is this hand-entered by a user or generated by internal logic somehow?  If user-entered, all you need is to add on-form validation.  If it's system-generated, then i guess my question is, why is this case possible?