Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Salesforce by (11.9k points)

I want to disable a User programmatically by using SOAP API. How can I do that? I am using Partner API and I have Developer edition. I have to manage the users permissions 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 shows 'Active' checkbox for user [email protected] selected :-(

1 Answer

0 votes
by (32.1k points)

So maybe you're not sending the update back to Salesforce. Looks like you're just setting  IsActive to invalid provincially. You will need to use a call to PartnerConnection.update(SObject[] sObjects) in order for Salesforce to return 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();

}

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...