Hi everyone,
I'm having a question regarding how to use the writeToDataStoreEntity function to modify values in DB.
To be specific, I'm having a form that allow users to update their phone numbers. It will be saved in a DB with corresponding DSE storing all customer data. When the customer click the button Save New Change, the existing number need to be replaced by the new number, both on the interface and in the DB. I'm not sure how to do that yet. I'm thinking of using writeToDataStoreEntity, and find the number that needs to change by the unique customer ID, but not really know in specific how.
Any one have a solution for this? Really appreciate your help!
Code for the Save New Change button is currently as below, in that local!allUser query list of users from db, and selected user is the one that need to change phone number:
This code shows an error: An error occurred while trying to convert the given data to the type of the specified entity "ALL_USERS" [id=f4ece61e-8fb0-4abd-a39b-8096f1aaf650@32783, type=ALL_USERS (id=6306)] (data store: AUS Data Store). Value that could not be converted: ActivityClassParameter[name=DynamicInputs,it=3,v=new name] Details: Could not cast from Text to ALL_USERS. Details: CastInvalid (APNX-1-4400-000)
a!localVariable( local!saveNewChange:false, a!sideBySideItem( item: a!richTextDisplayField( labelPosition: "COLLAPSED", value: { a!richTextIcon( icon: "save", link: a!dynamicLink( saveInto: { a!save( a!writeToDataStoreEntity( dataStoreEntity: cons!DSE_All_USERS, valueToStore: local!newNumber, onSuccess: { a!save(local!saveNewChange, false), a!save(local!newNumber, null), a!save( tostring(index(local!allUsers,local!selectedUser.id,"phoneNumber",null)),tostring(fv!storedValues) ) }, ), linkStyle: "STANDALONE", size: "STANDARD", caption: "Save New Change" ) } ), width: "MINIMIZE", showWhen: a!isNullOrEmpty(local!newNumber) ),
Discussion posts and replies are publicly visible
writeToDataStoreEntity Smart service updates the records if there is a primary key present in the variable. If the PK is null, then it will write it as a new record. So if you want to update the record, make sure the PK is present when you pass this value to the smart service,
Hi Harshit, the primary key is customer ID for my case. By using that the corresponding number to be changed will be found
You will need to change line 14. The valueToStore cannot just be your new phone number. How should Appian know which field that is or which row in DB to update?
You either already have a local variable containing that selected user CDT or you need to query it from DB. Then, update that specific field in that CDT, and finally, write it to DB.
Thanks Stefan. Your solution sounds absolutely right to be. But not sure how to apply on my case yet.
I created a local variable storing all user data, queried from corresponding record type, and the selected user local variable is found within the all user local variable. I'm planning to change the data in local!allUsers first, using local!selectedUser.id then write that local!allUsers back to the DSE. Does it sound right?
Now if I want to change the selected user's name within the list of all user, which function can I do it? I tried a!save(index...) but seems like it was not the right way to do.
You will find many useful patterns in the Appian Interface Recipes
https://docs.appian.com/suite/help/22.3/SAIL_Recipes.html
Your case looks like
https://docs.appian.com/suite/help/22.3/recipe-add-edit-and-remove-data-in-an-inline-editable-grid.html
Thanks Stefan, seems like the given document is more on adding and removing data, not about editing it as I wanted...
Sure. The next step is, to write the captured data to DB. Then check whether the DB is as expected. Then add a query to the interface to load data from that table.
Put this into a saveInto of a button:
https://docs.appian.com/suite/help/22.3/Write_Records_Smart_Service.html#create-a-new-record-using-an-expression
To query data:
docs.appian.com/.../fnc_system_queryrecordtype.html
I was able to update data on the DB, and then query it back to the interface using queryRecordType as you suggested! Thanks a lot.
There is just one issue left is that when I changed the data and click button Save, the data shown on the interfase are not real time, I need to refresh the interface for the new phone number to show up eventhough refreshAlways = true in a!queryRecordType. Do you know what is the possible reason?