How to get all the required fields of sobject dynamically?



How to get all the required fields of sobject dynamically?..

Answer / Sandeep Shandilya

To get all the required fields of a sObject dynamically in Salesforce, you can use Type and Schema described methods. Here's an example using Schema:n```nSchema.SObjectType sobjectType = Schema.getGlobalDescribe().get('ObjectApiName'); Set<String> fieldSet = new Set<String>(); for (FieldDescription fd : sobjectType.getDescribe().fields.getMap().values()) {n if (fd.isRequiresUpdate() && fd.isNillable() == false) {n fieldSet.add(fd.getName());n }n}nList<sObject> records = new List<sObject>(); for (Integer i = 0; i < numberOfRecords; i++) {n sObject record = sobjectType.newSObject();n for (String field : fieldSet) {n if (sobjectType.fields.getMap().get(field).getDescribe().getType() == Type.DateTime)n record.put(field, Date.today());n else if (sobjectType.fields.getMap().get(field).getDescribe().getType() == Type.Decimal)n record.put(field, 0);n }n records.add(record);n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Salesforce Interview Questions

How many lookup relationship fields can be created in an object?

1 Answers  


What is Access at the Role Level?

1 Answers  


What is the search layout?

1 Answers  


Explain with sharing & without sharing keywords?

1 Answers  


What is the relationship in salesforce? What are its types?

1 Answers  


Difference b/w External ID & Unique ID?

0 Answers   Deloitte,


State the various workflow tasks in salesforce.

1 Answers  


Explain Dynamic Dashboards?

1 Answers  


What is system.runas () is test class?

1 Answers  


How many controllers can be used in a visual force page?

1 Answers  


How to get the list of all available sobject in salesforce database using apex (dynamic apex) ?

1 Answers  


How can you create many to many relationship in salesforce ?

1 Answers  


Categories