Find your content:

Search form

You are here

SOAP API - Reading first line of an excel file to determine setter methods


I want to use Java to bind to the SOAP API to insert some objects into salesforce from a CSV file. I don't want to use the dataloader or excel connector, because I will be inserting multiple related objects and will be writing code to automate the whole process with a single button press.

My CSV files have the first line defining the names of the fields that I want to insert. I want to keep my solution as flexible as possible, so I don't want to hardcode the order of the fields like the following:

Account acc = new Account();

Instead, I would like to read the first line and perhaps insert the order/name combination into a map and then use this to somehow call the right setter methods for my object (This obviously presumes that the header names are well formed).

I am stuck on how I can call a setter method, or even set a field using the dot notation, in the above scenario using the name of the field. Is there a way to achieve this?


Attribution to: Joe

Possible Suggestion/Solution #1

As you are using Java, you can use the Reflection API

A quick google found this blog post which gives you a brief example. There are lots out there.

Attribution to: Phil Hawthorn

Possible Suggestion/Solution #2

If you use the Partner-wsdl, you'd only have access to SObject fields by name. Obviously a drawback for some use cases, but in your case it might be preferable? Example from the documentation

SObject account = new com.sforce.soap.partner.sobject.wsc.SObject();
account.setField("XID1__c", "1");

Attribution to: Martin Peters
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content