cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

ForAll Patch Collection to SQL Server

I am trying to patch an entire collection to a SQL server at one time.  The collection contains unique values.  However when it patches the the SQL server the 1st record gets input for each item.


For example if the collection as 4 records (A, B, C, D), what gets patched to the SQL server is A, A, A, A.  The total number of records patched is correct (if there are 50 records in the collection, 50 records will be added to SQL), but it just keeps repeating the data for the 1st record in the collection.

The code I am using follows this structure:

ForAll(CollectionItems,
Patch(SQL.dbo,
          Defaults(SQL.dbo),
           {Var1: CollectionItem.var1,
            Var2: CollectionItem.var2 } ) );

Any idea as to why only the data from the 1st record is being patched?  Any help would be greatly appreciated!

@Shanescows @Pstork1 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Dual Super User II
Dual Super User II

Re: ForAll Patch Collection to SQL Server

You are very close, but I think you should be using just the field name as the value, not the collection.field name.  This works for me.

ForAll(testcollect,
Patch('[dbo].[SQL]',
          Defaults('[dbo].[SQL]'),
           {FieldA: fielda,
            FieldB: fieldb } ) );

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

5 REPLIES 5
Highlighted
Community Support
Community Support

Re: ForAll Patch Collection to SQL Server

Hi @KaiserKid ,

Do you want to patch entire collection data back to your SQL Table once time?

 

Based on the Patch formula that you mentioned, I think there is something wrong with it. I have made a test on my side, please consider modify your formula as below:

ForAll(
       CollectionItems,
       Patch(
             SQL.dbo,
             Defaults(SQL.dbo),
             {
               Var1: CollectionItems[@Var1],
               Var2: CollectionItems[@Var1],
               Var3: CollectionItems[@Var3],
               Var4: CollectionItems[@Var4]
             }
      )
);

When you use ForAll function to patch each record from the collection back to your SQL Table, you should reference the column value from the collection directly, rather than using CollectionItem.Var1, CollectionItem.Var2, CollectionItem.Var3, ... formula.

 

Best regards, 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Dual Super User II
Dual Super User II

Re: ForAll Patch Collection to SQL Server

You are very close, but I think you should be using just the field name as the value, not the collection.field name.  This works for me.

ForAll(testcollect,
Patch('[dbo].[SQL]',
          Defaults('[dbo].[SQL]'),
           {FieldA: fielda,
            FieldB: fieldb } ) );

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

Highlighted
Frequent Visitor

Re: ForAll Patch Collection to SQL Server

Great catch! Thanks!

Highlighted
Frequent Visitor

Re: ForAll Patch Collection to SQL Server

Spoiler
Hello,
I'm trying to do the same thing, however, I'm getting error: The specified record was not found. Server Respoinse [dbo] failed" "resource not found"

I'm using the same formula as described in this thread, however:
1- SQL table is empty, I'm trying to add records to the table
2- TRACK field is supposed to be the primary key

Please see formula below:

ForAll(CollectInvoices1,Patch('[dbo].[DMFPAYMENTREQUESTENTITY]',Defaults('[dbo].[DMFPAYMENTREQUESTENTITY]'),{TRACK:1,AMOUNT:'Amount Cur',CHEQUECANCELLED:123,PAYMENTMETHOD:'Payment Method',RECID:12,ProjId:"ZZZ",CREATEDON:Invoicedate,CURRENCYCODE:Currency,DATAAREA:"ZZZ",DATE:Invoicedate,OPSACCOUNTINGID:Vendor,OPSID:"AMZ",PAYMENTREFERENCE:"USD",PRINTERLOCATION:"MLT",SYSTEMID:"KKK",STATUSLOG:"KKK",TRFRSTATUS:true}))

Thank you so much!

 

Highlighted
Dual Super User II
Dual Super User II

Re: ForAll Patch Collection to SQL Server

Is the primary key an autogenerated identity key?  If it is you shouldn't specify it in the pattch.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Helpful resources

Announcements
Check this Out

Announcing

Class of 2020- Season 2

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (8,230)