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
Kam.ax1015Kam.ax1015 

REST API - Inserting complex transaction

Hello,

 

Use case is...

 

Using REST API, as part of one transaction (i.e. one RESt API call), I need to insert order, order details and create an account.

 

Is this possible?

 

Thanks,

Kam

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

Here's an example that allows you to send a POST to create an account and a list of contacts in a single transaction.

 

    @HttpPost
    global static string create(Account account, List<Contact> contacts) {
        insert account;
        for (Contact c : contacts)
            c.accountId = account.Id;
        insert contacts;
        return account.id;
    }

 

 

and a sample payload

 

 

{
    "account" : {
        "attributes": { "type": "account" },
	    "name" : "test account",
		"website" :"http://www.superfell.com"
    },
	"contacts" : [
		{ "attributes" : { "type" :"account"},
		   "firstName" :"Simon",
		    "lastName" :"Fell" },
		{ "attributes" : { "type" :"account"},
		   "firstName" :"Dan",
		    "lastName" :"Kador" }
	]
}

 

and i tested this with curl, like this. 

curl -v -H "Authorization:OAuth $sid" https://prerelna1.pre.salesforce.com/services/apexrest/sfell/foo --data-binary @acc.json -H "Content-Type:application/json"

> POST /services/apexrest/sfell/foo HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: prerelna1.pre.salesforce.com
> Accept: */*
> Authorization:OAuth <someSidWasHere>
> Content-Type:application/json
> Content-Length: 352
< HTTP/1.1 200 OK
< Server: 
< Content-Type: application/json; charset=UTF-8
< Content-Length: 20
< Date: Thu, 02 Jun 2011 23:04:27 GMT
"001x0000002JcMqAAK"

 

 

All Answers

SuperfellSuperfell

You can't do this with the standard API, you can build a custom apex web services to do this (or build a custom apex rest service to do this, which is in pilot)

Kam.ax1015Kam.ax1015

Hello,

 

 

My account has Rest framework enabled. So I tried following instructions in 
http://www.salesforce.com/us/developer/docs/apex_rest/api_apex_rest.pdf

When I edit the class I get an error,
Error: Compile Error: Unknown annotation: RestResource at line 1 column 2


@RestResource(urlMapping='/Account/*')

 

So there are 2 issues here...

1- Looks like custom API can take only primitive types

2 - Is the above right documentation? If not are there any samples to post complex REST/json to controller?

 

 

Thanks a lot.

 

Kam

SuperfellSuperfell

Most instances haven't yet been updated to the summer '11 instance, which instance are you testing on ?

SuperfellSuperfell

Also make sure you've set the API version to v22.

Kam.ax1015Kam.ax1015

Hello Simon,

 

I am using https://na3.salesforce.com/  I guess that is the instance name you are asking for right?

 

Also I do not see API v22 setting anywhere. Where can I update that? Or it has to be configured by Salesforce support?

 

Thanks,

Kam

SuperfellSuperfell

You haven't been updated to summer '11 yet, so it won't work.

Kam.ax1015Kam.ax1015

So let's say it will be updated soon. The question is are there sample of creating complex rest based (or web service) APIs?

 

Thanks,

Kam

SuperfellSuperfell

The docs are the only current source of samples that I'm aware of. but the parameter types support sobjects not just primitives.

SuperfellSuperfell

Here's an example that allows you to send a POST to create an account and a list of contacts in a single transaction.

 

    @HttpPost
    global static string create(Account account, List<Contact> contacts) {
        insert account;
        for (Contact c : contacts)
            c.accountId = account.Id;
        insert contacts;
        return account.id;
    }

 

 

and a sample payload

 

 

{
    "account" : {
        "attributes": { "type": "account" },
	    "name" : "test account",
		"website" :"http://www.superfell.com"
    },
	"contacts" : [
		{ "attributes" : { "type" :"account"},
		   "firstName" :"Simon",
		    "lastName" :"Fell" },
		{ "attributes" : { "type" :"account"},
		   "firstName" :"Dan",
		    "lastName" :"Kador" }
	]
}

 

and i tested this with curl, like this. 

curl -v -H "Authorization:OAuth $sid" https://prerelna1.pre.salesforce.com/services/apexrest/sfell/foo --data-binary @acc.json -H "Content-Type:application/json"

> POST /services/apexrest/sfell/foo HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: prerelna1.pre.salesforce.com
> Accept: */*
> Authorization:OAuth <someSidWasHere>
> Content-Type:application/json
> Content-Length: 352
< HTTP/1.1 200 OK
< Server: 
< Content-Type: application/json; charset=UTF-8
< Content-Length: 20
< Date: Thu, 02 Jun 2011 23:04:27 GMT
"001x0000002JcMqAAK"

 

 

This was selected as the best answer
Kam.ax1015Kam.ax1015

Simon,

 

That does answer my question. Once my instance is upgraded to v22 I will try it out.

 

Thank you so much for your prompt help.

 

Kam

 

 

Kam.ax1015Kam.ax1015

Hello,

 

Thanks for your help. I have created a controller

 

@RestResource(urlMapping='/DealController/*')
global class DealRestResource {



When I try to invoke it using, 

curl -k -H "Authorization: OAuth 00D50000000Isbl!ARYAQPcQ3MM9F1VigkIS6HJ_6MMP2lboHo_SCNrY8NQJIyHWn2n31IIVY5ljtuj.Eo2WlDELUzjIZwMySlcQK7Lsk0d_" -d @acc.json "https://na3.salesforce.com/services/apexrest/DealController/"

 

I am geeting error

[{"message":"Could not find a match for URL /DealController/","errorCode":"NOT_F
OUND"}]

 

Also if I want to call in inside APEX page, what would be the URL?

 

Please help.

 

Thanks,

Kam

PrajnithPrajnith

how to write test method for this.???