Skip to main content
The below  licenses are of manages packages assigned to a active user , On user creation page . when once user is made inactive , I want to de-activate all the manage packages  using trigger . can you please tell me how to write trigger .

trigger to remove managed package licenses when user becomes inactive
1 respuesta
  1. 17 jul 2016, 15:45
    Hi,

    Please try below code. 

    public void doRemoveLicense(List<id> inactiveUser) {

    if(inactiveUser != null && inactiveUser.size() > 0 ){

    Set<Id> packLicnSet = new Set<Id>();

    //SOQL to get the package license/installed package Id

    List<PackageLicense> pckgLicenseList = [SELECT Id, NamespacePrefix FROM PackageLicense WHERE NamespacePrefix = 'TestPackage' OR NamespacePrefix = 'test'];

    system.debug('pckgLicenseList===' +pckgLicenseList);

    if(pckgLicenseList != null && pckgLicenseList.size() > 0){

    List<UserPackageLicense> uplList = new List<UserPackageLicense>([select id,PackageLicenseid,Userid from UserPackageLicense where PackageLicenseid IN :pckgLicenseList AND userId IN :inactiveUser]);

    List<UserPackageLicense> upl_removeList = new List<UserPackageLicense>();

    for(UserPackageLicense ux : uplList) {

    //Removing the license to the current user

    upl_removeList.add(ux);

    }

    if(!upl_removeList.isEmpty()) {

    try{

    delete upl_removeList; //DML operation

    }catch(Exception e){

    system.debug(' delete exception: '+ e.getStackTrace() + ' \n exception message :'+e.getMessage());

    }

    }

    }// end of Inactive user list

    }

    }

    Pass on inactive user list to this method. 

    Hope this will helps you. 

    Thanks,

    Gaurav

    Email: gauravgarg.nmims@gmail.com