How to Programmatically Bulk Assign Users with ClosePlan Licenses

Please Note: From version 1.244, Users can be bulk assigned by using the new ‘Import’ tool found on the User Manager tab under ClosePlan Admin.

 

 

How to Programmatically Bulk Assign Users with ClosePlan Licenses - The old way

These steps describe how to assign multiple Users at once via the Salesforce Developer Console with

CP Suite full access licenses or Deal Relationships full access licenses.

  1. Create a CSV file with a column header labeled “Id” containing list of User Ids to be assigned

    • The file must contain a column with the Header: “Id”

    • The column will contain all User Ids to be assigned CPSuite Licenses

    • The CSV file will look like this:

Note: The file can also contain other columns such as User Name, First Name, Last Name and/or email for better orientation but a column with the header “Id” must be present.

Note: The quotation marks are required.

 

2. Upload the file as Static Resource

  • Go to Setup > search for Static Resources > New

  • Name: CP_users_list

  • Choose the CSV file you created and Save

 

3. Run script

  • Go to Gear Icon > Developer Console

  • Debug > Open Execute Anonymous Window

  • Copy the correct script see below and enter it into the Enter Apex Code window

Script to assign CP Suite full access licenses:

public static void importUserLicenses(){ String fileUrl = '/resource/CP_users_list'; PageReference pr = new PageReference(fileUrl); String fileContent = pr.getContent().toString(); SSSCsvReader reader = new SSSCsvReader(fileContent); Set<Id> userIds = new Set<Id>(); reader.readLine(); // skip header string[] line; while((line = reader.readLine()) != null){ userIds.add(line[0]); } System.debug(userIds.size()); User[] us = [SELECT Id, Username, IsActive, UserType FROM User WHERE Id IN: userIds]; TSPC.CPUserManagement.UserLicenseRequest[] ulrs = new TSPC.CPUserManagement.UserLicenseRequest[]{}; for(User u : us){ TSPC.CPUserManagement.UserLicenseRequest ulr = new TSPC.CPUserManagement.UserLicenseRequest(); ulr.user = u; ulr.scopes = 'suite:full'; ulrs.add(ulr); } system.debug('requests:' + ulrs.size()); TSPC.CPUserManagement.setUserLicense(ulrs); } public class SSSCsvReader { private String delim = ','; // the input data private String[] buffer; public SSSCsvReader(String data){ this.buffer = data.split('\n'); } public SSSCsvReader(String data, String delim){ this.buffer = data.split('\n'); this.delim = delim; } public String[] readLine(){ if(buffer.size() == 0) return null; String line = this.buffer.remove(0); String[] parts = new String[] {}; while(line != ''){ Integer next = 0; if(line.startsWith('"')){ line = line.substring(1); // strip initial Integer quoteIndex = findQuote(line, 0); while(quoteIndex == -1){ if(buffer.size() == 0){ // EOT! quoteIndex = line.length(); } else { // grab the next line Integer skip = line.length(); line += '\n' + this.buffer.remove(0); quoteIndex = findQuote(line, skip); } } // advance to comma next = quoteIndex + 1; parts.add(line.substring(0, quoteIndex).replace('""', '"')); } else { next = line.indexOf(this.delim, next); if(next == -1) next = line.length(); // NB in Substring, "endindex" is the index of the character AFTER the last index to get parts.add(line.substring(0, next)); } if(next == line.length() - 1) // case of a terminating comma. parts.add(''); line = next < line.length() ? line.substring(next+1) : ''; } if(parts.size() == 0) // empty string - we still want to return something... parts.add(''); return parts; } private Pattern quotePattern = Pattern.compile('(?<!")"(?!")'); private Integer findQuote(String line, Integer skip){ Matcher m = quotePattern.matcher(line); m.region(skip, m.regionEnd()); if(!m.find()) return -1; return m.start(); } } importUserLicenses();

 

Script to assign CP Deal Relationships full access licenses:

public static void importUserLicenses(){ String fileUrl = '/resource/CP_users_list'; PageReference pr = new PageReference(fileUrl); String fileContent = pr.getContent().toString(); SSSCsvReader reader = new SSSCsvReader(fileContent); Set<Id> userIds = new Set<Id>(); reader.readLine(); // skip header string[] line; while((line = reader.readLine()) != null){ userIds.add(line[0]); } System.debug(userIds.size()); User[] us = [SELECT Id, Username, IsActive, UserType FROM User WHERE Id IN: userIds]; TSPC.CPUserManagement.UserLicenseRequest[] ulrs = new TSPC.CPUserManagement.UserLicenseRequest[]{}; for(User u : us){ TSPC.CPUserManagement.UserLicenseRequest ulr = new TSPC.CPUserManagement.UserLicenseRequest(); ulr.user = u; ulr.scopes = 'deal.relations:full'; ulrs.add(ulr); } system.debug('requests:' + ulrs.size()); TSPC.CPUserManagement.setUserLicense(ulrs); } public class SSSCsvReader { private String delim = ','; // the input data private String[] buffer; public SSSCsvReader(String data){ this.buffer = data.split('\n'); } public SSSCsvReader(String data, String delim){ this.buffer = data.split('\n'); this.delim = delim; } public String[] readLine(){ if(buffer.size() == 0) return null; String line = this.buffer.remove(0); String[] parts = new String[] {}; while(line != ''){ Integer next = 0; if(line.startsWith('"')){ line = line.substring(1); // strip initial Integer quoteIndex = findQuote(line, 0); while(quoteIndex == -1){ if(buffer.size() == 0){ // EOT! quoteIndex = line.length(); } else { // grab the next line Integer skip = line.length(); line += '\n' + this.buffer.remove(0); quoteIndex = findQuote(line, skip); } } // advance to comma next = quoteIndex + 1; parts.add(line.substring(0, quoteIndex).replace('""', '"')); } else { next = line.indexOf(this.delim, next); if(next == -1) next = line.length(); // NB in Substring, "endindex" is the index of the character AFTER the last index to get parts.add(line.substring(0, next)); } if(next == line.length() - 1) // case of a terminating comma. parts.add(''); line = next < line.length() ? line.substring(next+1) : ''; } if(parts.size() == 0) // empty string - we still want to return something... parts.add(''); return parts; } private Pattern quotePattern = Pattern.compile('(?<!")"(?!")'); private Integer findQuote(String line, Integer skip){ Matcher m = quotePattern.matcher(line); m.region(skip, m.regionEnd()); if(!m.find()) return -1; return m.start(); } } importUserLicenses();

 

 

  • Click Execute

 

4. Check your results: Go to ClosePlan Admin tab > User Manager and check that Users are successfully assigned the correct Licenses