Create Two Records in One Save Action

Create Two Records in One Save Action

  
I have a membership database for my orginization. I two of my pages are one for the record of each member and another is the financial records.

I am wanting to set it up so once a new member is created, it will automatically add that record to the financial records data. I am unsure of how to implment this so any help would be appreciated. 

Micahel S
Hello,

In the save action of your page, you first need to create the member record. For that, you can use the "create" action that was added to your table by the platform when you created the table in service studio.

That action will return you the ID of the member record that was just created. I suppose that you link the financial record with the member via the member id.

Next, you need to call the "create" action for the financial table. In your code, create a local variable of type "financial" record, fill in all the information, and use the member id that you got back from the member create action.
Call the financial create action and pass the local variable "financial" record as parameter.

If the explanation is not clear, let me know and I'll send you some screen shots of the solution.
Thanks for your reply Kurt. I guess I am still not understanding how I need to create the local variable and what the source needs to be on the create action for the finacial record.

Micahel S
Michael,

Can you send me a printscreen of the 2 tables (member and financial) and I'll create a sample espace.
Sure, I have a screenshot of both below. Thanks for your help.

Michael S

Hello,

I think your solution should like somewhat like this:


The local variable I created for this are:




So in short, you need to create the member record. Use the standard create action for this that you can find in the Entities tab under the definition of your table.


The input parameter for this action is a record of type "members".
The action returns the ID of the record that was created.


Next, you want to create financial records.
I've created a local variable of a record list type of FinancialRecord. I will first create all the records I want to insert in the table and then loop over that list and actually call the CreateOrUpdate action.

The procedure is the same: fill up the FinancialRecord with the correct information and add it to the record list via the ListAppend action.

One remark: you'll need to link the financial records with the members record. How do you do that? From the screenshot I can't determine if the "User" field in the financialrecords table is linked to the members table or if it is the standard outsystems user id.

Does this explanation help?
What I am doing is User is the Outsytem user. The paid by/paid to field would just the the name of the member.

I will work on it in a bit and will let you know how I come.

Thanks
Michael
Ok, I am still having issues. On my assign I keep getting the error "Invalid Data Type 'FinancialRecords Record' data type required instead of 'Text'."  Unsure of what I need to do to fix it.

Thanks for your help
Michael S
Would you be able to send me the espace so that I can have a closer look?
Sure. The action I am working with is the Save under Member_Edit.
First of all a merry Christmas.

By seeing the code, it's a lot easier to help.
First of all, I moved the 2 local variables to the save action because you only need it there. I also rename the record.


You want to add, in my example, 2 financial records. So you need to fill up the FinanceRec (new name for the placeholder variable). Once this is done, we add it to the financialRecList and will do the insert later.
So in the assign statements, you need something like this:


Of course, in my example, we insert 2 records. The logic here will depend on how many records you want to insert of course.

One thing that I still don't get is how you are going to specify for which member the financial record is. Indeed, you use the OS user identifier, but how it the OS user linked to your members record? Don't get that.
Instead, I would use the members identifier.


The link between the two would then be:


Hope this helps you a bit.
Yes this helped me out a great amount. I was able to get it published, but whenever I used the Save button I did the an error that said ""outsystems_"."DBO"."OSUSR_0S5_FINANCIALRECORDS" with key 0 was not updated".

I have attached the oml of what I published. I made a few modifications to fit exactaly what I needed.

Thanks for all your help and I hope you had a Merry Christmas as well
Michael S

Hi,

Some remarks on your code:
- as you only seem to create only 1 Financial record, no need to add it to the record list and create the records in the for each. Just call CreateOrUpdateFinancialRecords with the FinanceRec (unless you plan to create more records in the future)

- there's no real need to create a local variable NewTot. You can directly work with GetTotalList.Current.FinancialTotalAmount and add the 15 there and use it to update the records.

- The error is occurring in "UpdateFinancialRecords" I think. When using that method, you need to make sure that the primary key (id in this case) is filled in. And that is not the case here. And if I read your code correctly, you want to update the just created record with the total. Why not calculate it upfront? Then you can pass it directly in the assign. If you do need to do it in two steps, you need to make sure that your FinanceRec holds the ID of the record. You get it back as output from the "CreateOrUpdateFinancialRecords".
And here again: as you only update 1 records, no need to go via the record list

It all went well.

Thanks for all your help on this.

Michael S