web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Create record if it do...
Power Apps
Answered

Create record if it doesn't exist with lookup column

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I need a form to Check if a record exists, and if not create a new record while setting a lookup column of that new record. HE is the code I have OnVisible for my form. 

 

If(IsBlank(LookUp('In-Depth Review', CRUCaseID.Id = Value(BrowseGallery1.Selected.ID))), Patch('In-Depth Review', Defaults('In-Depth Review'), {CRUCaseID: {Id:BrowseGallery1.Selected.ID, Value: BrowseGallery1.Selected.ID}}));

 

It isn't throwing any errors but it also doesn't seem to do anything. 

Categories:
I have the same question (0)
  • WarrenBelz Profile Picture
    155,686 Most Valuable Professional on at

    Hi @Anonymous ,

    As much as I wonder why Lookup fields are used at all (I have never found the need and they can cause you unwanted and unnecessary grief), there are two issues here

    If(
     IsBlank(
     LookUp(
     'In-Depth Review', 
     CRUCaseID.Id = Value(BrowseGallery1.Selected.ID)
     )
     ), 
     Patch(
     'In-Depth Review', 
     Defaults('In-Depth Review'), 
     {
     CRUCaseID: 
     {
     Id:BrowseGallery1.Selected.ID, 
     Value: BrowseGallery1.Selected.YourFieldName
     }
     }
     )
    );

    This line

    CRUCaseID.Id = Value(BrowseGallery1.Selected.ID)

    is not Delegable (it is a Relational query and SharePoint is not a Relational database) and therefore you will have issues on larger data sets and this part

    Value: BrowseGallery1.Selected.ID

    as per my main code above needs to be the field name (not the ID) being looked up in the list contained in the gallery.
    You can solve the Delegation issue with

    If(
     IsBlank(
     LookUp(
     'In-Depth Review', 
     CRUCaseID.Value = BrowseGallery1.Selected.YourFieldName
     )
     ), 
     Patch(
     'In-Depth Review', 
     Defaults('In-Depth Review'), 
     {
     CRUCaseID: 
     {
     Id:BrowseGallery1.Selected.ID, 
     Value: BrowseGallery1.Selected.YourFieldName
     }
     }
     )
    );

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Awesome, thanks for the help. 

    Two additional questions for you if thats alright. 

    1. Should I replace the Lookup column with a simple int? I really just need to use the ID to link list data together so if a lookup column is unnecessary i'm fine with just getting rid of it. 
    2. Should I do away with multiple lists? I separated the needed fields into various appropriate lists in order to keep the data more organized (which may have been self-defeating). Would it be better if I had the 200+ fields in one list to simplify the project?

    I'm very new to Powerapps so learning how to properly build an app with list data is a must. 

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I am also getting the error:
    "Behavior Function in a non-behavior property. You can't use this property to change values elsewhere."

  • WarrenBelz Profile Picture
    155,686 Most Valuable Professional on at

    Hi @Anonymous ,

    Firstly what is your Data source - I assumed SharePoint. If so, the answer to #1 is to use a text field. #2 depends on your data structure, but if the lists are identical in field names, certainly make one list.

  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    This fixed it for me. Needed to put the CRUCaseID: In the brackets. 

     

    If(
    IsBlank(
    LookUp(
    'In-Depth Review',
    CRUCaseID = Value(BrowseGallery1.Selected.ID)
    )
    ),
    Patch(
    'In-Depth Review',
    Defaults('In-Depth Review'),
    {
    CRUCaseID: BrowseGallery1.Selected.ID
    }
    ))

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 463

#2
WarrenBelz Profile Picture

WarrenBelz 364 Most Valuable Professional

#3
11manish Profile Picture

11manish 275

Last 30 days Overall leaderboard