4
votes

We have our system from where we want to push the records (e.g. Contact, Account, Opportunity, etc.) to SalesForce.

To achieve this, we have used ForceToolKit for .Net. We are able to insert\update the records successfully using the ForceToolKit functions.

Example:

dynamic contact = new ExpandoObject();
contact.FirstName = "FirstName";
contact.LastName = "Last";
contact.Email = "[email protected]";
contact.MobilePhone = "1234567890";
var successResponse = await forceClient.CreateAsync("Contact", contactList);

The Issue we are facing is as mentioned below.

In our source system, we have few custom fields which are not standard field in SalesForce and it can be different for different users.

So, first we have to map the custom fields between our source system and the SalesForce.

We want to fetch all the fields of SalesForce object in order to map the fields.

We are unable to find any function in ForceToolkitForNet. As described here, we have tried QueryById function by using the dynamic return type, but it is throwing an exception.

var contactFields = await forceClient.QueryByIdAsync<dynamic>("Contact", successResponse.Id);

Exception: "SELECT FROM Contact where Id = '{contactId}' Incorrect syntax near FROM".

What are the ways to get the fields of any object of SalesForce. Can anyone help us on getting the fields of an object using SalesForceToolkit or SalesForceApi?

1

1 Answers

3
votes

SOQL doesn't have a SELECT * FROM Account concept, you need to learn the fields beforehand. You have few options here.

You can use "describe" calls. I've recently answered similar question but it was about REST API: https://stackoverflow.com/a/48436870/313628, I think your solution is SOAP-based.

If you'd be doing this by hand...

As you're using the toolkit - there's a chance these methods already are in there somewhere. Experiment a bit. Between the C# examples under the links I gave you and the library you should be able to figure something out.

I mean I'm naive but just searching the repo for "describe" looks promising: https://github.com/developerforce/Force.com-Toolkit-for-NET/search?utf8=%E2%9C%93&q=describe&type=, maybe this example

There's also a way to not learn this info at runtime but consume a WSDL file generated out of Salesforce with all fields & generate C# code out of it. The downside is that you'd need to regenerate it every time you want to support new object/field in your app. Read about "Enterprise WSDL" to get started.

Last but not least - there are more or less friendly tools that dump the Salesforce metadata. i'm a fan of https://www.datasert.com/products/realforce/ but just have a look around. Hell, even exporting some records with DataLoader will give you all field names if that's all you need.