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
JQHellerJQHeller 

A simple C# create new record call to the web services API....Am I doing it right?

Greetings! I am new to the SalesForce world. I am starting up a small project that will use the Web Services API.  So far I've had good luck with accessing the service, pulling data, etc. 

 

Now I've dabbled with creating a new record.  I have it working but I was hoping that someone could look at my code sample and tell me if this is most efficient way.  I fear that I have over-complicated things.

 

My SF object is a simple table (is this even the right word in the salesforce world??) called test with just one field called Name.  Standard stuff.  So to create/insert I am doing the following in c#:

 

        Test__c[] newStuff = new Test__c[1];

        newStuff[0] = new Test__c();
        newStuff[0].Name = "My Test Name";

        String[] createdItemIds = new String[10];
        SaveResult[] sr = binding.create(newStuff);

        for (int i=0;i<sr.Length;i++) {

            if (sr[i].success) {

                Response.Write("<hr/>" + sr[i].id.ToString() + "<hr/>");
                createdItemIds[i] = sr[i].id;

            }

        }

 

 

I built this from some c# sample code that I found on here.  Like I said everything works but the sample code I found was for inserting multiple records at once.  I am curious if there is a cleaner, more direct way to insert a SINGLE record and get the new unique ID back from the webservice.

 

Thanks in advance for any tips or advice!

 

- Jasper

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

Test__c n = new Test__c();

c.name = "my test name";

SaveResult sr = binding.create(New SObject [] {c})[0];

if (sr.success) 

return sr.id; 

throw new Exception(sr.Errors[0].statusCode + ":" + sr.Errors[0].message); 

All Answers

SuperfellSuperfell

Test__c n = new Test__c();

c.name = "my test name";

SaveResult sr = binding.create(New SObject [] {c})[0];

if (sr.success) 

return sr.id; 

throw new Exception(sr.Errors[0].statusCode + ":" + sr.Errors[0].message); 

This was selected as the best answer
JQHellerJQHeller
Simon, thanks for the help. This worked! thanks again!
Mohit MohanMohit Mohan

Hi,

 

could you please send me the code for inserting and inserting record into/from the salesforce respectively.

 

I am geting some error while inserting the record into salesforce.

 

Thanks In advasnce

Mohit