Before I say anything, Dineshkumar Sivakumar has written a superior quality blog on the flow, please do read his blog for details regarding the flow structure, I have not deep dived into the flow as he has taken the effort to explain this flow exhaustively -https://thetechiedood.wordpress.com/2021/05/05/assign-remove-security-roles-to-users-via-power-automate/
Linkedin – https://www.linkedin.com/embeds/publishingEmbed.html?articleId=8650792220427939372
So recently I was testing a few security roles out, I am sure we all have been here at some point or the the other where you have either a previlae set for read and want to make sure you can’t write or you have a rule set to hide a button based on a security role etc.. the list is never ending.
Well, in my case I only had one user and was clueless how to proceed, the second I would remove my System administrator role I would not be able to re-assign my role back, the only way out was to bug someone to promote me to admin again.
Below are the list of things I tried and since I had the basic outline of the flow ( once again thank you @Dineshkumar ) a few updates to it would make it a beautiful looking app with a lot of functionality.
I used the new PowerApps connector that allows you to take variables as parameters from the PowerApps to the flow –

Let’s begin with what to not do to start – Initially I started with adding dataverse as a data source in the form of a connection, this looked very neat and worked flawlessly till I changed my security role to a non system admin – power apps with dataverse as a connection takes Your security role into consideration, so the second you remove your security role you loose access to the tables in your app – #DONT_BE_A_KARAN
But in case you want to have a drop down approach you can use the below formula ( provided you are not changing your own role )

Set(result,’PowerAppV2->GetarowbyID2,GetarowbyID’.Run(Dropdown3.Selected.User,Dropdown2.Selected.Role,”1″));If(result.success=”yes”,Notify(“Success”),Notify(“Failed”));
The best part of using this is it takes the GUID directly from the table so no more nested loops to get the GUID from the name or email address.
My next approach was to use a text input field and take the email address and security role name from the user as input; the formula on the ADD Security role button would look something like –
Set(result,’PowerAppV2->GetarowbyID2,GetarowbyID’.Run(TextInput3,TextInput4,”1″));If(result.success=”No”,Notify(“Failed”),Notify(“Success”));
The above formula creates a global variable ( called result ) and becomes an object of the result,
PowerAppV2->GetarowbyID2,GetarowbyID’.Run = Flow name
TextInput3,TextInput4,”1″ = Parameters; the first one is the email address and the second one is the security role name. The Value “1” is a flag value to direct the flow to either add or remove the security role. In my case 1 is used on the add button and 0 for the remove button.
If(result.success=”No”,Notify(“Failed”),Notify(“Success”)); = A condition to check if the returned value from the flow is equal to “No” and notify accordingly. { the structure of IF is if(condition,success action, failure action );}
Now let us begin with the flow –
I encapsulated the list conditions to find the user and role along with the other actions in a scope, the next action sets the output variable to No if the scope fails and hence decides the notification in the app

Notice the condition in the above picture, the condition checks the value of the third parameter to decide if you are adding or removing a security role.

For the next actions I followed Dineshkumar’s blog; posting it here once again https://thetechiedood.wordpress.com/2021/05/05/assign-remove-security-roles-to-users-via-power-automate/
I used the relate action by passing the GUID of the role in a static formatted URL which is a little different from the blog but the idea remains the same

And the working would look like –
You can now add this on teams too by following – https://powerapps.microsoft.com/en-us/blog/add-your-canvas-apps-to-microsoft-teams/

I hope this helps 🙂