Skip to content

Commit

Permalink
Fixees afawcett#18 deserialization error for blob content
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolgkv committed Mar 8, 2021
1 parent 2f5711e commit 66cc13a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
53 changes: 44 additions & 9 deletions apex-sobjectdataloader/src/classes/SObjectDataLoader.cls
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ public with sharing class SObjectDataLoader
// Prepare records for insert
for(SObject originalRecord : recordSetBundle.Records)
{
setBlob(recordsBundle, originalRecord, originalRecord.Id);

if(!recordsToInsert.containsKey(originalRecord.Id))
{
SObject newRecord = recordsByOriginalId.get(originalRecord.Id);
Expand All @@ -497,6 +499,8 @@ public with sharing class SObjectDataLoader
}
}

setBlob(recordsBundle, newRecord, originalRecord.Id);

if(!relationshipsFields.isEmpty())
{
Set<Schema.SObjectField> resolvableFieldReferences = new Set<Schema.SObjectField>();
Expand Down Expand Up @@ -554,7 +558,16 @@ public with sharing class SObjectDataLoader
return new Map<Id, SObject>(recordsBundle.recordSetBundles[0].Records).keySet();
}


private static void setBlob(RecordsBundle bundle, SObject record, Id recordId) {
if(bundle.blobsByUID != null && bundle.blobsByUID.containsKey(recordId)) {
Map<String, Blob> blobByField = bundle.blobsByUID.get(recordId);

for(String field : blobByField.keySet()) {
record.put(field, blobByField.get(field));
}
}
}

/*
* Method clones the deserialized Objects
*/
Expand Down Expand Up @@ -836,7 +849,8 @@ public with sharing class SObjectDataLoader

// Record type map by Ids
public Map<Id, RecordType> recordTypeMap;

public Map<Id, Map<String, Blob>> blobsByUID = new Map<Id, Map<String, Blob>>();

/**
* @description Create a map of the current record types for all of the included records
**/
Expand All @@ -847,11 +861,13 @@ public with sharing class SObjectDataLoader

// Build up a set of record type IDs
Set<Id> recordTypeIds = new Set<Id>();
Set<String> blobFields = new Set<String>();

for (RecordSetBundle bundle : RecordSetBundles) {
// Get a map of fields
SObjectType accountType = globalDesc.get(bundle.ObjectType);
Map<String,Schema.SObjectField> mfields = accountType.getDescribe().fields.getMap();
// Get a map of fields
SObjectType objectType = globalDesc.get(bundle.ObjectType);
Map<String,Schema.SObjectField> mfields = objectType.getDescribe().fields.getMap();
// If this object contains a record type then step through and get the IDs
if (mfields.containsKey('recordtypeid')) {
for (SObject obj : bundle.Records) {
Expand All @@ -860,15 +876,34 @@ public with sharing class SObjectDataLoader
}
}
}


for(String mfield : mfields.keySet()) {
if(mfields.get(mfield).getDescribe().getType() == Schema.DisplayType.base64) {
blobFields.add(mfield);
}
}
for(SObject record : bundle.Records) {
addToBlobsMap(record, blobFields);
}
}

// Get all of the record types that are included
recordTypeMap = new Map<Id, RecordType>([SELECT Id, Description, DeveloperName, Name, SobjectType, NamespacePrefix FROM RecordType WHERE Id IN :recordTypeIds]);

}
}

private void addToBlobsMap(SObject record, Set<String> blobFields) {
blobsByUID.put(record.Id, new Map<String, Blob>());

for(String field : blobFields) {
Blob content = (Blob) record.get(field);
blobsByUID.get(record.Id).put(field, content);

record.put(field, null);
}
}
}
/**
* Internal Apex represnetation of the serialized output for a given recordset
**/
Expand Down
34 changes: 34 additions & 0 deletions apex-sobjectdataloader/src/classes/SObjectDataLoaderTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,40 @@ private class SObjectDataLoaderTest {
}



/*
* Test Serializing and deserializing records with blob content
*/
@IsTest(seeAllData=False)
private static void testDeserializeBlobContent()
{
// Setup
Exception expectedException = null;
Account account = new Account(Name = 'Grand Parent');
insert account;

Attachment file = new Attachment();
file.Name = 'Attachment';
file.Body = Blob.valueOf('content');
file.ParentId = account.Id;
insert file;


// Execute
try {
String json = SObjectDataLoader.serialize(new Set<Id>{ account.Id }, new SObjectDataLoader.SerializeConfig()
.followChild(Attachment.ParentId));
SObjectDataLoader.deserialize(json);
}
catch (JSONException ex) {
expectedException = ex;
}

//Verify
System.assertEquals(null, expectedException);
}


// INNER CLASSES

public class TestCallback implements SObjectDataLoader.IDeserializeCallback {
Expand Down

0 comments on commit 66cc13a

Please sign in to comment.