4

I have a custom object Custom__c and I am accessing its fields by using

Public Custom__c custom1 {get;set;}

Map<String, Schema.SObjectField> map = Schema.SObjectType.Custom1__c.fields.getMap();
for(String fieldName : map.keySet()) {
    //using the fields for some processing using custom1
    this.custom1 = [select fieldName from Custom1 where.....];
}

Then I create another object for Custom__c

 Custom__c custom2 = new Custom__c();

Now what I want to do is assign the custom fields of custom2 the values of custom fields of custom1.

 for(String fieldName : map.keySet()) {
     value of fieldName of custom2 = value of fieldName of custom1
 }

I am not sure how to achieve this. All I know is it can be (?) done using DescribeResult, or is there any other way?

1 Answer 1

4

You can get the field name tokens using describe and then iterate over the keySet to set the values on Custom2 from Custom 1.

Map<String, Schema.SObjectField> M = Schema.SObjectType.Custom__c.fields.getMap();

For(string fName : M.keySet())
Custom2.put(fName, Custom1.get(fName));
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.