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 / Can I use UpdateContex...
Power Apps
Unanswered

Can I use UpdateContext witin an If function

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi, 

I have the following code in the OnSuccess method of a forn control:

If(CheckboxAddToOutlookCalendar.Value,
 UpdateContext({
 OutlookEvent: 
 Office365.V2CalendarPostItem(ddCalendar.Selected.Value,Event.EventName, 
 DateAdd( Event.StartTime, -TimeZoneOffset( Event.StartTime ), Minutes ),
 DateAdd(Event.EndTime,-TimeZoneOffset(Event.EndTime),Minutes),
 {
 Body:Event.EventNotes,
 Location:LookUp('[dbo].[Rooms]',RoomId=Event.RoomId,RoomName),
 Reminder:1440,
 ShowAs:"Busy"
 }
 ),
 Enrollment: First(Filter('[dbo].[Enrollments]',EnrollmentId=FormNewEnrollment.LastSubmit.EnrollmentId)) 
 });
 Patch('[dbo].[Enrollments]',Enrollment,
 {OutlookCalendarId:ddCalendar.Selected.Value, OutlookEventId:OutlookEvent.Id});
); 

ResetForm(FormNewEnrollment);
Refresh('[dbo].[View_Enrollment]');

Back();
Back()

 

Powerapps is coinplaining that the  Patch function has some invalid arguments (for boith the  'Enrollment'  and 'OutlookEvent.Id' parameters.  

 

If I move the UpdateContext and Patch statements outside of the If statement , like this:

//If(CheckboxAddToOutlookCalendar.Value,
 UpdateContext({
 OutlookEvent: 
 Office365.V2CalendarPostItem(ddCalendar.Selected.Value,Event.EventName, 
 DateAdd( Event.StartTime, -TimeZoneOffset( Event.StartTime ), Minutes ),
 DateAdd(Event.EndTime,-TimeZoneOffset(Event.EndTime),Minutes),
 {
 Body:Event.EventNotes,
 Location:LookUp('[dbo].[Rooms]',RoomId=Event.RoomId,RoomName),
 Reminder:1440,
 ShowAs:"Busy"
 }
 ),
 Enrollment: First(Filter('[dbo].[Enrollments]',EnrollmentId=FormNewEnrollment.LastSubmit.EnrollmentId)) 
 });
 Patch('[dbo].[Enrollments]',Enrollment,
 {OutlookCalendarId:ddCalendar.Selected.Value, OutlookEventId:OutlookEvent.Id});
//); 

ResetForm(FormNewEnrollment);
Refresh('[dbo].[View_Enrollment]');

Back();
Back()

It works just fine.

 

I am trying to set some local variables from within and If Statement using UpdateContext and using thos local variables within the same if statement,

 

Is this not allowed?

Categories:
  • v-micsh-msft Profile Picture
    Microsoft Employee on at

    The If() function you posted should be not available to get the values for both 

    Enrollment

    and the  

    OutlookEvent

    At the same time.

     

    The Logic of the if() you posted, will only update the OutlookEvent Variable if CheckboxAddToOutlookCalendar.Value is true, and when the value is false, it will only update the Enrollment variable.

     

    If you would like the two variables to be updated at the same time when the CheckboxAddToOutlookCalendar.Value is true, then please modify the formula as below:

     

    If(CheckboxAddToOutlookCalendar.Value,
     UpdateContext({
     OutlookEvent: 
     Office365.V2CalendarPostItem(ddCalendar.Selected.Value,Event.EventName, 
     DateAdd( Event.StartTime, -TimeZoneOffset( Event.StartTime ), Minutes ),
     DateAdd(Event.EndTime,-TimeZoneOffset(Event.EndTime),Minutes),
     {
     Body:Event.EventNotes,
     Location:LookUp('[dbo].[Rooms]',RoomId=Event.RoomId,RoomName),
     Reminder:1440,
     ShowAs:"Busy"
     }
     );
     Enrollment: First(Filter('[dbo].[Enrollments]',EnrollmentId=FormNewEnrollment.LastSubmit.EnrollmentId)) 
     });
     Patch('[dbo].[Enrollments]',Enrollment,
     {OutlookCalendarId:ddCalendar.Selected.Value, OutlookEventId: OutlookEvent.Id});
    ); 
    
    ResetForm(FormNewEnrollment);
    Refresh('[dbo].[View_Enrollment]');
    
    Back();
    Back()

    Change the " , " before Enrollment variable to " ; " , then check to see if this would meet your requirements.

     

    Regards,

    Michael

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 Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 243 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 190 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 135

Last 30 days Overall leaderboard