+ Start a Discussion
Code monkey see code monkey doCode monkey see code monkey do 

Newbie question: Updating a field in a custom table

Hi guys,

 

I'm fairly new to Salesforce and trying to do something pretty simple - update a field in a table which we have created using the Java SOAP API. Using SQL this would be something like:

 

UPDATE Custom_Employees__c

SET Leaving_Date__c='2013-09-04T17:50:45.000Z'

WHERE Employee_Number='0123456789'

 

Despite reading the SOAP API documentation and LOTS of googling I still cannot find any example code (and it looks like SOQL has no Update statement ???)

 

Can somebody please post a link or some example code?

 

Thanks in advance,

Marcus.

Best Answer chosen by Admin (Salesforce Developers) 
wt35wt35

You are right, SOQL can only be used to query data, it doesn't support any DML statements.

 

What you would need to do in your Java code is use subsequently the login(), query(),update() and logout() calls.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_login.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_query.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_update.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_logout.htm

 

There is a simple code in each of these links

All Answers

wt35wt35

You are right, SOQL can only be used to query data, it doesn't support any DML statements.

 

What you would need to do in your Java code is use subsequently the login(), query(),update() and logout() calls.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_login.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_query.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_update.htm

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_logout.htm

 

There is a simple code in each of these links

This was selected as the best answer
Code monkey see code monkey doCode monkey see code monkey do

Thanks that helped a lot. For anyone else interested here's the code in a nutshell

 

//Open connection to Salesforce
config=new ConnectorConfig();
config.setUsername(strLoginName);
config.setPassword(strPassword+strSecurityToken);
config.setProxy(strProxy, 8080);
conn=Connector.newConnection(config);
conn.setAllowFieldTruncationHeader(true);    //Allow truncation if insert data too long
System.out.println("Opened Salesforce connection");

//Create update buffer. Up to 200 rows can be written at once
Vector<SObject> updateList = new Vector<SObject>();

//Get the Employee record(s) we want to update. In this case there is only one.
String strSOQL="SELECT Id FROM PDTLC_Employee__c WHERE Name='"+strEmpNo+"'";
QueryResult queryResult=conn.query(strSOQL);
SObject[] results=queryResult.getRecords();
String strId=results[0].getId();

//Now create a new SObject for each row we want to update. It must contain a Salesforce Id field
SObject employee = new SObject();
employee.setType("Custom_Employee__c");
employee.setField("Id", strId);
employee.setField("Leaving_Date__c", javaDate);

//Add the employee to the update buffer
updateList.addElement(employee);

//Write the update buffer
SaveResult[] saveResults = conn.update(updateList);

//Iterate through saveResults to spot any errors
for (int i = 0; i < saveResults.length; i++) {
    if (saveResults[i].isSuccess()) {
        intOK++;
    } else {
        //Report all errors
        System.out.println("Error: could not create sobject for array element "+i);
        Error[] errors = saveResults[i].getErrors();
        for (int j=0; j<errors.length; j++){
            System.out.println("\tError Code: "+errors[j].getStatusCode()+" / Message="+errors[j].getMessage()+" / Fields: "+implodeArray(errors[j].getFields(), ","));
        }
        intErr++;    //1 error per document not written
    }            
}
System.out.println(">>>>> "+intOK+" rows written OK / "+intErr+" Errors");

 

Slightly longer than one line of SQL but it works :)