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
Kauser FatimaKauser Fatima 

Mandatory field validation not done while creating an Opportunity record via REST API

While creating an Opportunity record in Salesforce via REST API, even though the values for mandatory fields are not given, Salesforce accepts it and does not throw a validation error. For Ex: Account Name is mandatory in Salesforce UI while creating an Opportunity and it doesn't allow to create until we give a value for Account Name.

However if the same request is passed via REST API it accepts it.

Here is the sample request:
POST request 
/services/data/v40.0/sobjects/Opportunity
 { "Name": "test opportunity", "StageName": "Closed Lost","CloseDate": "2018-04-02"}

This request succeeds in creating an opportunity where in actual it should have given a validation error for Account Name field.

Is this behavior expected? Any help would be appreciated. 

Thanks
Best Answer chosen by Kauser Fatima
Om PrakashOm Prakash
Hi Kauser,
You need to create a validation rule if you want the same field validated from API as well.

In your org Account Name is made mandatory from record page layout thats why in Salesforce UI while creating an Opportunity it doesn't allow to create until we give a value for Account Name.
Internally this field is not mandatory thats why you are able to create record without validation error from API.

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Account name is not mandatory while creating opportunity only opportunity name, StageName and CloseDate are mandatory. Refer below screenshot

User-added imagePlease mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
Kauser FatimaKauser Fatima
Hi Sandhya,

Thanks for the quick response. In my account, Account Name is marked as Mandatory when I see in the Page Layout. Does this mean that the fields that user has made mandatory will not behave as such when we make requests via REST API?
User-added image

Is there a way we can make it behave the same as it works in UI?

Thanks
Om PrakashOm Prakash
Hi Kauser,
You need to create a validation rule if you want the same field validated from API as well.

In your org Account Name is made mandatory from record page layout thats why in Salesforce UI while creating an Opportunity it doesn't allow to create until we give a value for Account Name.
Internally this field is not mandatory thats why you are able to create record without validation error from API.
This was selected as the best answer
Kauser FatimaKauser Fatima
Hi Om,

I created a validation rule ISBLANK(AccountId) and it worked. Thanks for the suggestion. Was just wondering if this could be a viable solution if there are many such fields across many objects in customer's Salesforce account. For it to reflect via API they would have to create validation rules for all of them manually. 

Is this the only way for such fields to be considered mandatory via API?

Thanks
Waqar Hussain SFWaqar Hussain SF
Making a field required on page layout, make the field require only on front end. You will have to create a validation rule to make account field require on Opportunity. 
Om PrakashOm Prakash
Hi Kauser,
If fields are custom then we can mark it required from field properties.
But for standrad fields, only way is validation rule.
 
Kauser FatimaKauser Fatima
Noted. Thanks.