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 |
How many lookup relationship fields can be created in an object?
What is Access at the Role Level?
What is the search layout?
Explain with sharing & without sharing keywords?
What is the relationship in salesforce? What are its types?
Difference b/w External ID & Unique ID?
State the various workflow tasks in salesforce.
Explain Dynamic Dashboards?
What is system.runas () is test class?
How many controllers can be used in a visual force page?
How to get the list of all available sobject in salesforce database using apex (dynamic apex) ?
How can you create many to many relationship in salesforce ?