Skip to content

Instantly share code, notes, and snippets.

@banderson5144
Last active August 25, 2025 12:56
Show Gist options
  • Save banderson5144/4272a67e74fbc99a2b766416a39d744f to your computer and use it in GitHub Desktop.
Save banderson5144/4272a67e74fbc99a2b766416a39d744f to your computer and use it in GitHub Desktop.
API Access Control Cutover Script
OauthToken[] oaList = [
SELECT
LastUsedDate,AppMenuItemId,AppName,User.Profile.Name,User.ProfileId
FROM OauthToken
WHERE User.IsActive = true
/*Put additional filter based on whatever criteria your org seems sufficient*/
ORDER BY AppName
];
Set<Id> profileIds = new Set<Id>();
//Set<String> connAppNames = new Set<String>();
map<String,Set<Id>> appNameProfileIdMap = new map<String,Set<Id>>();
for(OauthToken oa : oaList){
profileIds.add(oa.User.ProfileId);
if(appNameProfileIdMap.containsKey(oa.AppName)){
appNameProfileIdMap.get(oa.AppName).add(oa.User.ProfileId);
}else{
appNameProfileIdMap.put(oa.AppName, new Set<Id>{oa.User.ProfileId});
}
}
ConnectedApplication[] cAppList = [
Select Id,Name
From ConnectedApplication
WHERE Name IN: appNameProfileIdMap.keyset()
ORDER BY Name ASC
];
map<String,Id> cAppIdMap = new map<String,Id>();
for(ConnectedApplication cApp : cAppList){
cAppIdMap.put(cApp.Name,cApp.Id);
}
PermissionSet[] permSetList = [
Select Id,Name,Profile.Name,ProfileId
From PermissionSet
WHERE ProfileId IN: profileIds
];
map<Id,Id> profilePermSetIdMap = new map<Id,Id>();
for(PermissionSet permSet : permSetList){
profilePermSetIdMap.put(permSet.ProfileId,permSet.Id);
}
SetupEntityAccess[] seaListToInsert = new SetupEntityAccess[]{};
for(String cAppName : appNameProfileIdMap.keyset()){
Id cAppId = cAppIdMap.get(cAppName);
Set<Id> profileIds = appNameProfileIdMap.get(cAppName);
for(Id profId : profileIds){
//Add a row for each ConnectedApp/PermissonSet(Profile) combo
SetupEntityAccess sea = new SetupEntityAccess();
sea.SetupEntityId = cAppId;
sea.ParentId = profilePermSetIdMap.get(profId);
seaListToInsert.add(sea);
}
}
//insert seaListToInsert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment