web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Update a Value in a ta...
Power Apps
Unanswered

Update a Value in a table variable

(0) ShareShare
ReportReport
Posted on by 27

I am trying to update a column in a table variable/collection before I patch it to the final data source.

 

On a previous screen I make this declarations:

Set(varFormData, Defaults('My Data Source'));

 

The varFormData works fine with other screens. The record is updated across screens with data cards.

 

Now, I want to update a value to the Submission column in my data set before the Patch. I have not done this manually before.  The UpdateIf statement is giving me an error. I tried removing .Value after SubmissionID, but it still does not work.  The error says  varFormData needs to be a collection.  How can I update the SubmissionID value? I will need to use it on the next screen.

 

Thanks!

 

 

Update a single value in collection before patch.PNG

Categories:
I have the same question (0)
  • SoPatt Profile Picture
    Microsoft Employee on at

    If would be helpful if you gave a typed version of your code to aid others in helping you. You've put the burden on others to retype your code. Probably you have a comma where you intend to have a semicolon.

    UpdateIf updates the records in the collection that match the condition. It doesn't update variables. If you want to update a variable, use Set(). 

    You seem to be setting AppSubmissionID in the "Then" part of your If, and then using it in the "Else" part of your if. 

    I think you just want to set a field in varFormData to a default if it's blank...something like this:

     

    If(IsBlank(varFormData.SubmissionID),
     Set(varFormData,
     Value(Text(Now(),"mmddyyyyhhmmss"))
     )
    )

     

    Also maybe take a look at the Coalesce() function. It's handy for creating default behavior where there might be Blank().



  • clight Profile Picture
    27 on at

    Here is the typed code. Please respond to this code, because you have the variables transposed. The Set(AppSubmissionID,Value(Text(Now(),"mmddyyyyhhmmss"))) works fine!

     

    My issue is not being able to update the existing varFormData field with AppSubmissionID. I cannot overwrite the entire record in varFormData. There are at least 20 fields all ready populated in varFormData prior to this screen. 

     

    Here is the declaration for varFormData. I put it on the first screen of my app.  Thanks

    Set(varFormData, Defaults('My Data Source'));

     

    This is code used on button onselect. The error starts on the line with the UpdateIf statement.

     

    If(
    IsBlank(varFormData.SubmissionID),
    Set (AppSubmissionID, Value(Text(Now(),"mmddyyyyhhmmss"))),
    UpdateIf(varFormData, IsBlank(SubmissionID.Value), { SubmissionID: AppSubmissionID } )
    );
    Patch('My Data Source',varFormData,frm.Updates);
    Navigate(scrImages,ScreenTransition.Cover)

     

     

  • SoPatt Profile Picture
    Microsoft Employee on at

    It seems to me that you have not understood my post.

    Particularly: "UpdateIf updates the records in the collection that match the condition. It doesn't update variables. If you want to update a variable, use Set()."

    Regarding this part here:

    If(
    IsBlank(varFormData.SubmissionID),
    Set (AppSubmissionID, Value(Text(Now(),"mmddyyyyhhmmss"))),
    UpdateIf(varFormData, IsBlank(SubmissionID.Value), { SubmissionID: AppSubmissionID } )
    );


    I think the goal of that code is to set varFormData.SubmissionID to a default value if it's blank, excep that this is not what UpdateIf() does, as I mentioned. This would accomplish that:

    If(IsBlank(varFormData.SubmissionID),
     Set(varFormData,
     Value(Text(Now(),"mmddyyyyhhmmss"))
     )
    )




  • clight Profile Picture
    27 on at

    Thanks for your response @sopatte . Since varFormData already has data in it, will that statement overwrite existing values. 

     

    As mentioned, on the first screen of my app, I use Set to store a blank record of fields from my data source.

    btnStart OnSelect

    Set(varFormData, Defaults('IPT Daily Safety Inspection'));

     

    Is use varFormData on the next several screens to store data, e.g. varFormData.SubmissionID, varFormData.InspectorName, varFormData.EngineCheck, etc.

     

    My issue is on the 17th screen of my app.  The varFormData current record has stored responses from my app. So, if I use your suggested statement, won't that overwrite my existing data?

    Set(varFormData,
     Value(Text(Now(),"mmddyyyyhhmmss")

     

    What I want to do is update the varFormData.SubmissionID field with a value.  The time function is not an issue. I want to know how to update one field in the varFormData record without overwriting its existing data.

  • SoPatt Profile Picture
    Microsoft Employee on at

    I think I've just now understood what you are looking for. You're looking to patch a single column in a record.

     

    If(IsBlank(varFormData.SubmissionID),
     Set(varFormData,
     Patch(varFormData,
     {SubmissionID: Value(Text(Now(),"mmddyyyyhhmmss"))}
     )
     )
    )

     

    Patch(Record,Record) returns a record containing everything in the second record, as well as those columns in the first record that do not exist in the second record. In other words, the first record is "patched" using the second record. You then want to save that patched record back to your variable.

  • SoPatt Profile Picture
    Microsoft Employee on at

    It occurs to me that you said varFormData held a table. Maybe you are wanting to patch multiple records in that table and that's why you are attempting to use UpdateIf. UpdateIf only works with collections. You could use a collection instead of a variable. I'm not quite sure why there is even a distinction. In any case, I can't think of a reason why you wouldn't want to use a collection rather than a variable if you're trying to store a table. Then UpdateIf would work. However, if varFormData has only a single record, my previous reply will do the trick.

  • clight Profile Picture
    27 on at

    Set(varFormData, Defaults('IPT Daily Safety Inspection'));

    The above line creates this variable with all the fields of my data source. I have about 40 fields, so it saves the typing. Also, as I add fields to the data source, I do not have to change the statement, only refresh.

  • SoPatt Profile Picture
    Microsoft Employee on at

    Yeah so since Defaults() returns a record, your variable contains a record type, not a table. So again the way to update a single field in a record variable, but only if it's blank, is like I said above, or here's another way:

     

    Set(varFormData,
     Patch(varFormData,
     {
     SubmissionID: Coalesce(
     varFormData.SubmissionID,
     Value(Text(Now(),"mmddyyyyhhmmss"))
     )
     }
     )
    )

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 765 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 272

Last 30 days Overall leaderboard