cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

User Defined Function to Create/Update Data

I'm trying to build a user defined function to replace this type of logic:

If(
//check to see if the record is blank
    IsBlank(
        LookUp(
            'General App Infos',
            'Application Name' = AppName_Input.Text
        )
    ),
// if blank create the record
    Patch(
        'General App Infos',
        Defaults('General App Infos'),
        {
            'Application Name': AppName_Input.Text,
            'File Name': FileName_Input.Text,
            'Discipline Choices': Discipline_Input.SelectedItems.Value,
            'App Description': AppDescription_Input.Text,
            'Application Owner': ApplicationOwner_Input.Text
        }
    ),
// if not update the record
    Patch(
        'General App Infos',
        LookUp(
            'General App Infos',
            'Application Name' = AppName_Input.Text
        ),
        {
            'Application Name': AppName_Input.Text,
            'File Name': FileName_Input.Text,
            'Discipline Choices': Discipline_Input.SelectedItems.Value,
            'App Description': AppDescription_Input.Text,
            'Application Owner': ApplicationOwner_Input.Text
        }
    )
);

 

Currently I have a component called DataTableFunctions that holds the user defined function CreateOrUpdateText. CreateOrUpdateText currently takes in three parameters, LookupTable, LookupRecord, TextInput. 

msze400_0-1619096315453.png

 

CreateOrUpdateText (my intent is to only check if something is in the table or not):

If(IsBlank(LookUp(LookupTable, LookupRecord.SampleStringField = TextInput)), "Something is Here", "Nothing is Here")

 

 

I also created a CDS data table called TestNames. I'm running into an issue with the inputs when trying to call CreateOrUpdateText from a label, where the lookup table input 'TestNames' is not showing up. The only thing that shows up as a recommended input is 'TestNames (Views). Is there another way I should be defining generic tables for user defined functions?

msze400_2-1619096855099.png

 

 

3 REPLIES 3
RandyHayes
Super User
Super User

@Anonymous 

The concept of the "user defined function" for components is relatively limited as components should not be connected to datasource (this breaks the component concept).

To further complicate, when you define table or record inputs or outputs of the component, you must specify the schema of the record.  So there is no concept of providing a "generic" table or record.

 

I would recommend instead that if this formula is something you need to call often, that you put this in a toggle control.

Ex.  Place a toggle control on the screen.  Set the OnCheck formula to the following (note this is a simplified version of your original formula that is functionally equivalent and less redundant):

// if existing update, if blank create
    Patch(
        'General App Infos',
        Coalesce(LookUp('General App Infos', 'Application Name' = AppName_Input.Text), Defaults('General App Infos')),
        {
            'Application Name': AppName_Input.Text,
            'File Name': FileName_Input.Text,
            'Discipline Choices': Discipline_Input.SelectedItems.Value,
            'App Description': AppDescription_Input.Text,
            'Application Owner': ApplicationOwner_Input.Text
        }
    );

Set(glbPerformPatch, false)

Then set the Default property of the toggle to:

glbPerformPatch

 

Now, anywhere in your app that you want to perform this function, simply do a : Set(glbPerformPatch, true)

The toggle will go true and the formula will evaluate and then it finally will set the toggle off and ready to go again.

 

You will have much less frustration with this method than trying to get a component to observe the proper schemas and datasources.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
Anonymous
Not applicable

Hi @RandyHayes ,

 

Thanks for the detailed response, I was unaware that doing setting up this way broke the component concept. Patching with the Coalesce function seems a lot more elegant. Thanks a bunch! 

RandyHayes
Super User
Super User

@Anonymous 

Yes, technically a component is a universal component.  Much like a button (for example) is a component and can be used in any app that you want.  If the button relied on a datasource or data that would only be relevant to one app, it would not be universal.

So, components should never rely on anything in the app in order to work properly.

 

This is the concept of components today.  There is another concept that was "in the works" some time ago where you could have both universal components (that can be imported into any other app) and app components (that are only available in the app and could rely on app resources).  But, I have no idea where that is at this time. Perhaps one day!

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

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

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Government Carousel

New forum: GCC, GCCH, DoD - Federal App Makers (FAM)

In response to the unique and evolving requirements of the United States public sector, Microsoft has created Power Apps US Government.

Users online (1,840)