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
Sravan R PinnintiSravan R Pinninti 

Copy Field values from One Record to another using FieldSets!

Hi,

I have a cutom object named Custom__c. THis object consists almost 400 fields. This application is fully built on VF pages. During a record conversion process, based on certain conditions I want to create a Custom__c object record and this record should have field values copied from the existing Custom__c object record and this record should have a different record type values. 
Our Administrator should have the flexibility to be able to add and remove the fields the needs to be copied over. Hence, I am planning to use fieldsets instead of querying the existing fields. Can someone suggest a sample code that can fulfill this requirement.

Thank You In advance for the help!!
Best Answer chosen by Sravan R Pinninti
vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
You can the below code for reference:

public with sharing class ContactFieldSets {

public Contact cont;
public ContactFieldSets (Apexpages.Standardcontroller stdcont)
{
  string query = 'select ';
  string oldid =Apexpages.currentPage().getParameters().get('exitingid');
  cont = (Contact)stdcont.getRecord();
  for(Schema.FieldSetMember fld : this.getFields())
  {
   query += fld.getFieldPath()+', ';
  }
  query += 'Id from Contact where Id =:oldid';
 
  Contact oldcon = Database.query(query);
  cont.FirstName = oldcon.FirstName;
  System.debug(oldcon.FirstName);
}
public List<Schema.FieldSetMember> getFields()
{
   return Schema.Sobjecttype.Contact.FieldSets.Contact_Demo.getFields();
}
}