cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MrNappa
Kudo Collector
Kudo Collector

How do I use the 'Errors' function correctly?

This is kind of related to my previous post but I think it warrants its own thread as it's a separate issue.

 

I tried to do a patch using the following code:

 

 

  UpdateContext({ guid: First(savebufferProjects).PrjId });
  UpdateContext({ patchresultProject: Patch( Projects, LookUp( Projects, Project=guid),{
      Planningstatus: 'Planningstatus (Projects)'.Geaudit 
    })});
  If( IsEmpty( Errors( Projects, patchresultProject ) ),
    RemoveIf( savebufferProjects, PrjId=guid );
    RemoveIf( schFlush_buffer, id=guid );
    SaveData( savebufferProjects, "savebufferProjects" );
    // trigger continue
    UpdateContext({ sendaction: true });
  , 
    Notify("Error:"&First(Errors(Projects, patchresultProject)).Message );
  );

 

 

Note that I check the patch result using the Errors function.

 

In my situation, the patch failed due to insufficient permissions. If this were the case, I would have expected this information to be contained in the patch result, and my own code should have shown a notification. However, that never happened; instead, the other branch in the IF statement was executed (removing the item from the buffer and continuing with other code).

 

What am I doing wrong here?

1 ACCEPTED SOLUTION

Accepted Solutions
v-bofeng-msft
Community Support
Community Support

HI @ MrNappa:

Could you tell me have you checked your data source? Whether the patch function has run successfully?

Taking into account the problem you mentioned ,my suggestion is: Do not use the return value of the patch directly.

Firstly, let me explain why you are not recommended to do this:

Errors can be returned for the entire data source, or for only a selected row by providing the Record argument to the function.

patch or another data function may return a blank value if, for example, a record couldn't be created. You can pass blank to Errors, and it will return appropriate error information in these cases. Subsequent use of data functions on the same data source will clear this error information. I think this link will help you a lot.

Errors function in Power Apps:

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-errors

Secondly ,I suggest you try this code:

  UpdateContext({ guid: First(savebufferProjects).PrjId });
  UpdateContext({ patchresultProject: LookUp( Projects, Project=guid));
 Patch(Projects, patchresultProject, {Planningstatus: 'Planningstatus (Projects)'.Geaudit});
  If( IsEmpty( Errors( Projects, patchresultProject ) ),
    RemoveIf( savebufferProjects, PrjId=guid );
    RemoveIf( schFlush_buffer, id=guid );
    SaveData( savebufferProjects, "savebufferProjects" );
    // trigger continue
    UpdateContext({ sendaction: true });
  ,
    Notify("Error:"&First(Errors(Projects, patchresultProject)).Message );
  );

Best Regards,

Bof

View solution in original post

11 REPLIES 11
v-bofeng-msft
Community Support
Community Support

HI @ MrNappa:

Could you tell me have you checked your data source? Whether the patch function has run successfully?

Taking into account the problem you mentioned ,my suggestion is: Do not use the return value of the patch directly.

Firstly, let me explain why you are not recommended to do this:

Errors can be returned for the entire data source, or for only a selected row by providing the Record argument to the function.

patch or another data function may return a blank value if, for example, a record couldn't be created. You can pass blank to Errors, and it will return appropriate error information in these cases. Subsequent use of data functions on the same data source will clear this error information. I think this link will help you a lot.

Errors function in Power Apps:

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-errors

Secondly ,I suggest you try this code:

  UpdateContext({ guid: First(savebufferProjects).PrjId });
  UpdateContext({ patchresultProject: LookUp( Projects, Project=guid));
 Patch(Projects, patchresultProject, {Planningstatus: 'Planningstatus (Projects)'.Geaudit});
  If( IsEmpty( Errors( Projects, patchresultProject ) ),
    RemoveIf( savebufferProjects, PrjId=guid );
    RemoveIf( schFlush_buffer, id=guid );
    SaveData( savebufferProjects, "savebufferProjects" );
    // trigger continue
    UpdateContext({ sendaction: true });
  ,
    Notify("Error:"&First(Errors(Projects, patchresultProject)).Message );
  );

Best Regards,

Bof

Thanks -- it wasn't really apparent to me that I needed to pass the original record to the Errors function. Especially because the return value of Patch is also a record.

 

Tried your suggestion and it worked!

Hi @v-bofeng-msft 

 

I have a follow up question. I also tried some error handling on creating a new record, but... I get an unexpected error.

 

Consider the code from this screenshot:

errorscheck.jpg

Here I'm testing 2 patches to create records. The code is the equivalent (the lookup in the first patch doesn't make a difference), except they operate on different data sources (both are CDS data sources).

 

However, as you can see, using the errors function on the first table works fine, but it fails on the second table.

 

Do you have any ideas what's the cause of this? 

Hi @MrNappa :

I suggest you try this code:

Errors(Registraties)

Best Regards,

Bof 

Thanks, using the Errors function without parameter as you suggested works.... but why is there a difference in the first place?

Hi @MrNappa :

If you don't specify this argument(Record ), the function returns errors for the entire data source.

Best Regards,

Bof

I know, that's in the docs. I'm just wondering why Errors( Projectdefinities, newProjectdefinitie ) is valid, while Errors( Registratie, newRegistratie ) is not.

Hi @MrNappa :

Your current operation is to create a record. If you created a record, the return value may include properties that the data source generated automatically. However, the return value doesn't provide a value for fields of a related entity.

I think this link will help you a lot:

Patch function in Power Apps:

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-patch#description

Best Regards,

Bof

I was investigating a bit further because it bothered me. I think I found the underlying cause: PowerApps gives this error when Notes have been enabled for the entity.

 

It's very easy to see. I created a test entity and then used this code:

UpdateContext({
    newTestEntity: Patch( TestEntities, Defaults(TestEntities), { new_name: "test" } )
});
If( IsEmpty( Errors( TestEntities, newTestEntity ) )
  , Notify("success!");
  , Notify("failed:"&First(Errors(TestEntities, newTestEntity)).Message );
);

This worked fine, as expected. But as soon as I modified the entity to enable notes, and refreshed the data source in PowerApps, it started giving me the error again. 

 

Not sure if that's a bug or a feature.

Helpful resources

Announcements
Super User 2 - 2022 Congratulations 768x460.png

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

<
Users online (2,568)