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
@DEVS@DEVS 

Creating Opportunity using Rest API with a particular record type

How to create an Opportunity using Rest API with a particular Record type - when the user has access to multiple Record Types.

Below is the sample to create opportunity:
URI: services/data/v39.0/sobjects/Opportunity
Http Method: Post
Input: {"Name":"Opp1","CloseDate":"2018-08-01","StageName":"New","Probability":"10","Amount":"2000"}
Best Answer chosen by @DEVS
@DEVS@DEVS

Okey - It was not straight. Input should be as below:

{
  "Name": "Test1",
  "CloseDate": "2018-08-01",
  "StageName": "New",
  "Probability": "10",
  "Amount": "2000",
  "RecordType": {
    "Name": "TEST Record Type"
  }
}

All Answers

Sumeet_ForceSumeet_Force
Also pass the RecordTypeName or recordtypeid in the JSON just like you pass the data. So an additional column with the record type info shall suffice. If you dont pass, then record will get created with default record type for that profile of the user.
Sukanya BanekarSukanya Banekar
Hi 
Yes You have to pass recordType name, but you can not pass id as it would be different on another org. Also the recordType Name should be avalable in the another org.

Thanks,
Sukanya Banekar
@DEVS@DEVS

Okey - It was not straight. Input should be as below:

{
  "Name": "Test1",
  "CloseDate": "2018-08-01",
  "StageName": "New",
  "Probability": "10",
  "Amount": "2000",
  "RecordType": {
    "Name": "TEST Record Type"
  }
}
This was selected as the best answer
Sumeet_ForceSumeet_Force
Thanks for pointing that @DEVS. This works as well (e.g. for Contact):
{
  "LastName": "Test12",
  "RecordTypeId": "012280000019WDg"
}


 
@DEVS@DEVS
Yes - but it would change org wise... and external applications calling SF Rest API's to create records will have to configure it in different environments.
Nick EscobedoNick Escobedo
I created an account just to say @DEVS was correct. Adding the following code allowed me to create a record with the record type using a name via the REST API. 
"RecordType": {
    "Name": "Record Name Here"
}
ShotsterShotster
I think I'm overlooking something obvious, but how do you indicate the object to which you want to associate the opportunity? In my case, I want to create an opportunity on an account. I'm creating both a contact and account via the REST API without issue. I'm just unclear on how to "link" an opportunity to the account.
ShotsterShotster
Ok, so I just needed to add an AccountId property to the Opportunity JSON. That was the piece I was missing. So something like...
{
   AccountId : 'WXWXWXWXWXWXWXWX',
   Name : 'My Opportunity Test',
   CloseDate : '2017-09-04',
   StageName : 'Posted',
   Probability : 100,
   Amount : 1.00,
   RecordType : {
      Name: 'Donation'
   }
}
Of course, that's just JS - not proper JSON; you'd need to run it through JSON.stringify() or similar.
 
@DEVS@DEVS
{
  "Name": "Test1",
  "CloseDate": "2018-08-01",
  "StageName": "New",
  "Probability": "10",
  "Amount": "2000",
  "RecordType": {
    "Name": "TEST Record Type"
  },
  "Account": {
	"AccountUniqueID": "1234567890" // Please note that this field has to be marked as External ID & Unique
  }
}
@Shotster - Please try this
 
ShotsterShotster
Thanks, @DEVS, I appreciate the help! As it turns out, simply adding an AccountId field at the root of the opportunity JSON did the trick. Is there an advantage to the approach you listed?
@DEVS@DEVS
Yes Shotster, We use Rest API to call from external systems and its least expected that they would have Salesforce IDs while calling Rest APIs. So we can use External Unique IDs to smartly identify Salesforce IDs for us. 
Michalis MantouvalosMichalis Mantouvalos

Hello guys,

 

Is there any solution from when we have more than one Record Types (at different Sobjects) with the same Name?

In my org I have 2 Service types with the name  : "Service" in both Account and Case Object. 

The answer I get from the server is: 

 

[
    {
        "message": "Name: more than one record found for external id field: [012090000005uVyAAI, 01                           2090000005uWHAAY]",
        "errorCode": "DUPLICATE_EXTERNAL_ID",
        "fields": [
            "Name"
        ]
    }
]

 

 

So basically it doenst know what Record type to apply. 

 

II have tried populating the SobjectType but it doenst work. 

 

What i did :

  "RecordType": {
                    "Name": "Service",
                    "SobjectType" : "Account" 
   }


Answer I get from server: 

[
    {
        "message": "More than 1 field provided in an external foreign key reference in entity: RecordTyp                             e",
        "errorCode": "INVALID_FIELD",
        "fields": []
    }
]

Eric ToomEric Toom
@Michalis Mantouvalos did you ever figure out a way to do this?  We just ran into this today and we're having our admins change the second record type to something unique to get around this but you'd think that by creating an account it would restrict the record types to that SobjectType for you.
Sravani GaddeSravani Gadde
Hi Anyone, is there any resolution found iam also facing the same 'Michalis Mantouvalos' have mentioned.The recordType - names are not unique accross objects so , it is giving the exception DUPLICATE_EXTERNAL_ID . Can any one help
swapna bhaskaruniswapna bhaskaruni
Hey, where can i get the format of request body and all the possible fields that can be given in the request body, which are mandatory, which are optional.... for the URI: services/data/v39.0/sobjects/Opportunity. basically i need a documentation of this URI. can some help me. 
Stijn MangelschotsStijn Mangelschots

Hi Michalis & Eric & Sravani,

The solution I would propose is to differentiate between the Recordtypes with the same value for the 'Name' Field using the 'DeveloperName' field. These two fields can have a different value. Once you have set the 'DeveloperName' field to a unique value the json would need to be changed to (example given would then be for Account object):
  "RecordType": {
                    "DeveloperName": "ServiceAccount"
   }
Hope this helps you.

Ayushii AgrawalllAyushii Agrawalll

Hi Stijn, it doesn't work and throws an error -
"message: Field name provided, DeveloperName is not an External ID or indexed field for RecordType".

Is there any other possible solution for this?