databasepowerapps

Patching New or Edited data back to a database


I am new to Power Apps and I am stuck. I look to see if a record exists in my database, If it exists, I am pulling that information into a collection and then displaying it in a gallery for the user to be able to see and possibly make edits. Where I am stuck is trying to save an new entries or edits back to the database.

If(
    CountRows(colRelatedJob) > 0,
    ForAll(
        colRelatedJob,
        Patch(
            If(
                IsBlank(LookUp(Filter(ds_tblRelatedJobs, FK_JobNum = txtJob.Value),RelatedJob = ThisRecord.RelJob)),
                Defaults(ds_tblRelatedJobs),
                //LookUp(ds_tblRelatedJobs, FK_JobNum = txtJob.Value && RelatedJob = RelJob)
                LookUp(Filter(ds_tblRelatedJobs, FK_JobNum = FKJobNum),RelatedJob = RelJob)
            ),
            {
                RelatedJob: ThisRecord.RelJob,
                RelatedGroup: ThisRecord.RelGroup,
                RelatedPM: ThisRecord.RelPM,
                RelatedForeman: ThisRecord.RelForeman,
                RelatedScope: ThisRecord.RelScope,
                FK_JobNum: txtJob.Value
            }
        )
    ),
    ""
);

In this line of code I am receiving multiple errors IsBlank(LookUp(Filter(ds_tblRelatedJobs, FK_JobNum = txtJob.Value),RelatedJob **=** ThisRecord**.RelJob**)

The error for the "Equal Sign" is "Incompatible types for comparison. These types can't be compared: Text, Error."

The error for .RelJob states "Name isn't valid. 'RelJob' isn't recognized."

Both errors go away if I remove ThisRecord. from ThisRecord.RelJob but then noithing will get written to the database.

I know the below code works for saving new entries only, but I am trying to make it dynamic to handle any new or edited data

If(CountRows(colRelatedJob) >0,
    ForAll(
        colRelatedJob,
            Patch(
                 Defaults(ds_tblRelatedJobs),
                {
                    RelatedJob: ThisRecord.RelJob,
                    RelatedGroup: ThisRecord.RelGroup,
                    RelatedPM: ThisRecord.RelPM,
                    RelatedForeman: ThisRecord.RelForeman,
                    RelatedScope: ThisRecord.RelScope,
                    FK_JobNum: ThisRecord.FKJobNum
                }
            )
    ),
"");

Solution

  • After weeks of trial and error, I was able to correct the code. I found an article from Microsoft Learn. This article helped me use an alias to get the error from the "Equal sign" and the .RelJob. There was also another issue I found that my Patch syntax was incorrect.

    If(CountRows(colRelatedJob) > 0,
        ForAll(colRelatedJob As _Related,
            If(_Related.SaveRecord,
                Patch(ds_tblRelatedJobs,
                        Coalesce(LookUp(ds_tblRelatedJobs, FK_JobNum = txtJob.Value && RelatedJob = _Related.RelJob),Defaults(ds_tblRelatedJobs)),
                    {
                        RelatedJob: _Related.RelJob,
                        RelatedGroup: _Related.RelGroup,
                        RelatedPM: _Related.RelPM,
                        RelatedForeman: _Related.RelForeman,
                        RelatedScope: _Related.RelScope,
                        FK_JobNum: txtJob.Value
                    }
                )
            )
        )
    );