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
ShamilShamil 

Detect a required field in Apex

Is there any way to determine if a custom field is required in Apex (not through Force.com API)?

 

 

I couldn't find any way - dynamic Apex is of no value/help.

 

 

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
FaridKognozFaridKognoz

Shamil;

I was looking for the same answer as you.

I did it using describe Information for the  object and asking for each field

 

IF(field -->  [is Creatable AND is NOT Nillable AND is NOT Defaulted On Create]

THEN Field is Required.

 

If you want to do this for all fields in an object here is a sample code:

 

Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.THEOBJECT.fields.getMap();

//CREATE A MAP WITH FIELD NAME AS KEY AND A BOOLEAN (Required) AS VALUE

Map<String, Boolean> fieldIsRequired = new Map<String, Boolean>();

Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(f).getDescribe();

//IF FIELD IS CREATEABLE AND IS NOT NILLABLE AND IS NOT DEFAULTED ON CREATE THEN ITS REQUIRED
fieldIsRequired.put(field,desribeResult .isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate());
}

 

Finally if you want to check if there is any required field with null value, you could do something like this:

 

 


 

for(String field : fieldIsRequired.keySet()){

if(fieldsTypes.get(field) == Schema.DisplayType.STRING){
String value = (String) YourObject.get(field); //THIS WORKS ONLY FOR STRING FIELDS, IN ANY OTHER CASE YOU SHOULD CAST IT TO THE CORRECT TYPE
if((value == null || value == '') && fieldIsRequired.get(field)){
System.debug('Value can not be null for field "'+ field+'".\n');
}
}
}

}

 

 

 

 Hope this helps.

 

Farid

 

Message Edited by FaridKognoz on 10-22-2009 09:36 AM
Message Edited by FaridKognoz on 10-22-2009 09:40 AM

All Answers

FaridKognozFaridKognoz

Shamil;

I was looking for the same answer as you.

I did it using describe Information for the  object and asking for each field

 

IF(field -->  [is Creatable AND is NOT Nillable AND is NOT Defaulted On Create]

THEN Field is Required.

 

If you want to do this for all fields in an object here is a sample code:

 

Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.THEOBJECT.fields.getMap();

//CREATE A MAP WITH FIELD NAME AS KEY AND A BOOLEAN (Required) AS VALUE

Map<String, Boolean> fieldIsRequired = new Map<String, Boolean>();

Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(f).getDescribe();

//IF FIELD IS CREATEABLE AND IS NOT NILLABLE AND IS NOT DEFAULTED ON CREATE THEN ITS REQUIRED
fieldIsRequired.put(field,desribeResult .isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate());
}

 

Finally if you want to check if there is any required field with null value, you could do something like this:

 

 


 

for(String field : fieldIsRequired.keySet()){

if(fieldsTypes.get(field) == Schema.DisplayType.STRING){
String value = (String) YourObject.get(field); //THIS WORKS ONLY FOR STRING FIELDS, IN ANY OTHER CASE YOU SHOULD CAST IT TO THE CORRECT TYPE
if((value == null || value == '') && fieldIsRequired.get(field)){
System.debug('Value can not be null for field "'+ field+'".\n');
}
}
}

}

 

 

 

 Hope this helps.

 

Farid

 

Message Edited by FaridKognoz on 10-22-2009 09:36 AM
Message Edited by FaridKognoz on 10-22-2009 09:40 AM
This was selected as the best answer
ShamilShamil

Farid,

 

That definitely works - thanks.

I figured out the same thing after I started using the Salesforce Web Services API - it has exactly the same information returned by the Describe calls.

jkucerajkucera

My laziness kicking in - either of you taken it one step further to do figure out the DisplayType and enter a dummy value relevant for that type?  If so, can you post your syntax?  

 

My AppExchange app tests fail for customers w/ required fields that have no defaults so I need to modify my tests using your below method. 

 

I'll post mine in a couple of days once I've modified my tests.  

ShamilShamil

Hi John,

 

Here is what I use (haven't finished the whole thing yet though) to get an SObject record populated with required, unique fields. I tested the code with a couple of major objects, like Opportunity and Account, but didn't go very far with testing.

 

With this code all you have to do is call the createSObjectRecord method and pass SObject name as an argument. It will return a record that you can use to populate fields that are known at design time.

 

Code is:

 



/**

* Potential limitation: Total number of fields calls allowed is 10

*/



public with sharing class TestUtils {



public static String EMAIL_SUFFIX = '@company.com';

public static String URL_PREFIX = 'http://www.company.com/';





//method takes an object name as a parameter and returns a record of that object with

//all required fields populated, based on the data type



public SObject createSObjectRecord(String sObjectName){



//get a map of sobject tokens

Map gd = Schema.getGlobalDescribe();

//get a token to sObject

Schema.SObjectType sObjectToken = gd.get(sObjectName);

System.debug('sObjectToken= ' + sObjectToken);

//create a record

SObject sObjectRecord = sObjectToken.newSObject();

System.debug('sObjectRecord= ' + sObjectRecord);

//get fields

Map sObjectFieldsMap = sObjectToken.getDescribe().fields.getMap();





return createSObjectRecord(sObjectRecord,sObjectFieldsMap);



}





private SObject createSObjectRecord(SObject sObjectRecord, Map sObjectFieldsMap){

//iterate through fields to assign them proper values

for(String fieldName: sObjectFieldsMap.keySet()){

//get field token

SObjectField fieldToken = sObjectFieldsMap.get(fieldName);

//get field describe

DescribeFieldResult fieldDescribe = fieldToken.getDescribe();

assignFieldValue(sObjectRecord,fieldDescribe);

}



return sObjectRecord;



}



/**

* Fields that can be unique in SFDC:

* 1. email

* 2. number

* 3. text

*/

public void assignFieldValue(SObject sObjectRecord, DescribeFieldResult fieldDescribe){

/*

System.debug('FIELD NAME: ' + fieldDescribe.getName());

System.debug('FIELD TYPE: ' + fieldDescribe.getType());

System.debug('fieldDescribe.isCreateable(): ' + fieldDescribe.isCreateable());

System.debug('fieldDescribe.isNillable(): ' + fieldDescribe.isNillable());

System.debug('fieldDescribe.isDefaultedOnCreate(): ' + fieldDescribe.isDefaultedOnCreate());

System.debug('fieldDescribe.isAutoNumber(): ' + fieldDescribe.isAutoNumber());

System.debug('fieldDescribe.isCalculated(): ' + fieldDescribe.isCalculated());

*/

//if a field is required, specify value:

if(fieldDescribe.isCreateable() && !fieldDescribe.isNillable() && !fieldDescribe.isDefaultedOnCreate()

&& !fieldDescribe.isAutoNumber() && !fieldDescribe.isCalculated()){





//check if field is unique

if(fieldDescribe.isUnique()){

//text fields

if(fieldDescribe.getType() == Schema.DisplayType.STRING || fieldDescribe.getType() == Schema.DisplayType.TEXTAREA ||

fieldDescribe.getType() == Schema.DisplayType.ENCRYPTEDSTRING){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

} //number fields

else if(fieldDescribe.getType() == Schema.DisplayType.DOUBLE){

sObjectRecord.put(fieldDescribe.getName(),constructUniqueNumber(fieldDescribe.getPrecision() - fieldDescribe.getScale(),fieldDescribe.getScale()));



}//number fields

else if(fieldDescribe.getType() == Schema.DisplayType.INTEGER){

sObjectRecord.put(fieldDescribe.getName(),constructUniqueInteger(fieldDescribe.getDigits()));

}

else if(fieldDescribe.getType() == Schema.DisplayType.EMAIL){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()) + EMAIL_SUFFIX);

}

}else{

//text fields

if(fieldDescribe.getType() == Schema.DisplayType.STRING || fieldDescribe.getType() == Schema.DisplayType.TEXTAREA ||

fieldDescribe.getType() == Schema.DisplayType.ENCRYPTEDSTRING){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

} //number fields

else if(fieldDescribe.getType() == Schema.DisplayType.DOUBLE || fieldDescribe.getType() == Schema.DisplayType.CURRENCY

|| fieldDescribe.getType() == Schema.DisplayType.PERCENT){



System.debug('GENERATION OF A DOUBLE NUMBER START ...');

System.debug('fieldDescribe.getName() ' + fieldDescribe.getName());

System.debug('fieldDescribe.getPrecision() ' + fieldDescribe.getPrecision());

System.debug('fieldDescribe.getScale() ' + fieldDescribe.getScale());





sObjectRecord.put(fieldDescribe.getName(),constructUniqueNumber(fieldDescribe.getPrecision() - fieldDescribe.getScale(),fieldDescribe.getScale()));

System.debug('GENERATION OF A DOUBLE NUMBER END ...');

}//number fields

else if(fieldDescribe.getType() == Schema.DisplayType.INTEGER){

sObjectRecord.put(fieldDescribe.getName(),constructUniqueNumber(fieldDescribe.getDigits(),0));

}

else if(fieldDescribe.getType() == Schema.DisplayType.EMAIL){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()) + EMAIL_SUFFIX);

}

else if(fieldDescribe.getType() == Schema.DisplayType.URL){

sObjectRecord.put(fieldDescribe.getName(), URL_PREFIX + constructUniqueText(fieldDescribe.getLength()));

}

else if(fieldDescribe.getType() == Schema.DisplayType.DATE){

sObjectRecord.put(fieldDescribe.getName(), System.today());

}

else if(fieldDescribe.getType() == Schema.DisplayType.DATETIME){

sObjectRecord.put(fieldDescribe.getName(), System.now());

}

else if(fieldDescribe.getType() == Schema.DisplayType.PHONE){

System.debug('GENERATION OF A PHONE NUMBER START ...');

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

System.debug('GENERATION OF A PHONE NUMBER END ...');

}

else if(fieldDescribe.getType() == Schema.DisplayType.COMBOBOX){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

}

else if(fieldDescribe.getType() == Schema.DisplayType.MULTIPICKLIST){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

}

else if(fieldDescribe.getType() == Schema.DisplayType.PICKLIST){

sObjectRecord.put(fieldDescribe.getName(), constructUniqueText(fieldDescribe.getLength()));

}

else if(fieldDescribe.getType() == Schema.DisplayType.TIME){

sObjectRecord.put(fieldDescribe.getName(), System.now());

}

else if(fieldDescribe.getType() == Schema.DisplayType.REFERENCE){

List referenceToList = fieldDescribe.getReferenceTo();

String referenceSObjectName;



if(referenceToList != null && referenceToList.size()>0)

referenceSObjectName = referenceToList[0].getDescribe().getName();

String Id = findReference(referenceSObjectName);

if(Id != null)

sObjectRecord.put(fieldDescribe.getName(), Id);

}

}

}//some fields are nillable, but ARE required! for now only reference type of fields are handled below:



// else if(fieldDescribe.isCreateable() ){

// if(fieldDescribe.getType() == Schema.DisplayType.REFERENCE){

// List referenceToList = fieldDescribe.getReferenceTo();

// String referenceSObjectName;

// System.debug('referenceToList: ' + referenceToList);

//

// //skip polymorphic references

// if(referenceToList != null && referenceToList.size()>0 && referenceToList.size() < 2){

// referenceSObjectName = referenceToList[0].getDescribe().getName();

//

// System.debug('referenceSObjectName: ' + referenceSObjectName);

// String Id = findReference(referenceSObjectName);

// if(Id != null)

// sObjectRecord.put(fieldDescribe.getName(), Id);

// }



// }

// }

}









private String findReference(String referenceSObjectName){



//get any record of type 'referenceSObjectName'

SObject[] obj = Database.query('SELECT ID FROM ' + referenceSObjectName + ' LIMIT 1');

if(obj != null && obj.size()> 0)

return obj[0].Id;



return null;



}





private Integer constructUniqueInteger(Integer intSize){



String uniqueKey = String.valueOf(System.now().getTime());

System.debug('uniqueKey: ' + uniqueKey);



String intPart = '';



//if it is Integer

if(intSize != null && intSize != 0){

if(intSize < uniqueKey.length())

intPart = uniqueKey.substring(uniqueKey.length() - intSize,uniqueKey.length());

else

intPart = uniqueKey;

}



System.debug('uniqueKey: ' + uniqueKey);

System.debug('intPart: ' + intPart);



return Integer.valueOf(intPart);



}





private Double constructUniqueNumber(Integer intSize, Integer fractSize){



String uniqueKey = String.valueOf(System.now().getTime());

System.debug('uniqueKey: ' + uniqueKey);



String fractPart = '';

String intPart = '';



//if it is double

if(fractSize != null && fractSize != 0){

if(fractSize < uniqueKey.length())

fractPart = '.' + uniqueKey.substring(uniqueKey.length() - fractSize,uniqueKey.length());

else

fractPart = uniqueKey;

}

System.debug('fractPart: ' + fractPart);

if(intSize != null && intSize != 0){

if(intSize < uniqueKey.length())

intPart = uniqueKey.substring(uniqueKey.length() - intSize,uniqueKey.length());

else

intPart = uniqueKey;

}



System.debug('uniqueKey: ' + uniqueKey);

System.debug('intPart + fractPart: ' + (intPart + fractPart));

if(!fractPart.contains('.'))

return Double.valueOf(intPart + fractPart + '.0');

else

return Double.valueOf(intPart + fractPart);



}







private String constructUniqueText(Integer fieldSize){



String uniqueKey = String.valueOf(System.now().getTime());



if(fieldSize < uniqueKey.length()){

return uniqueKey.substring(uniqueKey.length() - fieldSize,uniqueKey.length());

}

else

return uniqueKey;



//return null;

}





}




 

 

 

 

 HTH,

Shamil  

 

Message Edited by Shamil on 02-04-2010 12:15 PM
jkucerajkucera

Awesome!  You're my hero!  This should save me several hours and I greatly appreciate the help! :)

 

I'll work on it either tonight or Monday as my tests are on Lead & Campaign Member.  I'll update this thread if I make meaningful changes that might be helpful.  

 

I've run into the same describe limit in a few tests so I'll see if there's a way to minimize them as well.

 

btw-good job translating my poorly written post.  I confused myself when I reread it!

jkucerajkucera

Finally got around to implementing it and worked well for leads & campaign members with 5 different required field types.  I made only minor tweaks such as defining methods as static.  For the limit of 10, since it's only custom fields & only required ones, if the customer has 10+ required custom fields with no defaults they have bigger issues than installing my app.  

 

Thanks again for the help!

 

 

// Potential limitation: Total number of fields calls allowed is 10 public with sharing class TestUtils { public static String EMAIL_SUFFIX = '@company.com'; public static String URL_PREFIX = 'http://www.google.com/'; //method takes an object name as a parameter and returns a record of that object with //all required fields populated, based on the data type public static SObject createSObjectRecord(String sObjectName){ Map<String, Schema.SObjectType> sObjMap= Schema.getGlobalDescribe(); Schema.SObjectType sObjectToken = sObjMap.get(sObjectName); System.debug('sObjectToken= ' + sObjectToken); SObject sObjectRecord = sObjectToken.newSObject(); System.debug('sObjectRecord= ' + sObjectRecord); Map<String,Schema.SObjectField> sObjectFieldsMap = sObjectToken.getDescribe().fields.getMap(); sObject sObjRec=createSObjectRecordPrivate(sObjectRecord,sObjectFieldsMap); return sObjRec; } private static SObject createSObjectRecordPrivate(SObject sObjectRecord, Map<String,Schema.SObjectField> sObjectFieldsMap){ //iterate through fields to assign them proper values Integer Count=0; for(String fieldName: sObjectFieldsMap.keySet()){ if (count>=10){ break; }else{ if (fieldName.contains('__c')){//note only custom fields can be set to required. However doesn't account for validation rules... SObjectField fieldToken = sObjectFieldsMap.get(fieldName); DescribeFieldResult fieldDescribe = fieldToken.getDescribe(); count+=assignFieldValue(sObjectRecord,fieldDescribe); }//if }//if }//for return sObjectRecord; } /** * Fields that can be unique in SFDC: * 1. email * 2. number * 3. text */ public static Integer assignFieldValue(SObject sObjectRecord, DescribeFieldResult fieldDescribe){ Schema.DisplayType fType=fieldDescribe.getType(); String fName=fieldDescribe.getName(); Integer count=0; /* System.debug('FIELD NAME: ' + fName); System.debug('FIELD TYPE: ' + fType); System.debug('fieldDescribe.isCreateable(): ' + fieldDescribe.isCreateable()); System.debug('fieldDescribe.isNillable(): ' + fieldDescribe.isNillable()); System.debug('fieldDescribe.isDefaultedOnCreate(): ' + fieldDescribe.isDefaultedOnCreate()); System.debug('fieldDescribe.isAutoNumber(): ' + fieldDescribe.isAutoNumber()); System.debug('fieldDescribe.isCalculated(): ' + fieldDescribe.isCalculated()); */ //if a field is required, specify value: //only add 1 to count if: creatable, not nillable, not defaulted, not an auto number, and not calculated if( fieldDescribe.isCreateable() && !fieldDescribe.isNillable() && !fieldDescribe.isDefaultedOnCreate()&& !fieldDescribe.isAutoNumber() && !fieldDescribe.isCalculated()){ count++;//increment as the # fieldDescribe results is limited //check if field is unique if(fieldDescribe.isUnique()){ //text fields: String, TextArea, EncryptedString if(fType == Schema.DisplayType.STRING || fType == Schema.DisplayType.TEXTAREA ||fType == Schema.DisplayType.ENCRYPTEDSTRING){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); } //number fields else if(fType == Schema.DisplayType.DOUBLE){ sObjectRecord.put(fName,constructUniqueNumber(fieldDescribe.getPrecision() - fieldDescribe.getScale(),fieldDescribe.getScale())); }//number fields else if(fType == Schema.DisplayType.INTEGER){ sObjectRecord.put(fName,constructUniqueInteger(fieldDescribe.getDigits())); } else if(fType == Schema.DisplayType.EMAIL){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength()) + EMAIL_SUFFIX); }//if 2 }else{ //just give it a value //text fields if(fType == Schema.DisplayType.STRING || fType == Schema.DisplayType.TEXTAREA ||fType == Schema.DisplayType.ENCRYPTEDSTRING){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); }else if(fType == Schema.DisplayType.DOUBLE || fType == Schema.DisplayType.CURRENCY|| fType == Schema.DisplayType.PERCENT){ System.debug('GENERATION OF A DOUBLE NUMBER START ...'); System.debug('fName ' + fName); System.debug('fieldDescribe.getPrecision() ' + fieldDescribe.getPrecision()); System.debug('fieldDescribe.getScale() ' + fieldDescribe.getScale()); sObjectRecord.put(fName,constructUniqueNumber(fieldDescribe.getPrecision() - fieldDescribe.getScale(),fieldDescribe.getScale())); System.debug('GENERATION OF A DOUBLE NUMBER END ...'); }else if(fType == Schema.DisplayType.INTEGER){ sObjectRecord.put(fName,constructUniqueNumber(fieldDescribe.getDigits(),0)); }else if(fType == Schema.DisplayType.EMAIL){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength()) + EMAIL_SUFFIX); }else if(fType == Schema.DisplayType.URL){ sObjectRecord.put(fName, URL_PREFIX + constructUniqueText(fieldDescribe.getLength())); }else if(fType == Schema.DisplayType.DATE){ sObjectRecord.put(fName, System.today()); }else if(fType == Schema.DisplayType.DATETIME){ sObjectRecord.put(fName, System.now()); }else if(fType == Schema.DisplayType.PHONE){ System.debug('GENERATION OF A PHONE NUMBER START ...'); sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); System.debug('GENERATION OF A PHONE NUMBER END ...'); }else if(fType == Schema.DisplayType.COMBOBOX){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); }else if(fType == Schema.DisplayType.MULTIPICKLIST){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); }else if(fType == Schema.DisplayType.PICKLIST){ sObjectRecord.put(fName, constructUniqueText(fieldDescribe.getLength())); }else if(fType == Schema.DisplayType.TIME){ sObjectRecord.put(fName, System.now()); }else if(fType == Schema.DisplayType.REFERENCE){ List<Schema.sObjectType> referenceToList = fieldDescribe.getReferenceTo(); String referenceSObjectName; if(referenceToList != null && referenceToList.size()>0){ referenceSObjectName = referenceToList[0].getDescribe().getName(); }//if 3 String Id = findReference(referenceSObjectName); if(Id != null) sObjectRecord.put(fName, Id); }//if 3 }//if 2 }//some fields are nillable, but ARE required! for now only reference type of fields are handled below: // else if(fieldDescribe.isCreateable() ){ // if(fType == Schema.DisplayType.REFERENCE){ // List referenceToList = fieldDescribe.getReferenceTo(); // String referenceSObjectName; // System.debug('referenceToList: ' + referenceToList); // // //skip polymorphic references // if(referenceToList != null && referenceToList.size()>0 && referenceToList.size() < 2){ // referenceSObjectName = referenceToList[0].getDescribe().getName(); // System.debug('referenceSObjectName: ' + referenceSObjectName); // String Id = findReference(referenceSObjectName); // if(Id != null) // sObjectRecord.put(fName, Id); // } // } // } return count; } private static String findReference(String referenceSObjectName){ //get any record of type 'referenceSObjectName' SObject[] obj = Database.query('SELECT ID FROM ' + referenceSObjectName + ' LIMIT 1'); if(obj != null && obj.size()> 0){ return obj[0].Id; }else{ return null; }//if }//findReference private static Integer constructUniqueInteger(Integer intSize){ String uniqueKey = String.valueOf(System.now().getTime()); System.debug('uniqueKey: ' + uniqueKey); String intPart = ''; //if it is Integer if(intSize != null && intSize != 0){ if(intSize < uniqueKey.length()){ intPart = uniqueKey.substring(uniqueKey.length() - intSize,uniqueKey.length()); }else{ intPart = uniqueKey; }//if 2 }//if 1 System.debug('uniqueKey: ' + uniqueKey); System.debug('intPart: ' + intPart); return Integer.valueOf(intPart); }//constructUniqueInteger private static Double constructUniqueNumber(Integer intSize, Integer fractSize){ String uniqueKey = String.valueOf(System.now().getTime()); System.debug('uniqueKey: ' + uniqueKey); String fractPart = ''; String intPart = ''; //if it is double if(fractSize != null && fractSize != 0){ if(fractSize < uniqueKey.length()){ fractPart = '.' + uniqueKey.substring(uniqueKey.length() - fractSize,uniqueKey.length()); }else{ fractPart = uniqueKey; }//if 2 }//if 1 System.debug('fractPart: ' + fractPart); if(intSize != null && intSize != 0){ if(intSize < uniqueKey.length()){ intPart = uniqueKey.substring(uniqueKey.length() - intSize,uniqueKey.length()); }else{ intPart = uniqueKey; }//if 2 }//if 1 System.debug('uniqueKey: ' + uniqueKey); System.debug('intPart + fractPart: ' + (intPart + fractPart)); if(!fractPart.contains('.')){ return Double.valueOf(intPart + fractPart + '.0'); }else{ return Double.valueOf(intPart + fractPart); }//if }//constructUniqueNumber private static String constructUniqueText(Integer fieldSize){ String uniqueKey = String.valueOf(System.now().getTime()); if(fieldSize < uniqueKey.length()){ return uniqueKey.substring(uniqueKey.length() - fieldSize,uniqueKey.length()); } else return uniqueKey; //return null; }//constructUniqueText }

 

 

 

ShamilShamil

You're welcome!

Note, that sometimes the Descirbe information is not consistent [or it is a lack of my knowledge]. For instance, some fields have 'isNillable' set to true, but are still required (I forgot which object it was, but it did happen).

Raghavendra ARaghavendra A

Hi All,

 

I am new to Apex Programming.

 

I am trying to fetch the required fields from Account Object. Person Acccount is enabled in our Client's Sandbox instance. I am trying to fetch the reuired fields using code in the discussion mentioned below (Is Nillable Code):

 

http://boards.developerforce.com/t5/Apex-Code-Development/Detect-a-required-field-in-Apex/m-p/152845#M21730

 

But the Account Name(Standard) field which is required at the field level is not listed at all for me.When I created a Custom Field which is marked Required at the field level, that corresponding required field is listed properly.

 

To fetch the Required fields for Person Account I called the Describe method from the Contact object. Last Name was listed by this method. 

 

But when I use the same code in my Developer instance where Person Account is not enabled then the Account Name is listed as Required field.

 

Any help is greatly appreciated!!!

 

Thanks,

Raghu

 

Is this happening becase Person Account is enabled in our Client's instance?