Skip to content

Commit de559ca

Browse files
Updating the Patch article for explicit use of As and ThisRecord.
1 parent 8abc4bc commit de559ca

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

powerapps-docs/maker/canvas-apps/functions/function-patch.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Specify two or more records that you want to merge. Records are processed in the
6767

6868
**Patch** returns the merged record and doesn't modify its arguments or records in any data sources.
6969

70+
71+
72+
73+
7074
## Syntax
7175
#### Modify or create a record in a data source
7276
**Patch**( *DataSource*, *BaseRecord*, *ChangeRecord1* [, *ChangeRecord2*, … ])
@@ -108,3 +112,137 @@ After the previous formulas have been evaluated, the data source ends with these
108112
| --- | --- | --- |
109113
| **Patch(&nbsp;{&nbsp;Name:&nbsp;"James",&nbsp;Score:&nbsp;90&nbsp;}, {&nbsp;Name:&nbsp;"Jim",&nbsp;Passed:&nbsp;true&nbsp;} )** |Merges two records outside of a data source:<br><ul><li>The values in the **Name** column of each record don't match. The result contains the value (**Jim**) in the record that's closer to the end of the argument list instead of the value (**James**) in the record that's closer to the start.</li><li>The first record contains a column (**Score**) that doesn't exist in the second record. The result contains that column with its value (**90**).</li><li>The second record contains a column (**Passed**) that doesn't exist in the first record. The result contains that column with its value (**true**). |{&nbsp;Name:&nbsp;"Jim", Score:&nbsp;90, Passed:&nbsp;true&nbsp;} |
110114

115+
### Use of **As** or **ThisRecord**
116+
Avoid an ambiguous evaluation context by using the **As** or **ThisRecord** keyword.
117+
118+
In the example below, consider the first Lookup in the If statement. (OrderID = A[@OrderID]) is expected to compare the OrderId in the Lookup scope with the OrderId of collection A in the ForAll scope. In this case, you likely want A[@OrderId] to be resolved as a local parameter. But it is ambiguous.
119+
120+
Power Apps currently interprets both the LHS side OrderId and RHS side A[@OrderId] as a field in the Lookup scope. Therefore, Lookup will always find the first row in [dbo].[Orders1] because the condition is always true (i.e., any row's OrderId is equal to itself.)
121+
122+
```powerapps-dot
123+
ClearCollect(
124+
A,
125+
Filter(
126+
'[dbo].[Orders1]',
127+
OrderId = 8888888
128+
)
129+
);
130+
ForAll(
131+
A,
132+
If(
133+
LookUp(
134+
'[dbo].[Orders1]',
135+
OrderId = A[@OrderId],
136+
"OK"
137+
) = "OK",
138+
Patch(
139+
'[dbo].[Orders1]',
140+
LookUp(
141+
'[dbo].[Orders1]',
142+
OrderId = A[@OrderId]
143+
),
144+
{
145+
OrderName: "val1"
146+
}
147+
),
148+
Patch(
149+
'[dbo].[Orders1]',
150+
Defaults('[dbo].[Orders1]'),
151+
{
152+
OrderName: "val2"
153+
}
154+
)
155+
)
156+
)
157+
```
158+
159+
#### Using **As** or **ThisRecord**
160+
161+
Whenever possible use the **As** operator or the **ThisRecord** to disambiguate the LHS. **As** is recommended for the above scenario.
162+
163+
When your formula uses multiple scopes with ForAll, Filter, Lookup on the same data source or table, it is possible that the scope parameters may collide with a same named field elsewhere. Therefore, it is also recommended to use the **As** operator or **ThisRecord** to resolve the field name and avoid ambiguity.
164+
165+
For example, we can use the **As** operator to disambiguate in the example below.
166+
167+
```powerapps-dot
168+
ClearCollect(
169+
A,
170+
Filter(
171+
'[dbo].[Orders1]',
172+
OrderId = 8888888
173+
)
174+
);
175+
ForAll(
176+
A,
177+
If(
178+
LookUp(
179+
'[dbo].[Orders1]' As B,
180+
B.OrderId = A[@OrderId],
181+
"OK"
182+
) = "OK",
183+
Patch(
184+
'[dbo].[Orders1]',
185+
LookUp(
186+
'[dbo].[Orders1]' As C,
187+
C.OrderId = A[@OrderId]
188+
),
189+
{
190+
OrderName: "val1"
191+
}
192+
),
193+
Patch(
194+
'[dbo].[Orders1]',
195+
Defaults('[dbo].[Orders1]'),
196+
{
197+
OrderName: "val2"
198+
}
199+
)
200+
)
201+
)
202+
```
203+
204+
Alternatively, we can use **ThisRecord** for the same purpose.
205+
206+
```powerapps-dot
207+
ClearCollect(
208+
A,
209+
Filter(
210+
'[dbo].[Orders1]',
211+
OrderId = 8888888
212+
)
213+
);
214+
ForAll(
215+
A,
216+
If(
217+
LookUp(
218+
'[dbo].[Orders1]',
219+
ThisRecord.OrderId = A[@OrderId],
220+
"OK"
221+
) = "OK",
222+
Patch(
223+
'[dbo].[Orders1]',
224+
LookUp(
225+
'[dbo].[Orders1]',
226+
ThisRecord.OrderId = A[@OrderId]
227+
),
228+
{
229+
OrderName: "val1"
230+
}
231+
),
232+
Patch(
233+
'[dbo].[Orders1]',
234+
Defaults('[dbo].[Orders1]'),
235+
{
236+
OrderName: "val2"
237+
}
238+
)
239+
)
240+
)
241+
```
242+
For detailed usage of the **As** operator and **ThisRecord**. For details, please see the **[Operators](operators.md)** article.
243+
244+
245+
246+
247+
248+

0 commit comments

Comments
 (0)