I need to pull the highest "ID" number from SQL

Certified Associate Developer

So I have a SQL table with an ID number, I need Appian to pull the highest number and add 1 to that.

Thank you

  Discussion posts and replies are publicly visible

Parents
  • Mike is absolutely correct, but I also wanted to check with you - what's your use case for this? I've seen some folks try to do this as a way of "auto-generating" their next ID by adding 1 to this number in Appian. However, there are a lot of edge cases that make this more challenging than it appears. For instance, what if you have a race condition where two users perform this query at the same time? The second user will then end up overwriting what the first user did.

    If this is your use case (querying the max ID to define a PK for inserting the next record), I'd highly recommend using the database auto-increment capabilities. Since the database handles this, you won't have to worry about these race conditions at all.

    That being said, if you have another use case please feel free to ignore this Slight smile - I know I personally have come across someone trying to do this and just wanted to warn you from something that may lead to issues later!

  • 0
    Certified Associate Developer
    in reply to Peter Lewis

    How do you get it to write to the database when you do not have a value and the ID can not be null?

  • 0
    Certified Lead Developer
    in reply to AllenP

    When writing a new row to a table with the primary key set to auto_increment (which it should in most cases that i know of), you would just leave that field blank in your CDT and, as Peter mentioned, the DB takes care of populating the next available value for you.

  • 0
    Certified Associate Developer
    in reply to Mike Schmitt

    I am getting the following error.  I have attached the code.  I have it create 2 local variables, one for an existing contact and 1 for a new one.

      a!localVariables(
        local!contactExist: cast(
          'type!{urn:com:appian:sad_Contacts}sad_Contacts',
          {
            a!map(
              id: ri!existCustomer,
              locationId: ri!existLocation,
              contactFirstname: ri!contactFirstName,
              contactLastname: ri!contactLastName,
              contactTitle: ri!contactTitle,
              contactEmail: ri!contactEmail,
              contactPhone: ri!contactPhoneDB
            )
          }
        ),
        local!contactNew: cast(
          'type!{urn:com:appian:sad_Contacts}sad_Contacts',
          {
            a!map(
              locationId: ri!existLocation,
              contactFirstname: ri!contactFirstName,
              contactLastname: ri!contactLastName,
              contactTitle: ri!contactTitle,
              contactEmail: ri!contactEmail,
              contactPhone: ri!contactPhoneDB
            )
          }
        ),
        a!buttonArrayLayout(
          buttons: {
            a!buttonWidget(
              label: "Submit",
              saveInto: a!writeToDataStoreEntity(
                valuesToStore: {
                  if(
                    isnull(ri!currentContact),
                    a!entityData(cons!contactDetail, local!contactNew),
                    a!entityData(cons!contactDetail, local!contactExist)
                  ),
                  
                }
              ),

Reply
  • 0
    Certified Associate Developer
    in reply to Mike Schmitt

    I am getting the following error.  I have attached the code.  I have it create 2 local variables, one for an existing contact and 1 for a new one.

      a!localVariables(
        local!contactExist: cast(
          'type!{urn:com:appian:sad_Contacts}sad_Contacts',
          {
            a!map(
              id: ri!existCustomer,
              locationId: ri!existLocation,
              contactFirstname: ri!contactFirstName,
              contactLastname: ri!contactLastName,
              contactTitle: ri!contactTitle,
              contactEmail: ri!contactEmail,
              contactPhone: ri!contactPhoneDB
            )
          }
        ),
        local!contactNew: cast(
          'type!{urn:com:appian:sad_Contacts}sad_Contacts',
          {
            a!map(
              locationId: ri!existLocation,
              contactFirstname: ri!contactFirstName,
              contactLastname: ri!contactLastName,
              contactTitle: ri!contactTitle,
              contactEmail: ri!contactEmail,
              contactPhone: ri!contactPhoneDB
            )
          }
        ),
        a!buttonArrayLayout(
          buttons: {
            a!buttonWidget(
              label: "Submit",
              saveInto: a!writeToDataStoreEntity(
                valuesToStore: {
                  if(
                    isnull(ri!currentContact),
                    a!entityData(cons!contactDetail, local!contactNew),
                    a!entityData(cons!contactDetail, local!contactExist)
                  ),
                  
                }
              ),

Children