+ Start a Discussion
Zoom_VZoom_V 

Automating deactivation of User accounts

I am attempting to automate the deactivation of User accounts for terminated employees. In order to streamline the process I am attempting to use Email Services to kick off some code which will look up an account and mark it deactivated upon receiving an email with "Termination" in the subject and the employee ID in the body. 

Here is some code I have which I cannot get to work : 
 
global class InboundEmailHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        if ( email.subject == 'Termination' ) {
            TerminateController.getInstance().terminate(email.plainTextBody);
        }
        return result;
    }
}
 
public class TerminateController() {
     @TestVisible private static final TerminateController INSTANCE = new TerminateController();
    public static TerminateController getInstance() {
        return INSTANCE;
    }
    public void terminate(String commaSeparatedString) {
        String[] employeeids = commaSeparatedString.split(',');
        List<User> users = [SELECT isActive FROM User WHERE employee_Id__c IN: employeeids and isActive = true];
        for ( User u : users ) {
            u.isActive = false;
        }
        update users;
    }

I can't get the controller to save properly. I continuously get an error : "expecting left curly bracket, found '(' at line 1 column 32 "

Any ideas on this ? 

Thank you.
Best Answer chosen by Zoom_V
James LoghryJames Loghry
Yeah, you'll need to construct an instance of the Messaging.InboundEmailResult class and assign it to your result variable.  It should look something like the following:
 
global class InboundEmailHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new new Messaging.InboundEmailResult();
        if ( email.subject == 'Termination' ) {
            TerminateController.getInstance().terminate(email.plainTextBody);
        }
        result.success = true;
        return result;
    }
}

More info on implementing the InboundEmailHandler can be found here: https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com

And information on the various methods and variables of the InboundEmailResult class can be found here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_inbound_result.htm

All Answers

James LoghryJames Loghry

The issue is in the very first line of your Controller class.  Remove the () after TerminateController and you should be good to go.


 

public class TerminateController{
     @TestVisible private static final TerminateController INSTANCE = new TerminateController();
    public static TerminateController getInstance() {
        return INSTANCE;
    }
    public void terminate(String commaSeparatedString) {
        String[] employeeids = commaSeparatedString.split(',');
        List<User> users = [SELECT isActive FROM User WHERE Id IN: employeeids and isActive = true];
        for ( User u : users ) {
            u.isActive = false;
        }
        update users;
    }
}
Zoom_VZoom_V
Thanks a bunch James. I'm working with the InboundEmailHandler class now and I'm getting an error that the "variable doesn't exist" in reference to the 6th line. I'm confused on that. How do I declare that variable ?
James LoghryJames Loghry
Yeah, you'll need to construct an instance of the Messaging.InboundEmailResult class and assign it to your result variable.  It should look something like the following:
 
global class InboundEmailHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new new Messaging.InboundEmailResult();
        if ( email.subject == 'Termination' ) {
            TerminateController.getInstance().terminate(email.plainTextBody);
        }
        result.success = true;
        return result;
    }
}

More info on implementing the InboundEmailHandler can be found here: https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com

And information on the various methods and variables of the InboundEmailResult class can be found here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_inbound_result.htm
This was selected as the best answer