private string BuildJobName(CustomJob customJob) { customJob.Require(nameof(customJob)); CustomJob preImage = null; if (Context.MessageName == "Update") { preImage = Context.PreEntityImages?.FirstOrDefault().Value?.ToEntity <CustomJob>(); if (preImage == null) { throw new InvalidPluginExecutionException("A full pre image must be registered on this plugin step."); } } if (Context.MessageName == "Create" && !string.IsNullOrEmpty(customJob.Name)) { Log.Log("'Create' message and name is filled; using the custom name."); return(customJob.Name); } var customJobTemp = new CustomJob { Id = customJob.Id, ActionName = customJob.ActionName, Workflow = customJob.Workflow }; Log.Log("Loading lookup labels ..."); customJobTemp.LoadLookupLabels(Service); Log.Log("Loaded lookup labels."); var label = customJobTemp.WorkflowLabels?.FirstOrDefault(p => p.Key == 1033).Value; var newName = $"{customJob.TargetLogicalName}" + " (" + (string.IsNullOrEmpty(customJob.TargetID) ? (string.IsNullOrEmpty(customJob.TargetXML) ? "no target" : "multi-target") : customJob.TargetID) + ")" + $"{(string.IsNullOrEmpty(customJobTemp.ActionName) ? "" : " : " + customJobTemp.ActionName)}" + $"{(string.IsNullOrEmpty(label) ? "" : " : " + label)}"; Log.Log($"Assumed new name: {newName}."); if (Context.MessageName == "Update" && preImage != null) { Log.Log($"Updating message; comparing updated name."); customJobTemp = new CustomJob { Id = customJob.Id, ActionName = preImage.ActionName, Workflow = preImage.Workflow }; Log.Log("Loading lookup labels of pre-image ..."); customJobTemp.LoadLookupLabels(Service); Log.Log("Loaded lookup labels of pre-image."); var preLabel = customJobTemp.WorkflowLabels?.FirstOrDefault(p => p.Key == 1033).Value; var preName = $"{preImage.TargetLogicalName}" + " (" + (string.IsNullOrEmpty(preImage.TargetID) ? (string.IsNullOrEmpty(preImage.TargetXML) ? "no target" : "multi-target") : preImage.TargetID) + ")" + $"{(string.IsNullOrEmpty(customJobTemp.ActionName) ? "" : " : " + customJobTemp.ActionName)}" + $"{(string.IsNullOrEmpty(preLabel) ? "" : " : " + preLabel)}"; Log.Log($"Pre-image name: {preName}."); var existingName = customJob.Name; newName = (string.IsNullOrEmpty(existingName) || preName == existingName) ? newName : existingName; Log.Log($"Final new name: {newName}."); } return(newName.Trim(' ').Trim(':').Trim(' ')); }