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
sahisahi 

Merging records

 

           How to get the fields from child record to master record.I have done

           merging records using merge statement.Like merge c1 c2;

 

           But I am unable to get the child record fields in to master record(c1),

           the fields that are not present in master record after merging.

 

 

        If any one know the solution,tell me.

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

you can describe the object and get all the fields at once and then loop through it.

you can use the following :

 

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

for(String fieldName : M.keySet())

{

if(masterRecord.get(fieldName) == null && childRecord.get(fieldName) != null)

{

masterRecord.put(fieldName, childRecord.get(fieldName));

}

}

 

this code will process all the field at once.

All Answers

Bhawani SharmaBhawani Sharma

you can use masterObject.get('fieldName') method to check that if parent record's field is having a null value and if it's true then you can get the child record value using the same method childRecord.get('fieldName') and put in the master one masterRecord.put('fieldName',fieldValue);

sahisahi

 

 I am very thankful for your reply,

 

         But is there any way to get them with a single step,because if i have 100 fields

         then it becomes difficult to get all those field and put. Is it possible with mergeresult[ ]?

Bhawani SharmaBhawani Sharma

you can describe the object and get all the fields at once and then loop through it.

you can use the following :

 

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

for(String fieldName : M.keySet())

{

if(masterRecord.get(fieldName) == null && childRecord.get(fieldName) != null)

{

masterRecord.put(fieldName, childRecord.get(fieldName));

}

}

 

this code will process all the field at once.

This was selected as the best answer
sahisahi

 

Thanku,

 

 I got the fields in master record.