i have one requirement on dynamic field mapping, i have 2 objects, Main_object and Staging_object, i will insert records in staging object by using integration, after record get insertd in staging object , i will replicate same record into Main_object , to active this i have to map 20 fields before compare records example: mainobj.field1__C= stageobj.feild1__c .. till 20 fields, can any one suggest me how to achive this finctionaly dynamically.



Try something like this:-

public static sobject cloneFields(sobject input, sobject output){
    map <String, Schema.SObjectField> fieldMap = schema.getGlobalDescribe().get(input.getsobjecttype().getDescribe().getname().tolowercase()).getDescribe().fields.getMap();
    map <String, Schema.SObjectField> outputfieldMap = schema.getGlobalDescribe().get(output.getsobjecttype().getDescribe().getname().tolowercase()).getDescribe().fields.getMap();
    for (string field : fieldMap.keySet()) {
        Schema.DescribeFieldResult fieldDescribe = fieldmap.get(field).getDescribe();
        if (fieldDescribe.isUpdateable() && fieldDescribe.isAutoNumber() == false && fieldDescribe.isCalculated() == false && fieldDescribe.isCustom()) {
            if (outputfieldmap.containsKey(field)) output.put(field,input.get(field));
    return output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.