function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gowdagowda 

Single Account Creation

Folks

I have the following code,To create an Account  I compare our Account Number(our Database) against the Account Number present in the SalesForce, If it is equal , I need to updated the exiting Account Details in SalesForce , If its not equal , I need to create an Account in the SalesForce with that Account Number ,

Below is the code for that, for the testing purpose i am just passing some dummy Account Number compare against the same in Salesforce, If its equal i don't do anything now(currently) , but if its not equal i am creating an Account ,

The problem is that its creating multiple Accounts with the same Account Number , I think there is some problem in the LOOP , I was not able to figure out what was it..But if any one could figure out the problem that would be really great..

 

accountQuery = bindingStub
     .query("select Name,AccountNumber,Account_Id__c from Account");

sObjects = new SObject [1];
  Account sfAcct = new Account();
  Account account = new Account();

if (accountQuery.isDone()) {
   // Iterate through the records and process them
   for (int i = 0; i < accountQuery.getRecords().length; i++) {
    sfAcct = (Account)accountQuery.getRecords(i);
    acctNumber = sfAcct.getAccountNumber();
    acctId = sfAcct.getAccount_Id__c();

    System.out.println("Account Number & Account Id are = :"
      + acctNumber + "::" + acctId);
    
                if(acctNumber != null ) {
                 if(!(acctNumber.equals(acctNo))){
              System.out.println("inside IF LOOP");
     //create an account in salesforce
              account.setAccountNumber(acctNo);
              account.setAccount_Id__c(testId);
              account.setName("McDonalds");
     sObjects[0] = account;

     try {
      System.out.println("creating objects");
      saveResults = bindingStub.create(sObjects);
     } catch (UnexpectedErrorFault e1) {
      e1.printStackTrace();
     } catch (InvalidSObjectFault e1) {
      e1.printStackTrace();
     } catch (RemoteException e1) {
      e1.printStackTrace();
     }

    } else {
     // do update on the existing info in salesforce

    }
   }
  }

DevAngelDevAngel

Hi Gowda,

You have an unbounded query that is returning all your accounts.  In your loop you are using the length of the records array as the limit so that you will create one account for every existing account returned by the query (up to 2000 as that is the max number of records returned in the query.

Change your for statement to

for (int i=0;i<sObjects.length();i++)

and it will only create the number of objects that are dimensioned in your sObject array.

 

 

Cheers

gowdagowda

So how do i set the sObject array ,

SObject [] sObject = new SObject[];

accountQuery.getRecords().length ?

 

Let me know about this.

 

DevAngelDevAngel

You are querying to return all accounts.  Not sure why.

 

To create a single account you can do this.

Account act = new Account();

act.setName = "some name";

binding.create(new SObject[] {act});

This will create a single account.