+ Start a Discussion

How can we get the names of fields when any record in inserted in any object using apex

How can we get the names of fields when any record in inserted in any object using apex. I need field API name not value.
Suppose I had created a record in Account and inserted name. Then I need to know from apex that what is the field API name for the record I inserted.
Prangya JenaPrangya Jena

Your requirement is little unclear . If you are inserting Account and inserting name then name field API is the API name . API name exists for Objects and Fields  which are in record .
Archana BattaArchana Batta

I think, everytime you insert a record, you want to see which fields (API Name of the field) have got values in it.

If so, please try below. As i created an Account with only Name. Below code will print API name of the field "Name" and ofcourse "ID" as it will have value populated due to our INSERT statement.

Account acc = new Account(Name='Test Account');
insert acc;
Map <String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();
for(Schema.SObjectField sfield:fieldMap.values()){
    if(acc.get(sfield.getDescribe().getlocalname()) != null){

Adventures DreamAdventures Dream
In my opinion, Archana batta is perfectly right! 
How to find cheap flights to anywhere? This article will show you use of google flights, low cost airlines, secret flights and best airlines to fly for you! Secret Flying
Adventures Dream

naked ab workout
abs workout