Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

Need help with If statement on the OnSave event

(0) ShareShare
ReportReport
Posted on by 7

I have a SharePoint list integrated with a PowerApp. In the OnSave of the SharePointIntegration I am trying to update and validate different forms based on a selected vale and a checkbox. I cannot seem to get everything right with my if statements. I have some datacards required but with the current statements, it is letting me click the save button with or without the required fields populated but it is not actually saving my item to the list. Any help is appreciated. Below is the code I currently have:

If (
Checkbox1.Value = true,
If(
BasicInfoForm.Mode = FormMode.New && BasicInfoForm.Valid,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates,
ManualAddForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates,
ManualAddForm.Updates
)),
ResetForm(BasicInfoForm);
RequestHide()
);

If(
DataCardValue5.Selected.Value = "Manual Add" && Checkbox1.Value = false,
If(
BasicInfoForm.Valid && ManualAddForm.Valid,
If(
BasicInfoForm.Mode = FormMode.New,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates,
ManualAddForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates,
ManualAddForm.Updates
)
);
Notify("Please fill the required fields")
);
ResetForm(BasicInfoForm);
ResetForm(ManualAddForm);
RequestHide();

);
If(
DataCardValue5.Selected.Value = "Cost Adjustment" && Checkbox1.Value = false,
If(
BasicInfoForm.Valid && CostAdjustmentForm.Valid,
If(
BasicInfoForm.Mode = FormMode.New,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates,
CostAdjustmentForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates,
CostAdjustmentForm.Updates
);
Notify("Please fill the required fields")
)
);
ResetForm(BasicInfoForm);
ResetForm(CostAdjustmentForm);
RequestHide();

);
If(
DataCardValue5.Selected.Value = "Transfer" && Checkbox1.Value = false,
If(
BasicInfoForm.Valid && TransferForm.Valid,
If(
BasicInfoForm.Mode = FormMode.New,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates,
TransferForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates,
TransferForm.Updates
);
Notify("Please fill the required fields")
)
);
ResetForm(BasicInfoForm);
ResetForm(TransferForm);
RequestHide();

);
If(
DataCardValue5.Selected.Value = "Disposal" && Checkbox1.Value = false,
If(
BasicInfoForm.Valid,
If(
BasicInfoForm.Mode = FormMode.New,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates
);
Notify("Please fill the required fields")
)
);
ResetForm(BasicInfoForm);
RequestHide();

);
If(
DataCardValue5.Selected.Value = "Adjust Useful Life / Accelerate Depreciation" && Checkbox1.Value = false,
If(
BasicInfoForm.Valid,
If(
BasicInfoForm.Mode = FormMode.New,
Patch(
'Business Asset',
Defaults('Business Asset'),
BasicInfoForm.Updates
),
Patch(
'Business Asset',
SharePointIntegration.Selected,
BasicInfoForm.Updates
);
Notify("Please fill the required fields")
)
);
ResetForm(BasicInfoForm);
RequestHide();

)

  • BCBuizer Profile Picture
    BCBuizer 21,809 on at
    Re: Need help with If statement on the OnSave event

    Hi @HollieD ,

     

    Glad to see you got this working 🙂 .

  • HollieD Profile Picture
    HollieD 7 on at
    Re: Need help with If statement on the OnSave event

    Thank you so much @BCBuizer! It wasn't exactly correct. There are actually 5 options and there were a couple typos but you definitely got me going in the right direction.

    1. Only BasicForm is submitted
    2. Both BasicForm and ManuallAddForm are submitted
    3. Both BasicForm and CostAdjustmentForm are submitted
    4. Both BasicForm and TransferForm are submitted
    5. A notification is given that required fields are missing.

    Here is what ended up working:

    If(
    (Checkbox1.Value = true && BasicInfoForm.Valid) || 
    (DataCardValue5.Selected.Value = "Disposal" && Checkbox1.Value = false && BasicInfoForm.Valid) ||
    (DataCardValue5.Selected.Value = "Adjust Useful Life / Accelerate Depreciation" && Checkbox1.Value = false && BasicInfoForm.Valid),
     
    SubmitForm(BasicInfoForm); 
    ResetForm(BasicInfoForm);
    RequestHide(),
     
    (DataCardValue5.Selected.Value = "Manual Add" && Checkbox1.Value = false && BasicInfoForm.Valid && ManualAddForm.Valid), 
     
    SubmitForm(BasicInfoForm);
    SubmitForm(ManualAddForm);
    ResetForm(BasicInfoForm);
    ResetForm(ManualAddForm);
    RequestHide(),
     
    (DataCardValue5.Selected.Value = "Cost Adjustment" && Checkbox1.Value = false && BasicInfoForm.Valid && CostAdjustmentForm.Valid), 
     
    SubmitForm(BasicInfoForm);
    SubmitForm(CostAdjustmentForm);
    ResetForm(BasicInfoForm);
    ResetForm(CostAdjustmentForm);
    RequestHide(),
     
    (DataCardValue5.Selected.Value = "Transfer" && Checkbox1.Value = false && BasicInfoForm.Valid && TransferForm.Valid),
     
    SubmitForm(BasicInfoForm);
    SubmitForm(TransferForm);
    ResetForm(BasicInfoForm);
    ResetForm(TransferForm);
    RequestHide(),
     
    Notify("Please fill the required fields")
    )
  • Verified answer
    BCBuizer Profile Picture
    BCBuizer 21,809 on at
    Re: Need help with If statement on the OnSave event

    Hi @HollieD ,

     

    Since you seems to be using FormMode, you can replace all of the patch functions to simplify your formula. Also by combining conditions in a single If statement further simplification can be achieved.

     

    Applying these two, I can to the below formula:

     

    If(
    	(Checkbox1 && BasicInfoForm.Valid)
    	(Checkbox1.Value = true && BasicInfoForm.Valid) || 
    	(DataCardValue5.Selected.Value = "Disposal" && Checkbox1.Value = false && BasicInfoForm.Valid) ||
    	(DataCardValue5.Selected.Value = "Adjust Useful Life / Accelerate Depreciation" && Checkbox1.Value = false && BasicInfoForm.Valid),
    	
    	SubmitForm(BasicForm); 
    	ResetForm(BasicInfoForm);
    	RequestHide(),
    		
    	(DataCardValue5.Selected.Value = "Manual Add" && Checkbox1.Value = false || BasicInfoForm.Valid && ManualAddForm.Valid) ||
    	(DataCardValue5.Selected.Value = "Cost Adjustment" && Checkbox1.Value = false && BasicInfoForm.Valid && ManualAddForm.Valid) ||
    	(DataCardValue5.Selected.Value = "Transfer" && Checkbox1.Value = false && BasicInfoForm.Valid && ManualAddForm.Valid)
    
    	SubmitForm(BasicForm);
    	SubmitForm(ManualAddForm);	
    	ResetForm(BasicInfoForm);
    	ResetForm(ManualAddForm);
    	RequestHide(),
    	
    	Notify("Please fill the required fields")
    )

     

    Basically there are 3 options:

    1. Only BasicForm is submitted

    2. Both BasicForm and ManuallAddForm are submitted

    3. A notification is given that required fields are missing.

     

    The first two options have their own set of conditions, whereas the third is selected if none of the conditions for the first two options are met. 

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,475

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,767

Leaderboard