Thursday, June 05, 2014

Retrive record from salesforce

package a;

import com.sforce.soap.enterprise.EnterpriseConnection;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.GetUserInfoResult;
import com.sforce.soap.enterprise.UpsertResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class Retrive { public String authEndPoint = "";
EnterpriseConnection con;
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args)
{          String arr[] = new String[] {"0019000000vEcw0"};
            if ( sample.login() )
            {
                        sample.retrieveRecords(arr);
            }
}
public Retrive(String authEndPoint)
{
            this.authEndPoint = authEndPoint;
}
public String getUserInput(String prompt)
{
            String result = "";
            try
            {
                        System.out.print(prompt);
                        result = reader.readLine();
            }
            catch (IOException ioe)
            {
                        ioe.printStackTrace();
            }
            return result;
}
public boolean login()
{
            boolean success = false;
            String userId = getUserInput("UserID: ");
            String passwd = getUserInput("Password + Security Token: ");
            try
            {
                        /* Setting up Username and Password */
                        ConnectorConfig config = new ConnectorConfig();
                        config.setAuthEndpoint(authEndPoint);
                        config.setUsername(userId);
                        config.setPassword(passwd);
                        config.setCompression(true);
                        //config.setProxy("Your Proxy", 80);
                        System.out.println("AuthEndPoint: " + authEndPoint);
                        config.setTraceFile("upsertLogs.txt");
                        config.setTraceMessage(true);
                        config.setPrettyPrintXml(true);

                        System.out.println("\nLogging in ...\n");
                        /* Setting up connection to Salesforce */
                        con = new EnterpriseConnection(config);
                        GetUserInfoResult userInfo = con.getUserInfo();   
                        System.out.println("UserID: " + userInfo.getUserId());
                        System.out.println("\nLogged in Successfully\n");
                        success = true;
            }
            catch (ConnectionException ce)
            {
                        ce.printStackTrace();
            }
            catch (FileNotFoundException fnfe)
            {
                        fnfe.printStackTrace();
            }
            return success;
}
public void updateAcct()
{
            try
            {                      
                        /* Getting Account details */
                        SObject[] upserts = new Account[1];
                        Account upsertAccount = new Account();
                        String acctName = getUserInput("Enter Account name:");
                        String acctNo = getUserInput("Enter Account no:");
                        String phoneNo = getUserInput("Enter phone no:");
                       
                        String extId = getUserInput("Enter External Id:");
                       
                        upsertAccount.setName(acctName);
                        upsertAccount.setPhone(phoneNo);
                        upsertAccount.setAccountNumber(acctNo);
                       
                        upsertAccount.setSLASerialNumber__c(extId);
                       
                        upserts[0] = upsertAccount;
                        System.out.println("\nUpserting...");
                        /* Upserting account records */   
                        UpsertResult[] upsertResults = con.upsert("SLASerialNumber__c",upserts);
                        for (UpsertResult result : upsertResults)
                        {
                                    if (result.isSuccess())
                                    {
                                                System.out.println("\nUpsert succeeded.");
                                                System.out.println(
                                                                        (result.isCreated() ? "Insert" : "Update") +
                                                                        " was performed."
                                                );
                                                System.out.println("Account ID: " + result.getId());
                                    }
                                    else
                                    {
                                                System.out.println("The Upsert failed because: " +
                                                                        result.getErrors()[0].getMessage());
                                    }
                        }
            }
            catch (ConnectionException ce)
            {
                        ce.printStackTrace();
            }
}

public void retrieveRecords(String[] ids) {
  try {
     SObject[] sObjects = con.retrieve("ID, Name, Website",
           "Account", ids);
     // Verify that some objects were returned.
     // Even though we began with valid object IDs,
     // someone else might have deleted them in the meantime.
     if (sObjects != null) {
        for (int i = 0; i < sObjects.length; i++) {
           // Cast the SObject into an Account object
           Account retrievedAccount = (Account) sObjects[i];
           if (retrievedAccount != null) {
              System.out.println("Account ID: " + retrievedAccount.getId());
              System.out.println("Account Name: " + retrievedAccount.getName());
              System.out.println("Account Website: "
                    + retrievedAccount.getWebsite());
           }
        }
     }
  } catch (ConnectionException ce) {
     ce.printStackTrace();
  }
}



}

No comments:

Post a Comment

Thanks for your comment