Bridge: How to automatically add users to "Licensed Users Group" or "Other Users Group"?

NeilKY
NeilKY ✭✭✭✭✭
edited 04/04/25 in API & Developers

We're trying to maintain a "group" of users in our Smartsheet instance who are Licensed, so that we can use Smartsheet to share Licensed-user dashboards with them. And if they're not licensed, they would go into "Other Users Group" for a dashboard highlighting features available to free users.

{94CE4CFE-4ABF-49E5-A3DB-F36190FF1629}.png

Currently, we intermittently use the "User List" report to export all users, scrub the data, then import it to the respective groups in Smartsheet. However, We'd LOVE to automate this tedious task!

Has anyone created something like this already, or can you explain to a Bridge noob how to accomplish this, please?

-Neil

Best Answers

  • Paul Newcome
    Paul Newcome Community Champion
    Answer ✓

    Try this for the JS. Make sure that you include the key and value in the module. This is what gives the JS module data to work with.

    Key: userArray

    Value: Data reference to user array output by the API module.

    // INPUT: userArray


    let licensedCsv = [];
    let nonLicensedCsv = [];


    userArray.forEach(user => {

    const userData = {
    lastName: user.lastName,
    firstName: user.firstName,
    name: user.name,
    email: user.email
    };

    if (user.licensedSheetCreator === true) {

    licensedCsv.push(userData);
    } else {

    nonLicensedCsv.push(userData);
    }
    });


    function convertToCSV(array) {
    const header = "lastName,firstName,name,email";
    const rows = array.map(item => `${item.lastName},${item.firstName},${item.name},${item.email}`);
    return [header, ...rows].join("\n");
    }


    let licensedCsvOutput = convertToCSV(licensedCsv);
    let nonLicensedCsvOutput = convertToCSV(nonLicensedCsv);


    return {
    licensedCsv: licensedCsvOutput,
    nonLicensedCsv: nonLicensedCsvOutput
    };

    The above should do the following:

    Each user object will contain lastName, firstName, name, and email properties (these are what get passed when adding a user to a group).

    It will iterate through each user object and check the licensedSheetCreator property. If it is true, it will put the user object in the "licensedCsvOutput" array. Otherwise the object will be put in the "nonLicensedCsvOutput" array.

    Once it has looped through all of the source array, it will convert both of the CsvOutput arrays into an actual csv string and output those.

    image.png
  • NeilKY
    NeilKY ✭✭✭✭✭
    Answer ✓

    Well, the solution was as simple as you said. Except I had to reformat the output to be in JSON format.

    image.png

    {{states.Output CSVs.javascript.run_script.result.licensedUsers}}

    Here's the new code:

    let licensedUsers = [];
    let nonLicensedUsers = [];

    userArray.forEach(user => {
    const userData = {
    email: user.email
    };

    if (user.licensedSheetCreator === true) {
        licensedUsers.push(userData);
    } else {
        nonLicensedUsers.push(userData);
    }
    

    });

    function convertArrayToJson(array) {
    return array.map(item => ({ email: item.email }));
    }

    let licensedUsersJson = convertArrayToJson(licensedUsers);
    let nonLicensedUsersJson = convertArrayToJson(nonLicensedUsers);

    return {
    licensedUsers: licensedUsersJson,
    nonLicensedUsers: nonLicensedUsersJson
    };

    Thank you @Paul Newcome !

    -Neil

Answers

NEW Smartsheet API Documentation - bookmark the updated link! https://developers.smartsheet.com