+ Start a Discussion
PreussenBlauPreussenBlau 

Java - Update an Array of Accounts Previously Selected

In Java using a partner WSDL for the API:

 

I'd like to run a query, selecting several accounts based on the value of a particular field and then clear a few fields on the Account object for that list of accounts that was previously selected. 

 

Do I need to iterate through the list of accounts and update them one at a time or can I code something similar to the Apex language that allows me to update all the accounts with one line of code? 

 

APEX Example

global void execute(Database.batchableContext info, List<Account> scope){

List<Account> accsToUpdate = new List<Account>();

for(Account a : scope){

a.name = 'true';

a.numberOfEmployees = 69;

accsToUpdate.add(a);

}

update accsToUpdate;

}

Best Answer chosen by Admin (Salesforce Developers) 
PreussenBlauPreussenBlau

Hi Dinesh,

 

I added the line setting the type to Account but that didn't really fix the issue.  I did find a line of code for casting an array which a co-worker was able to help me utilize (he is a "true" java developer, not a part timer like me). 

 

String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

 

By using the example line of code above, we were able to correclty convert the list.  I changed some of the variables in the code from previous updates to make things clearer.  This works!

 

I do sincerely appreciate your assistance and encouragement!

 

Thanks,

Tom

 

List<SfdcAccount> sfdcAccountList = new ArrayList<SfdcAccount>();
      for (Account account : accounts) {

         SfdcAccount clearReinAccount = new SfdcAccount();

         clearReinAccount.setId(account.getSfdcId());
         clearReinAccount.setFieldsToNull(new String[] { "REIN_CAPTV_DESC__c", "REIN_CAPTV_BEG_DT__c", "REIN_CAPTV_END_DT__c" });
         sfdcAccountList.add(clearReinAccount);
      }
      try {
         // call the Salesforce API
         SoapBindingStub binding = SfdcSessionManager.getSoapBindingStub();

         SObject[] sobjectArray = Arrays.copyOf(sfdcAccountList.toArray(), sfdcAccountList.size(), SObject[].class);
         SaveResult[] saveResults = binding.update(sobjectArray);

 

 

 

All Answers

NasipuriNasipuri

Yes ,

 

The Force.com web services have also Update method and it takes Array of sObject as argument.

 

But the difference is that you need to do the Login Call to establish the session between the client application and the SFDC , and need to initiate instance of SOAPBindingStub through which you need to use the Update method.

 

Please refer to the Force.com web services API guide ( available at http://wiki.developerforce.com/index.php/Documentation) and sample code.

 

Thanks,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com

PreussenBlauPreussenBlau

Thanks, Dinesh!

 

I appreciate your response.  I've searched around in the web services documentation and elsewhere today as I did previously but I still am not able to find an example.  It helps to know that it's possible and I'm not wasting my time.  But would you happen to have a simple example?  Thanks!

NasipuriNasipuri
private void updateAccountSample()
{
// create the account object to hold our changes
SObject updateAccount = new SObject();
updateAccount.setType("Account");
// need to have the id so that API knows which account to update
updateAccount.setId(new ID("001x0000002kuk1AAA"));
// set a new value for the name property
MessageElement[] ufields = new MessageElement[1];
ufields[0] = new MessageElement(new QName("Name"),"New Account from Update Sample");
updateAccount.set_any(ufields);
// create one that will throw an error
SObject errorAccount = new SObject();
errorAccount.setType("Account");
errorAccount.setId(new ID("SLFKJLFKJ"));
// set the value of Name to null
errorAccount.setFieldsToNull(new String[] {"Name"});
// call the update passing an array of object
// assuming that you've already established an enterprise WSDL binding
try {
SaveResult[] saveResults = binding.update(new SObject[] {updateAccount,
errorAccount});
//loop through the results, checking for errors
for (int j = 0; j < saveResults.length; j++) {
System.out.println("Item: " + j);
if (saveResults[j].isSuccess()) {
System.out.println("An account with an id of: " +
saveResults[j].getId() + " was updated.\n");
}
else {
System.out.println("Item " + j + " had an error updating.");
System.out.println(" The error reported was: " +
saveResults[j].getErrors()[0].getMessage() + "\n");
}
}
} catch (Exception ex) {
System.out.println("An unexpected error has occurred." + ex.getMessage());
return;
}
}

 This is the example for JAVA , available in the Force.com web service API guide , whare binding is an instance of SoapBindingStub , need to set up by Login call.

 

Thanks and Regards,

Dinesh

PreussenBlauPreussenBlau

Thanks again, Dinesh.  The example was not quite what I was looking for but it does definitely help.

 

Maybe you can help further.  Let me run the code below by you as it illustrates more precisely what I'm trying to accomplish. 

privatevoidclearAccountReinsuranceData2 (List<Account> accounts)
    throwsRuntimeException
{
List<SfdcAccount> sobjects = newArrayList<SfdcAccount>();

    for(Account account : accounts) {
        SfdcAccount clearReinAccount =  newSfdcAccount();
        clearReinAccount.setId(account.getSfdcId());
        clearReinAccount.setFieldsToNull{new String[] { "REIN_CAPTV_DESC__c", "REIN_CAPTV_BEG_DT__c"});
        sobjects.add(clearReinAccount);
    }
    
try{

    // call the Salesforce API
       SoapBindingStub binding = SfdcSessionManager.getSoapBindingStub();

   //SaveResult[] saveResults = binding.update((SObject[]) sobjects.toArray());
   //SaveResult[] saveResults = binding.update(newSObject[] { (SObject) sobjects });
   //From SF Example - SaveResult[] saveResults = binding.update(new SObject[] {updateAccount,errorAccount});

 I can compile this code using either of the two SaveResult [ ] lines of code but both cause errors when I try to run the program.

 

                   error message: java.util.ArrayList cannot be cast to com.sforce.soap.partner.sobject.SObject

 

Can you help or provide some clues for this last line of code that is currently commented out above?

NasipuriNasipuri

You are almost there.

 

Just need to put a line ,

 

clearReinAccount.type = "Account";

 

To set the which SObject type you are using , Account or Contact or some thing else.

 

Thanks,

Dinesh

PreussenBlauPreussenBlau

Hi Dinesh,

 

I added the line setting the type to Account but that didn't really fix the issue.  I did find a line of code for casting an array which a co-worker was able to help me utilize (he is a "true" java developer, not a part timer like me). 

 

String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

 

By using the example line of code above, we were able to correclty convert the list.  I changed some of the variables in the code from previous updates to make things clearer.  This works!

 

I do sincerely appreciate your assistance and encouragement!

 

Thanks,

Tom

 

List<SfdcAccount> sfdcAccountList = new ArrayList<SfdcAccount>();
      for (Account account : accounts) {

         SfdcAccount clearReinAccount = new SfdcAccount();

         clearReinAccount.setId(account.getSfdcId());
         clearReinAccount.setFieldsToNull(new String[] { "REIN_CAPTV_DESC__c", "REIN_CAPTV_BEG_DT__c", "REIN_CAPTV_END_DT__c" });
         sfdcAccountList.add(clearReinAccount);
      }
      try {
         // call the Salesforce API
         SoapBindingStub binding = SfdcSessionManager.getSoapBindingStub();

         SObject[] sobjectArray = Arrays.copyOf(sfdcAccountList.toArray(), sfdcAccountList.size(), SObject[].class);
         SaveResult[] saveResults = binding.update(sobjectArray);

 

 

 

This was selected as the best answer