Esempio n. 1
0
        /// <summary>
        /// Reprocesses lookup relationship fields that were missed during the initial import and
        /// populates them, once all the records are loaded into the sandbox. Similar to
        /// UpdateRecursiveField.
        /// </summary>
        private PopulateSandboxResult ReprocessObjects(List <ObjectTransformer> objectsToReprocess)
        {
            var results = new PopulateSandboxResult();

            foreach (var obj in objectsToReprocess)
            {
                foreach (var field in obj.LookupsToReprocess)
                {
                    ProgressUpdate(string.Format("Reprocessing referenced objects for {0} field {1}",
                                                 field.ObjectName, field.FieldName));

                    List <sObject> updateList = new List <sObject>();
                    foreach (var idPair in field.IdPairs)
                    {
                        var updateObj = CreateSobjectWithLookup(field.ObjectName,
                                                                field.RelatedObjectName, field.FieldName, idPair);

                        if (updateObj != null)
                        {
                            updateList.Add(updateObj);
                        }
                    }

                    // switch the IDs with the new ones in the sandbox
                    foreach (sObject rowLoop in updateList)
                    {
                        rowLoop.Id = _relationMapper.RecallNewId(field.ObjectName, rowLoop.Id);
                    }

                    ProgressUpdate(string.Format("Updating {0} {1} records",
                                                 updateList.Count, field.ObjectName));

                    var result = UpdateRecords(field.ObjectName, updateList);
                    results.ObjectResults.Add(result);
                }
            }

            return(results);
        }
Esempio n. 2
0
        private void UpdateRecursiveField(string apiName, List <ObjectTransformer.sObjectWrapper> workingList, string recursiveRelationshipField)
        {
            logger.DebugFormat("Object {0} has a recursive relation field {1}, now doing second pass to set it....",
                               apiName, recursiveRelationshipField);

            // make a new list of sObjects to do the update
            List <sObject> updateList = new List <sObject>();

            foreach (var wrapLoop in workingList)
            {
                if (!string.IsNullOrEmpty(wrapLoop.RecursiveRelationshipOriginalId))
                {
                    var upd = new sObject();

                    upd.type = wrapLoop.sObj.type;
                    upd.Id   = wrapLoop.NewId;
                    XmlDocument dummydoc    = new XmlDocument();
                    XmlElement  recursiveEl = dummydoc.CreateElement(recursiveRelationshipField);

                    string replaceValue = _relationMapper.RecallNewId(apiName, wrapLoop.RecursiveRelationshipOriginalId);

                    if (replaceValue == null)
                    {
                        logger.DebugFormat("Object {0} {1} recursive field {2} have value {3} could not translate - will ignore",
                                           apiName, wrapLoop.OriginalId, recursiveRelationshipField, wrapLoop.RecursiveRelationshipOriginalId);
                    }
                    else
                    {
                        recursiveEl.InnerText = replaceValue;

                        upd.Any = new XmlElement[] { recursiveEl };

                        updateList.Add(upd);
                    }
                }
            }

            logger.DebugFormat("{0} rows in Object {1} have recursive relation {2} to update ....",
                               updateList.Count(), apiName, recursiveRelationshipField);

            // update objects in batches
            int  successCount = 0;
            int  failCount    = 0;
            int  done         = 0;
            bool allDone      = false;

            if (updateList.Count == 0)
            {
                allDone = true;
            }
            while (!allDone)
            {
                var batch = updateList.Skip(done).Take(100).ToList();
                done += batch.Count;
                if (done >= updateList.Count)
                {
                    allDone = true;
                }

                var updateRes = _targetTasks.UpdateSObjects(apiName,
                                                            batch.ToArray());

                for (int i = 0; i < updateRes.Length; i++)
                {
                    if (updateRes[i].Success)
                    {
                        successCount += 1;
                    }
                    else
                    {
                        logger.WarnFormat("Error when updating {0} {1} in target: {2}",
                                          apiName, batch[i].Id, updateRes[i].ErrorMessage);
                        failCount += 1;
                    }
                }
            }
            logger.DebugFormat("Object {0} recursive relation {1} updated. Attempted {2} Success {3} Failed {4}",
                               apiName, recursiveRelationshipField, updateList.Count, successCount, failCount);
        }