Salesforce Mobile Dev Part 2 – APEX Controller

Written by ShamrockCRM on July 27, 2012 – 1:08 pm

Yesterday, I posted the VisualForce code for developing a mobile “insert user” page. ┬áToday, I am posting the details on the APEX Controller required to perform the heavy lifting in Salesforce.

This APEX Class will perform a few tasks: 1) It will query the available Roles, User License types, and Profiles for population into the dropdown select lists. 2) Perform an “insert” of the new user into the database. 3) Return the DML error message if an error occurred (e.g. username already exists, etc.).

Currently, this code is only enabled to work with the “Salesforce” User License type. This could be enhanced to allow for Force.com platform licenses, Chatter licenses, etc.

global with sharing class usermanagerController {

    @RemoteAction
    global static String insertUser(String firstName, String lastName, String email, String profile, String role, String sendEmail) {
        try {
            String alias = firstName.substring(0, 2) + lastName.substring(0, 2) + email.substring(0, 2);
            User u = new User(IsActive = true, FirstName = firstName, LastName = lastName, Email = email, Username = email, ProfileId = profile, UserRoleId = role, Alias = alias, DefaultGroupNotificationFrequency = 'D', DigestFrequency = 'D', EmailEncodingKey = 'ISO-8859-1', LanguageLocaleKey = 'en_US', LocaleSidKey = 'en_US', TimeZoneSidKey = 'America/Los_Angeles', UserPermissionsCallCenterAutoLogin = false, UserPermissionsMarketingUser = false, UserPermissionsOfflineUser = false);

            if (sendEmail == 'on') {
                Database.DMLOptions dlo = new Database.DMLOptions();
                dlo.EmailHeader.triggerUserEmail = true;
                u.setOptions(dlo);
            }
            insert u;
        } catch (DmlException e) {
            return e.getDmlMessage(0);
        }
        return 'true';
    }

    @RemoteAction
    global static List < Profile > queryProfile() {
        //Only works for SFDC, enable later for Force.com with a dependent picklists in jquery
        ID salesforceUserLicense = [Select Id from UserLicense where Name = 'Salesforce'
        limit 1].Id;
        String profileQuery = 'Select Id, Name, UserLicenseId From Profile where UserType = \'Standard\' And UserLicenseId = :salesforceUserLicense ORDER BY Name ASC';
        List < Profile > p = Database.query(profileQuery);
        return p;
    }

    @RemoteAction
    global static List < UserRole > queryRole() {
        String roleQuery = 'Select Id, Name From UserRole where PortalType = \'None\' ORDER BY Name ASC';
        List < UserRole > r = Database.query(roleQuery);
        return r;
    }

    @RemoteAction
    global static List < UserLicense > queryUserLicense() {
        String ulQuery = 'Select Id, Name, LicenseDefinitionKey From UserLicense where Name =\'Salesforce\' limit 1';
        List < UserLicense > ul = Database.query(ulQuery);
        return ul;
    }
}

Next, I will post details on the JavaScript.


Tags: , , , , , , ,
Posted in Force.com User Adoption, HTML5, Salesforce Mobile, Salesforce.com | Comments Off on Salesforce Mobile Dev Part 2 – APEX Controller