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
Rhythm SharmaRhythm Sharma 

How to get the real time object level changes in apex ?

I have an object called myObject__c and when I create a new field, I want to get the field's name on runtime in apex. How can we do this?
SwethaSwetha (Salesforce Developers) 
HI Sharma,
The field name and other details about an object's fields can be retrieved at runtime using the Apex Describe API. 

Example:
// Replace MyObject__c with your custom object name
String objectName = 'MyObject__c';
// Replace Custom_Field__c with your dynamically created field API name
String fieldName = 'Custom_Field__c';

// Get the SObject describe result for the custom object
Schema.DescribeSObjectResult describeResult = Schema.getGlobalDescribe().get(objectName).getDescribe();

// Get the field describe result for the dynamically created field
Schema.DescribeFieldResult fieldDescribeResult = describeResult.fields.getMap().get(fieldName).getDescribe();

// Retrieve the field name
String fieldApiName = fieldDescribeResult.getName();

// Output the field name
System.debug('Field Name: ' + fieldApiName);


Related: https://salesforce.stackexchange.com/questions/71911/what-meant-by-describefieldresult-class
https://salesforce.stackexchange.com/questions/105495/dynamic-schema-getdescribe

If this information helps, please mark the answer as best. Thank you
 
Rhythm SharmaRhythm Sharma
Hello Swetha, But if we don't know about fields then what we will do, I just want to get the name of the fields which recently created. For example I created the field named demo in the object so I just want to right now to get the name of the field
Prateek Prasoon 25Prateek Prasoon 25
You can use the Schema.DescribeFieldResult class to get the details of the field, including its name. Here's an example of how you can get the name of a newly created field on the myObject__c object in Apex:
 
// Replace 'New_Field_Name__c' with the API name of your newly created field
String fieldName = 'myObject__c.New_Field_Name__c';
// Use Schema.SObjectType to get the describe result for your custom object
Schema.DescribeSObjectResult objectDescribe = Schema.SObjectType.myObject__c;
// Use Schema.DescribeFieldResult to get the describe result for the field
Schema.DescribeFieldResult fieldDescribe = objectDescribe.fields.getMap().get(fieldName).getDescribe();
// Get the name of the field
String name = fieldDescribe.getName();
// You can now use the 'name' variable to access the newly created field
System.debug('The name of the new field is: ' + name);

If you find my answer helpful, please mark it as the best answer. Thanks!
Frank ReebigFrank Reebig
The Schema is usable. To learn more (https://innowesy.de/)about the field, including its name, use the Describe Field Result class.