You need to sign in to do that
Don't have an account?
Kauser 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
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
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
Account name is not mandatory while creating opportunity only opportunity name, StageName and CloseDate are mandatory. Refer below screenshot
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
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?
Is there a way we can make it behave the same as it works in UI?
Thanks
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.
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
If fields are custom then we can mark it required from field properties.
But for standrad fields, only way is validation rule.