cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
martinav
Super User
Super User

Can I patch, recall, and patch again all in one OnSelect?

I have a collection I am actioning on an item.  I would like to patch the record, then recall the value, then patch to another column based on that data.  Can I do this all in one go?

 

 

With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},
    Patch(Assign_SN_List,ThisItem,{
    SNListItem:SNListTmp,
    SNListLast:Value(Last(Split(SNListTmp,",")).Result),
    SNValveTmp:"Test"
    })
);
Clear(varTestList4);
ForAll(Split(ThisItem.SNListItem,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));

Clear(colTestList);

ForAll( 
   
    Sort(varTestList4,Value,Ascending) As Count,

 
    If( 
      
        CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
        
       
        Count.Value = Last( colTestList).SecondCol+1,
        Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
        Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
        )
    )
);
Patch(Assign_SN_List,ThisItem,{
    SNValveTmp:
Match(Concat( colTestList, 
            FirstCol & 
                If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim

})

 

  In my sample, "Test" writes to SNValveTmp in the first statement, but does not get updated in the last patch statement.   

1 ACCEPTED SOLUTION

Accepted Solutions
martinav
Super User
Super User

Well, disregard, I just nested in with another With() statement, then did one patch at the end.  

 

With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},

Clear(varTestList4);
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));

Clear(colTestList);

ForAll( 

    Sort(varTestList4,Value,Ascending) As Count,

  
    If( 
       
        CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
        
       
        Count.Value = Last( colTestList).SecondCol+1,
        Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
        Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
        )
    )
);
Patch(Assign_SN_List,ThisItem,{
    SNListItem:SNListTmp,
    SNListLast:Value(Last(Split(SNListTmp,",")).Result),
    SNValveTmp:
Match(Concat( colTestList, 
            FirstCol & 
                If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim

}))

View solution in original post

5 REPLIES 5
martinav
Super User
Super User

Well, disregard, I just nested in with another With() statement, then did one patch at the end.  

 

With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},

Clear(varTestList4);
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));

Clear(colTestList);

ForAll( 

    Sort(varTestList4,Value,Ascending) As Count,

  
    If( 
       
        CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
        
       
        Count.Value = Last( colTestList).SecondCol+1,
        Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
        Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
        )
    )
);
Patch(Assign_SN_List,ThisItem,{
    SNListItem:SNListTmp,
    SNListLast:Value(Last(Split(SNListTmp,",")).Result),
    SNValveTmp:
Match(Concat( colTestList, 
            FirstCol & 
                If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim

}))
RandyHayes
Super User
Super User

@martinav 

Yes, you can perform many function in one formula, but in review of your current formula, I am not sure that it is necessary to make multiple datasource calls/patches as you can do it all in one step.

 

I am a little confused on what you are trying to accomplish in the colTestList ForAll part (which is backward - ForAll is a function that returns a table. It is not a ForLoop like in development).  There is certainly no need to create all the collections and then have all that logic from what I can tell, but I am not sure your designated purpose of that.  Perhaps an example of what you are getting in the SNListTmp (or what you are trying to do in that).

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
RandyHayes
Super User
Super User

@martinav 

You must have responded while I was responding!

Your ForAll's are still backward.  This is a bad habit in PowerApps as it leads to performance issues.

Glad you have something working though now.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@RandyHayes ,

 

I'm curious on your statement on the forall() being backwards.  What do you suggest with this?

 

 

ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)))

 

 

Here is the ther forall() with the remarks.  This one is doing something a bit complex.  It is taking an array of numbers, and reformatting.  input is [4,1,2,5,3,10,12,13] Output is "1-5,10,12-13"  Its a little hard to explain, but the columns manage groupings of consecutive vs non-consecutive numbers.  Then the final concat() statement pulls it all together into the final string.

 

 

ForAll( 
    // sorting the list of values makes sure you  can add more values in any order without error
    Sort(varTestList4,Value,Ascending) As Count,

    // build collection of first and second values
    If( 
        // create first row in collection
        CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
        
        // update collection rows or add new ones as required
        Count.Value = Last( colTestList).SecondCol+1,
        Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
        Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
        )
    )
);
Match(Concat( colTestList, 
            FirstCol & 
                If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim

 

 

RandyHayes
Super User
Super User

@martinav 

ForAll is a function that returns a table.  Using it like a For/Loop, while it might work (depending on what you are doing), it impacts performance. 

For your formula:

ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)))

You are instantiating a Collect function on each iteration of the source table - which is the table produced from the Split function.  Ultimately, the collection would have a table in it.  But, in the sense of what it is doing...let's look at it like gathering records from a datasource.  Let's say you have records in a datasource with ID's and you want the first 5.

You would perform (normally - if you wanted them in some collection), the following formula:

Collect(col, Filter(yourData, ID<=5))

Filter gets all the records and returns a Table.  This table is then given to the collection (because a collection is a table). One step...all done.

But, the following would work too:

ForAll(Sequence(5), Collect(col, LookUp(yourData, ID=Value)))

Ignoring the actual LookUp performance, you are adding a record one-by-one.

However, using the ForAll properly (and again, ignoring the performance of the LookUp for this example), the formula would be:

Collect(col, ForAll(Sequence(5), LookUp(yourData, ID=Value)))

In this case, the ForAll (as mentioned) returns a table, so the Collect function is only instantiated once and have (just like in the Filter case) the resultant table to collect.

 

As for the second part, your not so much working with an array as much as a string.  But, I'm not seeing how you would get 4,1,2,5,3,10,12,13 from your initial With statement.  It would be sequential. 

So can you explain how you get that sequence/array? 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (978)