Creating records based on other record

Certified Associate Developer

I have a Process Model that creates a record. That record has a field "record types", which can be A, B, or C. Based on which record type is selected, I would like to create a record from a related table with additional inputs. I have tried following this documentation: https://docs.appian.com/suite/help/23.1/Write_Records_Smart_Service.html#create-records-and-related-records, but there is a lot that it leaves out. 
Here is an example. If the Record Type is A, the process forks down to "Create A record".

My problem is creating the A record. The A table has a 1-1 relationship with the Record table.
Some specific questions I have are:
What should the process variables be set up like for this? 
How should the "Create A record" Data section be set up for this process?
Both tables have Auto Incrementing IDs, is any additional setup needed for this on the actual Appian platform? If so, what?
Are there any additional services or processes needed to accomplish this? 

  Discussion posts and replies are publicly visible

  • +1
    Certified Lead Developer

    You can output the records that were created as the output of the first write node.  Then, in a script task in between the two, update the A Record with the ID from the one that was written, then you can write it too.  With a 1-to-1 relationship, the FK has to be on one of them.  The one with the FK has to be the one that's written second, with its FK populated from the script task in between.

  • 0
    Certified Associate Developer
    in reply to Dave Lewis

    Thank you, that is very helpful. Unfortunately, the current setup is to where the FK for A, B, C, etc are on the Record table, so I will have to do some reconfiguration, but not the end of the world. Just to be sure and for future reference, do you know this process is possible in any other way with the current setup?
    Current Setup:
    Record:
    PK recordID
    FK aID
    FK bID
    FK cID

    A
    PK aID

    B
    PK bID

    C
    PK cID

  • +1
    Certified Lead Developer
    in reply to matt

    Write A, or B, or C first as needed, based on conditional logic, then update the FK on the main record, then write it.  I assume that the FKs are all potentially null, because you have a 0-1-to1 relationship.  Each of the 3 possible conditional writes has an outflow that all goes to the same script task that updates the main one.  It only gets a little more complicated and might need OR and AND gateways if there's potential to sometimes save A and B or B and C or all 3.

  • 0
    Certified Associate Developer
    in reply to Dave Lewis

    That makes sense, thanks. However, when implementing this, I get the following error when it attempts to write the A record: "Each item within the input "Records" must be a record data type. Received: A." 
    Is this because the pv!A is null, even though it is a record data type? If so, would a possible solution be to update the ID of ri!A to the next available ID when submitting the form for Record?

  • 0
    Certified Lead Developer
    in reply to matt

    It sounds like you don't have an "A" in this case.  If you used a recordType constructor, you shouldn't have do add the ID, and when there's no ID you perform an INSERT when you write to the database.  You should check first to see if you have an "A" at all, or if there's no record before trying to write A.  That means an XOR gateway.  If there's an A, go down the path where you write it, otherwise skip and do the next part of the process.

  • 0
    Certified Associate Developer
    in reply to Dave Lewis

    That's correct, there is no "A" record when the process starts. My goal is to create one from scratch depending on the Record Type selection. If I set the value for the input in the Create A Record write service to be a recordType constructor, it still gets an error saying the record type cannot be empty. However, if I give it an ID in the constructor, it seems to work. So, for now, I have created a rule to get the next available A ID and set it in the A constructor. This works with one user, but if multiple users are executing this process at the same time, I can potentially see issues. 

    Any suggestions on what I may have setup wrong? Or how to trigger an INSERT without the write service needing a value in the constructor?

  • 0
    Certified Lead Developer
    in reply to matt

    You need a value.  You need SOMETHING to Insert, otherwise you're performing a non-operation.  What other fields are there in the record?  The name, for instance.  You use a recordType contstructor:  recordType!myRecord( <this is where you fill in fields in key:value pairs>)

    All you do is leave off the ID only.  Put everything else in the DB, but no ID, then it makes an ID to stick on to all the other stuff with an auto-generated ID from the database.

  • 0
    Certified Associate Developer
    in reply to Dave Lewis

    For my use case, the other fields need to be empty, but it looks like I can just set one of them to null (ex. A.name = null()) and it takes it. This is strange because it seems like I'm just setting a null value to null, but it works!
    Thanks for the help, Dave!

  • 0
    Certified Lead Developer
    in reply to matt

    Hey, if it's enough to trick it into making the auto-increment Id.  I should have remembered another trick some people use is to set the id to -1.  That's another fun trick to force the system to replace -1 with a positive integer, the AutoIncrement value.