function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Agustina GarciaAgustina Garcia 

Assign Permission Set to Connected App via code

Hi there,
My org has a PS and a Connected App and I would like to write a piece of code that do the assignment for me after installing a package. 

Running this piece of code from Dev Console, I get it work
SetupEntityAccess sea = new SetupEntityAccess(
   ParentId = ps.Id,
   SetupEntityId = connectedApp.Id
);
insert sea;

However it doesn't work from instalation script. In order to test it quickly I also created tab related to a VF page with a button that calls this code and it neither works.

I guess the reason is that Dev Console has more priviledges to run code rather than vf page or post instalation script. But I would like to find a way to make it work

Thanks!
ANUTEJANUTEJ (Salesforce Developers) 
Hi Agustina,

I have not checked this but can you try using the runas below is the link for implementation  I hop this helps.

>>https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Regards,
Anutej
Agustina GarciaAgustina Garcia
Hi Anutej,

Thanks for your response but I think you can only use runas during testing. Your link is also under that section.

Regards
Agustina
ANUTEJANUTEJ (Salesforce Developers) 
Oh, I apologize I see that and I checked if there is a way but unfortunately I couldn't find one however I found an idea on idea exchange that has similar description that could be of help.

>>https://success.salesforce.com/ideaView?id=08730000000a16jAAA

In case if this comes to use can you please choose this as the best answer so that it is easily accessible to others in the future.
Agustina GarciaAgustina Garcia
Thanks Anutej,

So, at this moment, June 2020, there is no way to run apex code as system admin, but I will follow the idea (https://success.salesforce.com/ideaView?id=08730000000a16jAAA) you found just in case it changes in the future. 

Regards,
Agustina 
ANUTEJANUTEJ (Salesforce Developers) 
Sure Agustine. :)