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
AWilkinsonAWilkinson 

Adding Contacts

Is there a way to add contacts through the java api coding?

if so, could you post the coding to perform the action?

thank you.

Message Edited by AWilkinson on 08-01-2003 08:35 AM

DevAngelDevAngel

Hi AWilkinson,

You can find sample code for inserting data on the sforce website at http://www.sforce.com/us/resources/code.jsp.

The pertinent code is excerpted below:

  org.w3c.dom.Element value = null;
  salesforce.MapEntry[] record = new salesforce.MapEntry[2];

  record[1] = new salesforce.MapEntry();
  record[1].setKey("industry");
  record[1].setValue("Software");
  record[0] = new salesforce.MapEntry();
  record[0].setKey("name");
  record[0].setValue("newaccount");

  value = (org.w3c.dom.Element)binding.insert("account", record);

Download the sample to obtain context for this snippet.

AWilkinsonAWilkinson
I want to add contacts under the Account field, am I missing thepart of the code that does this?

in salesforce.com I would like to click on the accounts tab, select the account and within the account will be contacts under the account information, I believe I need to write a new struct to do this but I just wanted to make sure there was no other way. thank you
DevAngelDevAngel

Hi AWilkinson,

The contact contains a field called accountID.  If you inspect the properties of this field you will see a property called "referenceTo".  Under this property it will have a value of account.  So to relate a contact to an account you will need to set this field to the account id that you want to relate the contact to.  This means that you need to obtain the account id either as a result of a query or an insert.  All inserts work the same way.  You build the struct using the pertinent fields for the entity that you are inserting.  This is referred to as a record.  You call the insert method and pass the method the name of entity you are inserting and the record that describes the entity.

So, at a minimum for a standard contact (one that has not been modified from the original provisioning), you would need the following:

  org.w3c.dom.Element value = null;
  salesforce.MapEntry[] record = new salesforce.MapEntry[1];

  record[0] = new salesforce.MapEntry();
  record[0].setKey("lastName");
  record[0].setValue("Wilkinson");
  value = (org.w3c.dom.Element)binding.insert("contact", record);

AWilkinsonAWilkinson
 outputNode = XPathAPI.selectSingleNode(fldNode, "referenceTo/text()");
    if (outputNode != null) System.out.println("    referenceTo: " + outputNode.getNodeValue());
    else System.out.println("    referenceTo: (na)");


this part was in the main Demo page, is this what you were referring to?
DevAngelDevAngel
Yes
AWilkinsonAWilkinson
works like a charm, thanks for your time!!