To insert data from external application to Salesforce, create() method is used.
create ()
create () add one or more new records to your organization’s data. Use create () to add one or more records, such as an Account or Contact record, to your organization’s information. create () call is analogous to the INSERT statement in SQL.
Syntax
SaveResult[] = connection.create(sObject[] sObjects);
where connection is an object of EnterpriseConnection.
Sample JAVA Program to Insert data into Salesforce
package wsc;
public class InsertAccount
{
public String authEndPoint = “”;
EnterpriseConnection con;
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
InsertAccount sample = new InsertAccount(“https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r”);
if ( sample.login() )
{
sample.insertAcct();
}
}
public InsertAccount(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(“insertLogs.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 Successfullyn”);
success = true;
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
return success;
}
public void insertAcct()
{
/* Getting Account details */
String acctName = getUserInput(“Enter Account name:”);
String acctNo = getUserInput(“Enter Account no:”);
String phoneNo = getUserInput(“Enter phone no:”);
String extId = getUserInput(“Enter External Id:”);
Account acct = new Account();
/* Setting Account details */
acct.setName(acctName);
acct.setPhone(phoneNo);
acct.setAccountNumber(acctNo);
acct.setExternal_ID__c(extId);
Account[] accts = { acct };
try
{
System.out.println(“nInserting…n”);
/* Inserting account records */
con.create(accts);
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
System.out.println(“nAccount has been inserted successfully.”);
}
}
Output:
UserID: [email protected]
Password + Security Token: chennaiuytrfghikolmnhB1kFNW9kEw
AuthEndPoint: https://login.salesforce.com/services/Soap/c/24.0/0DF90123456lko8r
[WSC][InsertAccount.login:61]Log file already exists, appending to insertLogs.txt
Logging in …
UserID: 00590000000qXHsAAM
Logged in Successfully
Enter Account name:Test SFDC POC
Enter Account no:82827
Enter phone no:8877665544332
Enter External Id:90
Inserting…
Account has been inserted successfully.