Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/deactivate a SalesForce User through SOAP API?

I want to disable a User programmetically by using SOAP API. How can I do that? I am using Partner API and I have Developer edition. I have manage users persmissions set. I have gone through this link. I am looking for code which can help me disable/deactivate a User.

This is my code:

import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class DeactivateUser {
    public static void main(String[] args) {
        ConnectorConfig config = new ConnectorConfig();

        config.setUsername("[email protected]");
        config.setPassword("sjjhggrhgfhgffjdgj");

        PartnerConnection connection = null;

        try {
            connection = Connector.newConnection(config);
            QueryResult queryResults = connection.query("SELECT Username, IsActive from User");

        if (queryResults.getSize() > 0) {
            for (SObject s : queryResults.getRecords()) {
                if(s.getField("Username").equals("[email protected]")){
                    System.out.println("Username: " + s.getField("Username"));
                    s.setField("IsActive", false);
                }
                System.out.println("Username: " + s.getField("Username") + " IsActive: " + s.getField("IsActive"));
            }
        }
        } catch (ConnectionException ce) {
            ce.printStackTrace();
        }
    }
}

This is output:

Username: [email protected] IsActive: true
Username: [email protected] IsActive: false
Username: [email protected]
Username: [email protected] IsActive: false

However in UI when I go to My Name > Setup > Manage Users > Users, it always show 'Active' check box for user [email protected] selected :-(

like image 692
waprau Avatar asked Sep 14 '25 05:09

waprau


1 Answers

It doesn't look like you're actually sending the update back to Salesforce - you're just setting IsActive to false locally. You will need to use a call to PartnerConnection.update(SObject[] sObjects) in order for Salesforce to reflect your changes, like so:

try {
    connection = Connector.newConnection(config);
    QueryResult queryResults = connection.query("SELECT Id, Username, IsActive from User");

    if ( queryResults.getSize() > 0 ) {
        // keep track of which records you want to update with an ArrayList
        ArrayList<SObject> updateObjects = new ArrayList<SObject>();
        for (SObject s : queryResults.getRecords()) {
            if ( s.getField("Username").equals("[email protected]") ){
                System.out.println("Username: " + s.getField("Username"));
                s.setField("Id", null);
                s.setField("IsActive", false);
            }
            updateObjects.add(s);    // if you want to update all records...if not, put this in a conditional statement
            System.out.println("Username: " + s.getField("Username") + " IsActive: " + s.getField("IsActive"));
        }
        // make the update call to Salesforce and then process the SaveResults returned
        SaveResult[] saveResults = connection.update(updateObjects.toArray(new SObject[updateObjects.size()]));
        for ( int i = 0; i < saveResults.length; i++ ) {
            if ( saveResults[i].isSuccess() )
                System.out.println("record " + saveResults[i].getId() + " was updated successfully");
            else {                        
                // There were errors during the update call, so loop through and print them out
                System.out.println("record " + saveResults[i].getId() + " failed to save");
                for ( int j = 0; j < saveResults[i].getErrors().length; j++ ) {
                    Error err = saveResults[i].getErrors()[j];
                    System.out.println("error code: " + err.getStatusCode().toString());
                    System.out.println("error message: " + err.getMessage());
                }
            }
        }
    }
} catch (ConnectionException ce) {
        ce.printStackTrace();
}
like image 177
JCD Avatar answered Sep 17 '25 19:09

JCD