Organization Chart using Visualforce and Google Visualization API

1:42 PM

The below diagram is what we are going to build. This hierarchy is based upon the manager field in the user object. The below example is NOT based upon the role hierarchy. Anyhow, you can modify the code below to fit the role hierarchy.

Visualforce Page:

<apex:page controller="orgchart" showHeader="false">
    <script type='text/javascript' src=''></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {packages:['orgchart']});
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
        chart.draw(data, {allowHtml:true});

    <div id='chart_div' style="font-size:18px;"></div>

Apex Class
public class orgchart {

Public String getuserdata()
    List<User> allusers = [Select Name,Manager.Name from User where IsActive = TRUE and ManagerId != NULL];
    String datastr = '';
    for (integer i=0;i<allusers.size();i++)
        datastr = datastr + '[\'';
        datastr += allusers[i].Name;
        datastr+= '\',\'';
        datastr+= allusers[i].Manager.Name;
    datastr = datastr + '';
    return datastr;



  1. This is nice, how can we modify this so the chart fits the hierarchy ?

  2. Ho to do a multi-level hierarchy? How many levels are possible?

  3. I think there is no limit on the level of hierarchy, you can just keep adding datarows and the hierarchy expands

  4. This will not work, until we need to remove the comma at the end. Include this line after the for loop ends.
    datastr = datastr.substring(0, datastr.length() -1);
    datastr = datastr + '';
    return datastr;

  5. Thanks for pointing that out Viswa. Also, it looks like a single datastr variable can display a maximum of 50 separate hierarchy divisions..

  6. This comment has been removed by the author.

  7. Thanks,This is nice information about organizations.
    A big thank you for your blog article.Organization charts definitely help to structure the employees and work flow environment.

  8. I am getting this error System.StringException: Ending position out of bounds: -1
    can any one why it is coming

  9. I used the same code but i didnt got any output.can any one help