In order to check if a field is required or not, is the result of isNillable method sufficient?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Accordingly to the documentation of isNillable
method of Schema.DescribeFieldResult
class:
isNillable()
Returns true if the field is nillable, false otherwise. A
nillable field can have empty content. A non-nillable field must have
a value for the object to be created or saved.
Is it correct to state that a necessary and sufficient condition for a field to be required is that isNillable
method returns false?
I'm pretty sure that it is a necessary condition but not so sure that it is also a sufficient condition if we take into account also validation rules and triggers.
apex required-field describefieldresult
add a comment |
Accordingly to the documentation of isNillable
method of Schema.DescribeFieldResult
class:
isNillable()
Returns true if the field is nillable, false otherwise. A
nillable field can have empty content. A non-nillable field must have
a value for the object to be created or saved.
Is it correct to state that a necessary and sufficient condition for a field to be required is that isNillable
method returns false?
I'm pretty sure that it is a necessary condition but not so sure that it is also a sufficient condition if we take into account also validation rules and triggers.
apex required-field describefieldresult
add a comment |
Accordingly to the documentation of isNillable
method of Schema.DescribeFieldResult
class:
isNillable()
Returns true if the field is nillable, false otherwise. A
nillable field can have empty content. A non-nillable field must have
a value for the object to be created or saved.
Is it correct to state that a necessary and sufficient condition for a field to be required is that isNillable
method returns false?
I'm pretty sure that it is a necessary condition but not so sure that it is also a sufficient condition if we take into account also validation rules and triggers.
apex required-field describefieldresult
Accordingly to the documentation of isNillable
method of Schema.DescribeFieldResult
class:
isNillable()
Returns true if the field is nillable, false otherwise. A
nillable field can have empty content. A non-nillable field must have
a value for the object to be created or saved.
Is it correct to state that a necessary and sufficient condition for a field to be required is that isNillable
method returns false?
I'm pretty sure that it is a necessary condition but not so sure that it is also a sufficient condition if we take into account also validation rules and triggers.
apex required-field describefieldresult
apex required-field describefieldresult
edited 1 hour ago
gvgramazio
asked 1 hour ago
gvgramaziogvgramazio
688
688
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First Approach -
For the field to be required it has to meet 3 conditions:
- is Creatable
- is NOT Nillable
- is NOT Default on create
So your if condition should be as following:
if(field.isCreateable() && !field.isNillable() && !field.isDefaultedOnCreate()){
// your code
}
Once ’field’ is an element while of DescribeFieldResult.
Second Approach - to cover code in a try catch block. Into the ’try’ you can insert the Object without the field you want to check, if an exception is thrown, check which required fields are mentioned in the error message.
In this approach, you have to delete the record once it succeeds to be inserted.
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
|
show 1 more comment
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f260294%2fin-order-to-check-if-a-field-is-required-or-not-is-the-result-of-isnillable-met%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First Approach -
For the field to be required it has to meet 3 conditions:
- is Creatable
- is NOT Nillable
- is NOT Default on create
So your if condition should be as following:
if(field.isCreateable() && !field.isNillable() && !field.isDefaultedOnCreate()){
// your code
}
Once ’field’ is an element while of DescribeFieldResult.
Second Approach - to cover code in a try catch block. Into the ’try’ you can insert the Object without the field you want to check, if an exception is thrown, check which required fields are mentioned in the error message.
In this approach, you have to delete the record once it succeeds to be inserted.
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
|
show 1 more comment
First Approach -
For the field to be required it has to meet 3 conditions:
- is Creatable
- is NOT Nillable
- is NOT Default on create
So your if condition should be as following:
if(field.isCreateable() && !field.isNillable() && !field.isDefaultedOnCreate()){
// your code
}
Once ’field’ is an element while of DescribeFieldResult.
Second Approach - to cover code in a try catch block. Into the ’try’ you can insert the Object without the field you want to check, if an exception is thrown, check which required fields are mentioned in the error message.
In this approach, you have to delete the record once it succeeds to be inserted.
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
|
show 1 more comment
First Approach -
For the field to be required it has to meet 3 conditions:
- is Creatable
- is NOT Nillable
- is NOT Default on create
So your if condition should be as following:
if(field.isCreateable() && !field.isNillable() && !field.isDefaultedOnCreate()){
// your code
}
Once ’field’ is an element while of DescribeFieldResult.
Second Approach - to cover code in a try catch block. Into the ’try’ you can insert the Object without the field you want to check, if an exception is thrown, check which required fields are mentioned in the error message.
In this approach, you have to delete the record once it succeeds to be inserted.
First Approach -
For the field to be required it has to meet 3 conditions:
- is Creatable
- is NOT Nillable
- is NOT Default on create
So your if condition should be as following:
if(field.isCreateable() && !field.isNillable() && !field.isDefaultedOnCreate()){
// your code
}
Once ’field’ is an element while of DescribeFieldResult.
Second Approach - to cover code in a try catch block. Into the ’try’ you can insert the Object without the field you want to check, if an exception is thrown, check which required fields are mentioned in the error message.
In this approach, you have to delete the record once it succeeds to be inserted.
edited 1 hour ago
answered 1 hour ago
DerminalDerminal
1035
1035
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
|
show 1 more comment
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Could you please expand your answer with some explanations on why the other two conditions should be checked?
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
Also, could you please spend a word about validation rules? I think that it's not possible to check them from apex but I'm not sure.
– gvgramazio
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your first question. For example, CreatedDate is not a nillable field since it has value once record is created, but he is not a required field for the client. To avoid these fields, you should add the two conditions I’ve mentioned.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
@gvgramazio About your second question. Validation rule is a problematic issue. Your valudation rule includes formula which avoids saving or creating a record once it meets the conditions. You can retrieve the formula using apex, but you can't really know what the formula means in run time. For covering this scenario, the workaround I suggest is the second approach I've mentioned in my answer.
– Derminal
1 hour ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
Or, instead of a try-catch, you can use allOrNone=false to make sure you get all the errors.
– sfdcfox
40 mins ago
|
show 1 more comment
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f260294%2fin-order-to-check-if-a-field-is-required-or-not-is-the-result-of-isnillable-met%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown