From 1c4e23e2167aeea0f58088255088bc98f947d12a Mon Sep 17 00:00:00 2001 From: Matthew Higel Date: Mon, 27 Aug 2018 23:10:58 -0500 Subject: [PATCH] ShovlrImportrQueueable First draft of importer class. Learned all about the voodoo involved in referencing external ids in relationship fields during creation DML (line 37-40). Haven't tested it yet, but wanted to get this committed. Also, this commit also includes ShovlrUtil class that I'm planning on using holding the functionality to create the XC_Id__c external id field on objects that do not yet have it. I figured this removes another friction point so that we don't have to point/click add that field on any/all objcets that we're exporting. --- src/classes/ShovlrImportrQueueable.cls | 65 +++++++++++++++++++ .../ShovlrImportrQueueable.cls-meta.xml | 5 ++ src/classes/ShovlrUtil.cls | 16 +++++ src/classes/ShovlrUtil.cls-meta.xml | 5 ++ 4 files changed, 91 insertions(+) create mode 100644 src/classes/ShovlrImportrQueueable.cls create mode 100644 src/classes/ShovlrImportrQueueable.cls-meta.xml create mode 100644 src/classes/ShovlrUtil.cls create mode 100644 src/classes/ShovlrUtil.cls-meta.xml diff --git a/src/classes/ShovlrImportrQueueable.cls b/src/classes/ShovlrImportrQueueable.cls new file mode 100644 index 0000000..19b1ff3 --- /dev/null +++ b/src/classes/ShovlrImportrQueueable.cls @@ -0,0 +1,65 @@ + +public class ShovlrImportrQueueable implements Queueable { + + Integer counter { get; set; } + Data_Shover_Setting__mdt settings { get; set; } + + public ShovlrImportrQueueable() { + counter = 0; + settings = [ + SELECT Object_Name__c, Limit__c, Sequence__c + FROM Data_Shover_Setting__mdt + ORDER BY Sequence__c + ][counter]; + } + + public ShovlrImportrQueueable(Integer c, String sId) { + counter = c; + settings = [ + SELECT Object_Name__c, Limit__c, Sequence__c + FROM Data_Shover_Setting__mdt + ORDER BY Sequence__c + ][counter]; + } + + public void execute(QueueableContext context) { + String srName = settings.Object_Name__c.replaceAll('_', ''); + StaticResource sr = [SELECT Body FROM StaticResource WHERE Name = :srName]; + + Map> dataMap = (Map>)JSON.deserializeUntyped(sr.Body.toString()); + List soList = dataMap.values()[0]; + + Map refFieldMap = defineRefFieldMap(); + + for(SObject so : soList) { + so.put('XC_Id__c', so.Id); + for(String refField : refFieldMap.keySet()) { + SObjectType refType = refFieldMap.get(refField); + SObject refObj = refType.newSObject(); + refObj.put('XC_Id__c', String.valueOf(so.get(refField))); + so.put(refField, refObj); + } + } + + Database.insert(soList); + } + + Map defineRefFieldMap() { + Map refFieldMap = new Map(); + + String objectName = settings.Object_Name__c; + Schema.DescribeSObjectResult dsr = Schema.getGlobalDescribe().get(objectName).getDescribe(); + + Map fieldMap = dsr.fields.getMap(); + + for(String field : fieldMap.keySet()) { + Schema.SObjectField sof = fieldMap.get(field); + if(sof.getDescribe().getType() == Schema.DisplayType.REFERENCE) { + Schema.SObjectType soType = sof.getDescribe().getReferenceTo()[0]; + refFieldMap.put(field, soType); + } + } + + return refFieldMap; + } +} \ No newline at end of file diff --git a/src/classes/ShovlrImportrQueueable.cls-meta.xml b/src/classes/ShovlrImportrQueueable.cls-meta.xml new file mode 100644 index 0000000..fec71a2 --- /dev/null +++ b/src/classes/ShovlrImportrQueueable.cls-meta.xml @@ -0,0 +1,5 @@ + + + 42.0 + Active + diff --git a/src/classes/ShovlrUtil.cls b/src/classes/ShovlrUtil.cls new file mode 100644 index 0000000..5710b53 --- /dev/null +++ b/src/classes/ShovlrUtil.cls @@ -0,0 +1,16 @@ + +public with sharing class ShovlrUtil { + + MetadataService.MetadataPort createService(String sessionId) { + System.debug('ShovlrUtil ShovlrUtil'); + MetadataService.MetadataPort service = new MetadataService.MetadataPort(); + service.SessionHeader = new MetadataService.SessionHeader_element(); + service.SessionHeader.sessionId = sessionId; + return service; + } + + public static void createXCIdField(String objectName, String sessionId) { + MetadataService.MetadataPort svc = createService(sessionId); + } + +} \ No newline at end of file diff --git a/src/classes/ShovlrUtil.cls-meta.xml b/src/classes/ShovlrUtil.cls-meta.xml new file mode 100644 index 0000000..fec71a2 --- /dev/null +++ b/src/classes/ShovlrUtil.cls-meta.xml @@ -0,0 +1,5 @@ + + + 42.0 + Active +