
I have a post-operation plugin that runs on each Check Import Record within the Check Import Entity when any Check Import Record from the Check Import Entity is updated.
The plugin collects all the relevant data from the current Check Import Record in the Check Import Entity the user is updating, and then it goes out and creates a new Check Record in the Check Register Entity and it adds a bunch of data to the new Check record in Check Register Entity.
Once its done creating the new Check record, I need to somehow make the plugin grab the GUID from the Check record it just finished creating in the Check Register Entity, and then put that GUID into the Check Lookup Field on the original Check Import Record, so that Users can see which check record was derived from the currently open Check Import Record.
Once that lookup field is populated with the child check that was created from the import, the plugin will never run again to create a new check using this particular Check Import record.
Below is the plugin code that runs when a Check Import Record is updated from within the Check Import Entity, and collects all the relevant fields from the Check Import Record that is being updated.
namespace EsCheatAddImportChkToChkMgmt.Plugins
{
using System;
using System.Data;
using System.ServiceModel;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.XmlNamespaces;
public class AddImportCheckToCheckRegister: Plugin
{
public AddImportCheckToCheckRegister()
: base(typeof(AddImportCheckToCheckRegister))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_s_checkstaging", new Action<LocalPluginContext>(ExecuteAddImportCheckToCheckRegister)));
}
// Define the IDs needed for this sample.
public Guid _new_checkinfoid;
protected void ExecuteAddImportCheckToCheckRegister(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
ITracingService TracingSvc = localContext.TracingService;
TracingSvc.Trace("Plugin has started.");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
//Set Up Process
Entity MyEntity = (Entity)context.InputParameters["Target"];
var myCheckID = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_checkmanagementid");
if (myCheckID != null)
{
Right here – we execute thousands of lines of code that collects all the data, and manipulates it, to ultimately get all of the values that must be inserted into the record we will soon be creating in the Check Register Entity below.
Below is the section of the plugin code that takes that information generated above, and passes it to THE FUNCTION THAT WILL create the new Check record in the Check Register Entity using that information.
CreateNewCheckInRegister(service, localContext, "new_s_checkinfo", "new_checkinfoid", --- a bunch of fields with data values here --- );
}
else { }
}
catch (Exception ex) { throw new Exception("Error in top level at TRY housing: " + ex.Message); }
}
else
{
throw new InvalidPluginExecutionException("ELSE Error - code went to ELSE inside the top level.");
}
}
The function below is what receives the passed data and creates the new Check record, once created, it then retrieves it and updates all the fields accordingly using the passed data.
protected void CreateNewCheckInRegister(IOrganizationService CreateService, LocalPluginContext CreateLocalContext, string CreateEntity, string PassedField,
string RecordOwner, string RecordOwnerVal, --- a bunch of fields with data values here ---)
{
IPluginExecutionContext context = CreateLocalContext.PluginExecutionContext;
CreateService = CreateLocalContext.OrganizationService;
ITracingService TracingSvc = CreateLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity newCheckManagementCheck = new Entity(CreateEntity);
newCheckManagementCheck[PassedField] = "<AutoNumber>";
_new_checkinfoid = CreateService.Create(newCheckManagementCheck);
Console.Write("{0} {1} created, ", newCheckManagementCheck.LogicalName, newCheckManagementCheck.Attributes[PassedField]);
// Create a column set to define which attributes should be retrieved.
ColumnSet attributes = new ColumnSet(new string[] { PassedField });
// Retrieve the account and its name and ownerid attributes.
newCheckManagementCheck = CreateService.Retrieve(newCheckManagementCheck.LogicalName, _new_checkinfoid, attributes);
Console.Write("retrieved, ");
There are thousands of lines of code here to handle all of the data and related processes. I removed the code since it is not relevant to my problem.
CreateService.Update(newCheckManagementCheck);
Console.WriteLine("and updated.");
THIS IS WHERE I AM ENCOUNTERING THE PROBLEM – AFTER THIS –
//GET GUID OF NEWLY CREATED CHECK RECORD
var CheckGUIDAsString = _new_checkinfoid.ToString();
Entity MyEntity = (Entity)context.InputParameters["Target"];
THE FUNCTION THAT WILL UPDATE THE LOOKUP VALUE ON MY ORIGINAL CHECK IMPORT RECORD IN THE CHECK IMPORT ENTITY IS CALLED BELOW, AND THE REVENT DATA IS PASSED
PerformLookupGUIDUpdate(CreateService, CreateLocalContext, MyEntity, CheckGUIDAsString, "new_checkmanagementid", "new_s_checkinfo");
}
catch (Exception ex) { throw new Exception("Error in IF/TRY Failed to Update Entities: " + ex.Message); }
}
else { }
}
BELOW is the actual function that will update the lookup value in my original Check Import Record from the Check Import Entity.
protected void PerformLookupGUIDUpdate(IOrganizationService UpdateService,
LocalPluginContext UpdateLocalContext,
Entity PassedEntity,
string PassedCheckInfoID, string PassedUpdateField, string PassedRelatedEntityName)
{
IPluginExecutionContext context = UpdateLocalContext.PluginExecutionContext;
UpdateService = UpdateLocalContext.OrganizationService;
ITracingService TracingSvc = UpdateLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
//EVERYTHING UP TO THIS POINT WORKS
PassedEntity[PassedUpdateField] = new EntityReference(PassedRelatedEntityName, new Guid(PassedCheckInfoID));
UpdateService.Update(PassedEntity);
//EVERYTHING BELOW THIS POINT WORKS
}
catch (Exception ex) { throw new Exception("Error in IF/TRY Failed to Update Entities: " + ex.Message); }
}
else
{
try
{
TracingSvc.Trace("Error! Code went to ELSE/TRY in PerformUpdate method and should have never went here.");
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY in PerformUpdate method and should have never went here. Code went to Catch but should never have entered this entire section." + ex.Message); }
}
}
//END - DONT PUT CODE BELOW HERE
}
}
THIS ENDS WHERE I AM ENCOUNTERING THE PROBLEM – END IS BEFORE THIS –
The exact error I am receiving is shown below
Unexpected exception from plug-in (Execute): EsCheatAddImportChkToChkMgmt.Plugins.AddImportCheckToCheckRegister: System.Exception: Error in top level at TRY housing: Error in IF/TRY Failed to Update Entities: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
The problem is that the string that holds the GUID value CheckGUIDAsString does contain a valid Guid.
It contains the GUID of the newly created record which will be the Lookup ID to that record when it gets inserted into the associated Check Import record so I don’t exactly understand the problem.
I have updated similar things in the past, but they were always GUID’s of other records (not the currently open one)
This is the first time I have a plugin collect data from an open/updated record, and create a new record in a different entity, then jump back to the original open/updated record to further update that. And I am certain this jumping forward and back between records is the root of the problem. I know I am not “jumping between them” properly.
*This post is locked for comments
I have the same question (0)I was able to solve this. Here is the solution.
namespace EsCheatAddImportChkToChkMgmt.Plugins
{
using System;
using System.Data;
using System.ServiceModel;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.XmlNamespaces;
/// <summary>
/// AddImportCheckToCheckRegister Plugin.
/// Fires when the following attributes are updated:
/// All Attributes
/// </summary>
public class AddImportCheckToCheckRegister: Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="AddImportCheckToCheckRegister"/> class.
/// </summary>
public AddImportCheckToCheckRegister()
: base(typeof(AddImportCheckToCheckRegister))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_s_checkstaging", new Action<LocalPluginContext>(ExecuteAddImportCheckToCheckRegister)));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
// Define the IDs needed for this sample.
public Guid _new_checkinfoid;
/// <summary>
/// Executes the plug-in.
/// </summary>
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
protected void ExecuteAddImportCheckToCheckRegister(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
ITracingService TracingSvc = localContext.TracingService;
TracingSvc.Trace("Plugin has started.");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
//Set Up Process
Entity MyEntity = (Entity)context.InputParameters["Target"];
var myCheckID = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_checkmanagementid");
if (myCheckID == "NULL")
{
//Get Field Data - TEXT
var CheckStagingID = GetFieldStringData(service, localContext, MyEntity, "new_chkstagenum");
var AddressLine1Pri = GetFieldStringData(service, localContext, MyEntity, "new_addressline1pri");
var AddressLine1Sec = GetFieldStringData(service, localContext, MyEntity, "new_addressline1sec");
var AddressLine2Pri = GetFieldStringData(service, localContext, MyEntity, "new_addressline2pri");
var AddressLine2Sec = GetFieldStringData(service, localContext, MyEntity, "new_addressline2sec");
var BankCode = GetFieldStringData(service, localContext, MyEntity, "new_bankcode");
var BusinessNamePri = GetFieldStringData(service, localContext, MyEntity, "new_businessname");
var BusinessNameSec = GetFieldStringData(service, localContext, MyEntity, "new_businessnamesec");
var CheckCatalogNum = GetFieldStringData(service, localContext, MyEntity, "new_checkcatalognumber");
var CheckNum = GetFieldStringData(service, localContext, MyEntity, "new_checknum");
var CheckYear = GetFieldStringData(service, localContext, MyEntity, "new_checkyear");
var CheckStagingNumber = GetFieldStringData(service, localContext, MyEntity, "new_chkstagenum");
var CityPri = GetFieldStringData(service, localContext, MyEntity, "new_citypri");
var CitySec = GetFieldStringData(service, localContext, MyEntity, "new_citysec");
var FirstNamePri = GetFieldStringData(service, localContext, MyEntity, "new_firstnamepri");
var FirstNameSec = GetFieldStringData(service, localContext, MyEntity, "new_firstnamesec");
var IssueReason = GetFieldStringData(service, localContext, MyEntity, "new_issuereasonlong");
var IssuingDepartment = GetFieldStringData(service, localContext, MyEntity, "new_issuingdepartmentlong");
var LastNamePri = GetFieldStringData(service, localContext, MyEntity, "new_lastnamepri");
var LastNameSec = GetFieldStringData(service, localContext, MyEntity, "new_lastnamesec");
var MiddleNamePri = GetFieldStringData(service, localContext, MyEntity, "new_middlenamepri");
var MiddleNameSec = GetFieldStringData(service, localContext, MyEntity, "new_middlenamesec");
var OwnerTypeDescriptionPri = GetFieldStringData(service, localContext, MyEntity, "new_otypelongpri");
var OwnerTypeDescriptionSec = GetFieldStringData(service, localContext, MyEntity, "new_otypelongsec");
var OwnerRelationshipPri = GetFieldStringData(service, localContext, MyEntity, "new_ownerrelationship");
var OwnerRelationshipSec = GetFieldStringData(service, localContext, MyEntity, "new_ownerrelationshipsec");
var OwnerStatusPri = GetFieldStringData(service, localContext, MyEntity, "new_ownerstatuslong");
var OwnerStatusSec = GetFieldStringData(service, localContext, MyEntity, "new_ownerstatuslongsec");
var CorporateStatusPri = GetFieldStringData(service, localContext, MyEntity, "new_ownertypelong");
var CorporateStatusSec = GetFieldStringData(service, localContext, MyEntity, "new_ownertypelongsec");
var PropertyOwnerType = GetFieldStringData(service, localContext, MyEntity, "new_potype");
var RecordOwner = GetFieldStringData(service, localContext, MyEntity, "new_recowner");
var StatusCode = GetFieldStringData(service, localContext, MyEntity, "new_scode");
var SSNTAXIDNum = GetFieldStringData(service, localContext, MyEntity, "new_ssntaxid");
var SSNTAXIDNumSec = GetFieldStringData(service, localContext, MyEntity, "new_ssntaxidsec");
var ZipCodePri = GetFieldStringData(service, localContext, MyEntity, "new_zipcodepri");
var ZipCodeSec = GetFieldStringData(service, localContext, MyEntity, "new_zipcodesec");
//Get Field Data - LOOKUP
var Bank = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_bank");
var IssueReasonCode = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_issuereason");
var CheckID = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_checkmanagementid");
var IssuingDepartmentCode = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_issuingdepartment");
var PrefixPri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_nameprefixpri");
var PrefixSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_nameprefixsec");
var SuffixPri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_namesuffixpri");
var SuffixSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_namesuffixsec");
var NumofOwners = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_numberofowners");
var OwnerTypePri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_otypecodepri");
var OwnerTypeSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_otypecodesec");
var OwnerRelationshipCodePri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownerrelationshipcode");
var OwnerRelationshipCodeSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownerrelationshipcodesec");
var OwnerStatusCodeSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownerstatuscodesec");
var OwnerStatusCodePri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownerstatuspri");
var CorporateStatusCodeSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownertypecodesec");
var CorporateStatusCodePri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_ownertypepri");
var StatePri = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_statepri");
var StateSec = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_statesec");
var SubmissionStatus = GetLookupFieldGuid(service, localContext, MyEntity, "new_s_checkstaging", "new_submissionstatus");
// throw new Exception("The Prefix GUID is: " + PrefixPri);
//Get Field Data - MEMO
var AdditionalInformationSec = GetFieldStringData(service, localContext, MyEntity, "new_additionalinformationasec");
var AdditionalInformationPri = GetFieldStringData(service, localContext, MyEntity, "new_additionalinformationpri");
//Get Field Data - CURRENCY
var CheckAmount = GetFieldMoneyDataAsDecimal(service, localContext, MyEntity, "new_checkamount");
//throw new Exception("The Check Number is: " + CheckNum);
//Get Field Data - DATE
var CheckDateAsString = GetFieldDate(service, localContext, MyEntity, "new_checkdate");
//throw new Exception("The Check Date is: " + CheckDateAsString);
//Get GUID of Transaction Record
//var CheckStagingGUID = new_s_checkstaging.Id;
CreateNewCheckInRegister(service, localContext, "new_s_checkinfo", "new_checkinfoid",
"new_recowner", RecordOwner,
"new_checkcatalognumber", CheckCatalogNum,
"new_bank", "new_s_bankinfo", Bank,
"new_bankcode", BankCode,
"new_checknum", CheckNum,
"new_checkamount", CheckAmount,
"new_checkdate", CheckDateAsString,
"new_issuereason", "new_s_issuereason" ,IssueReasonCode,
"new_issuereasonlong", IssueReason,
"new_checkyear", CheckYear,
"new_issuingdepartment", "new_s_countydepartment", IssuingDepartmentCode,
"new_issuingdepartmentlong", IssuingDepartment,
"new_numberofowners", "new_s_number", NumofOwners,
"new_submissionstatus", "new_s_submissionstatus", SubmissionStatus,
"new_scode", StatusCode,
"new_ownertypepri", "new_s_ownertype", CorporateStatusCodePri,
"new_ownertypelong", CorporateStatusPri,
"new_businessname", BusinessNamePri,
"new_nameprefixpri", "new_s_nameprefix", PrefixPri,
"new_firstnamepri", FirstNamePri,
"new_middlenamepri", MiddleNamePri,
"new_lastnamepri", LastNamePri,
"new_namesuffixpri", "new_s_namesuffix", SuffixPri,
"new_addressline1pri", AddressLine1Pri,
"new_addressline2pri", AddressLine2Pri,
"new_citypri", CityPri,
"new_statepri", "new_s_state", StatePri,
"new_zipcodepri", ZipCodePri,
"new_otypecodepri", "new_s_otype", OwnerTypePri,
"new_otypelongpri", OwnerTypeDescriptionPri,
"new_ownerrelationshipcode", "new_s_ownerrelationship", OwnerRelationshipCodePri,
"new_ownerrelationship", OwnerRelationshipPri,
"new_ownerstatuspri", "new_s_ownerstatus", OwnerStatusCodePri,
"new_ownerstatuslong", OwnerStatusPri,
"new_ssntaxid", SSNTAXIDNum,
"new_additionalinformationpri", AdditionalInformationPri,
"new_ownertypecodesec", "new_s_ownertype", CorporateStatusCodeSec,
"new_ownertypelongsec", CorporateStatusSec,
"new_businessnamesec", BusinessNameSec,
"new_nameprefixsec", "new_s_nameprefix", PrefixSec,
"new_firstnamesec", FirstNameSec,
"new_middlenamesec", MiddleNameSec,
"new_lastnamesec", LastNameSec,
"new_namesuffixsec", "new_s_namesuffix", SuffixSec,
"new_addressline1sec", AddressLine1Sec,
"new_addressline2sec", AddressLine2Sec,
"new_citysec", CitySec,
"new_statesec", "new_s_state", StateSec,
"new_zipcodesec", ZipCodeSec,
"new_otypecodesec", "new_s_otype", OwnerTypeSec,
"new_otypelongsec", OwnerTypeDescriptionSec,
"new_ownerrelationshipcodesec", "new_s_ownerrelationship", OwnerRelationshipCodeSec,
"new_ownerrelationshipsec", OwnerRelationshipSec,
"new_ownerstatuscodesec", "new_s_ownerstatus", OwnerStatusCodeSec,
"new_ownerstatuslongsec", OwnerStatusSec,
"new_ssntaxidsec", SSNTAXIDNum,
"new_additionalinformationsec", AdditionalInformationSec);
//throw new Exception("The Batch GUID is: " + CheckStagingGUID + " and the ID is: " + CheckStagingID);
var MyGuidValue = _new_checkinfoid.ToString();
//throw new Exception("The GUID is: " + MyGuidValue);
PerformLookupGUIDUpdate(service, localContext, MyEntity, MyGuidValue, "new_checkmanagementid", "new_s_checkinfo");
}
else { return; }
}
catch (Exception ex) { throw new Exception("Error in top level at TRY housing UpdateCheckRecordsWithTransactionID: " + ex.Message); }
}
else
{
throw new InvalidPluginExecutionException("ELSE Error - code went to ELSE inside the top level ExecutePostTestEntityCreate method when calling UpdateLicenseRecordsWithTransactionID(service,TransactionID); .");
}
}
//BUSINESS LOGIC FUNCTIONS
protected string GetLookupFieldGuid(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string CurrentEntity, string LookupFieldName)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
// Get the context
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
var postImage = context.PostEntityImages["PostImageAddImportCheckToCheckRegister"];
PassedEntity = postImage;
EntityReference lookupRef = PassedEntity.GetAttributeValue<EntityReference>(LookupFieldName);
if (lookupRef == null)
{
// no value set
return "NULL";
}
else
{
ColumnSet myAttributes = new ColumnSet(new String[] { LookupFieldName });
Entity myEntityHavingLookup = EntityService.Retrieve(CurrentEntity, PassedEntity.Id, myAttributes);
var myLookupId = ((Microsoft.Xrm.Sdk.EntityReference)(myEntityHavingLookup.Attributes[LookupFieldName])).Id;
var myLookupName = ((Microsoft.Xrm.Sdk.EntityReference)(myEntityHavingLookup.Attributes[LookupFieldName])).Name;
var MyGUIDAsString = myLookupId.ToString();
return MyGUIDAsString;
}
}
//catch (Exception ex) { throw new Exception("Error in GetTransactionMethod: " + ex.Message); }
catch {return "This is the catch exception for GetLookupFieldGuid Function"; }
}
else
{
try
{
var result2 = "BLANK";
return result2;
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY TransactionID Check: " + ex.Message); }
}
}
protected string GetFieldStringData(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
//Entity new_s_testentity = (Entity)context.InputParameters["Target"];
try
{
var postImage = context.PostEntityImages["PostImageAddImportCheckToCheckRegister"];
PassedEntity = postImage;
var MyFieldValue = PassedEntity.GetAttributeValue<string>(PassedField);
//var TranscationID = PassedEntity.GetAttributeValue<string>("new_testentityinfoid");
// throw new Exception("The Additional Info is: " + MyFieldValue);
if (String.IsNullOrEmpty(MyFieldValue))
{
try
{
var result = "NULL";
return result;
}
catch (Exception ex) { throw new Exception("Error in IF/TRY TransactionID Check: " + ex.Message); }
}
else
{
try
{
var result2 = MyFieldValue;
return result2;
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY TransactionID Check: " + ex.Message); }
}
}
catch (Exception ex) { throw new Exception("Error in GetFieldStringData: " + ex.Message); }
}
return null;
}
protected decimal GetFieldMoneyDataAsDecimal(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
//Entity new_s_testentity = (Entity)context.InputParameters["Target"];
try
{
var postImage = context.PostEntityImages["PostImageAddImportCheckToCheckRegister"];
PassedEntity = postImage;
Money MyFieldValue = (Money)PassedEntity.GetAttributeValue<Money>(PassedField);
if (MyFieldValue != null)
{
decimal actualAmount = MyFieldValue.Value;
return actualAmount;
}
else { return 0; }
}
catch (Exception ex) { throw new Exception("Error in GetFieldMoneyDataAsDecimal: " + ex.Message); }
}
return 0;
}
protected string GetFieldDate(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
//Entity new_s_testentity = (Entity)context.InputParameters["Target"];
try
{
var postImage = context.PostEntityImages["PostImageAddImportCheckToCheckRegister"];
PassedEntity = postImage;
DateTime MyFieldValue = (DateTime)PassedEntity.GetAttributeValue<DateTime>(PassedField);
if (MyFieldValue == DateTime.MinValue)
{
return "NULL";
}
else {
var StrDateTime = MyFieldValue.ToString("MM/dd/yyyy");
return StrDateTime; }
}
catch (Exception ex) { throw new Exception("Error in GetFieldDate: " + ex.Message); }
}
return "NULL";
}
protected void CreateNewCheckInRegister(IOrganizationService CreateService, LocalPluginContext CreateLocalContext, string CreateEntity, string PassedField,
string RecordOwner, string RecordOwnerVal,
string CheckCatalogNumber, string CheckCatalogNumberValue,
string BankGuid, string RelatedBankInfoEntity, string BankGuidValue,
string BankCode, string BankCodeValue,
string CheckNumber, string CheckNumberVal,
string CheckAmount, decimal CheckAmountValue,
string CheckDate, string CheckDateVal,
string IssueReasonCodeGuid, string RelatedIssueReasonCodeEntity, string IssueReasonCodeGuidValue,
string IssueReason, string IssueReasonVal,
string CheckYear, string CheckYearVal,
string IssuingDepartmentCodeGuid, string RelatedIssuingDepartmentEntity, string IssuingDepartmentCodeGuidValue,
string IssuingDepartment, string IssuingDepartmentValue,
string NumberOfOwnerGuid, string RelatedNumberOfOwnerEntity, string NumberOfOwnerGuidValue,
string SubmissionStatusGuid, string RelatedSubmissionStatusEntity, string SubmissionStatusGuidValue,
string StatusCode, string StatusCodeValue,
string CorporateStatusCodePriGuid, string RelatedCorporateStatusCodePriEntity, string CorporateStatusCodePriGuidValue,
string CorporateStatusPri, string CorporateStatusPriValue,
string BusinessNamePri, string BusinessNamePriValue,
string PrefixPriGuid, string RelatedPrefixPriEntity, string PrefixPriGuidValue,
string FirstNamePri, string FirstNamePriValue,
string MiddleNamePri, string MiddleNamePriValue,
string LastNamePri, string LastNamePriValue,
string SuffixPriGuid, string RelatedSuffixPriEntity, string SuffixPriGuidValue,
string AddressLine1Pri, string AddressLine1PriValue,
string AddressLine2Pri, string AddressLine2PriValue,
string CityPri, string CityPriValue,
string StatePriGuid, string RelatedStatePriEntity, string StatePriGuidValue,
string ZipCodePri, string ZipCodePriValue,
string OwnerTypeCodePriGuid, string RelatedOwnerTypePriEntity, string OwnerTypeCodePriGuidValue,
string OwnerTypeCodePriDescription, string OwnerTypeCodePriDescriptionValue,
string OwnerRelationshipCodePriGuid, string ReleatedOwnerRelationshipPriEntity, string OwnerRelationshipCodePriGuidValue,
string OwnerRelationshipPri, string OwnerRelationshipPriValue,
string OwnerStatusCodePriGuid, string RelatedOwnerStatusPriEntity, string OwnerStatusCodePriGuidValue,
string OwnerStatusPri, string OwnerStatusPriValue,
string SSNTaxIDPri, string SSNTaxIDPriValue,
string AdditionalInfoPri, string AdditionalInfoPriValue,
string CorporateStatusCodeSecGuid, string RelatedCorporateStatusCodeSecEntity, string CorporateStatusCodeSecGuidValue,
string CorporateStatusSec, string CorporateStatusSecValue,
string BusinessNameSec, string BusinessNameSecValue,
string PrefixSecGuid, string RelatedPrefixSecEntity, string PrefixSecGuidValue,
string FirstNameSec, string FirstNameSecValue,
string MiddleNameSec, string MiddleNameSecValue,
string LastNameSec, string LastNameSecValue,
string SuffixSecGuid, string RelatedSuffixSecEntity, string SuffixSecGuidValue,
string AddressLine1Sec, string AddressLine1SecValue,
string AddressLine2Sec, string AddressLine2SecValue,
string CitySec, string CitySecValue,
string StateSecGuid, string RelatedStateSecEntity, string StateSecGuidValue,
string ZipCodeSec, string ZipCodeSecValue,
string OwnerTypeCodeSecGuid, string RelatedOwnerTypeSecEntity, string OwnerTypeCodeSecGuidValue,
string OwnerTypeCodeSecDescription, string OwnerTypeCodeSecDescriptionValue,
string OwnerRelationshipCodeSecGuid, string ReleatedOwnerRelationshipSecEntity, string OwnerRelationshipCodeSecGuidValue,
string OwnerRelationshipSec, string OwnerRelationshipSecValue,
string OwnerStatusCodeSecGuid, string RelatedOwnerStatusSecEntity, string OwnerStatusCodeSecGuidValue,
string OwnerStatusSec, string OwnerStatusSecValue,
string SSNTaxIDSec, string SSNTaxIDSecValue,
string AdditionalInfoSec, string AdditionalInfoSecValue
)
{
IPluginExecutionContext context = CreateLocalContext.PluginExecutionContext;
CreateService = CreateLocalContext.OrganizationService;
ITracingService TracingSvc = CreateLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity newCheckManagementCheck = new Entity(CreateEntity);
newCheckManagementCheck[PassedField] = "<AutoNumber>";
_new_checkinfoid = CreateService.Create(newCheckManagementCheck);
Console.Write("{0} {1} created, ", newCheckManagementCheck.LogicalName, newCheckManagementCheck.Attributes[PassedField]);
// Create a column set to define which attributes should be retrieved.
ColumnSet attributes = new ColumnSet(new string[] { PassedField});
// Retrieve the account and its name and ownerid attributes.
newCheckManagementCheck = CreateService.Retrieve(newCheckManagementCheck.LogicalName, _new_checkinfoid, attributes);
Console.Write("retrieved, ");
if (RecordOwnerVal != null && RecordOwnerVal != "NULL" && RecordOwnerVal != "") { newCheckManagementCheck[RecordOwner] = RecordOwnerVal; }
if (CheckCatalogNumberValue != null && CheckCatalogNumberValue != "NULL" && CheckCatalogNumberValue != "") { newCheckManagementCheck[CheckCatalogNumber] = CheckCatalogNumberValue; }
if (BankCodeValue != null && BankCodeValue != "NULL" && BankCodeValue != "") { newCheckManagementCheck[BankCode] = BankCodeValue; }
if (CheckNumberVal != null && CheckNumberVal != "NULL" && CheckNumberVal != "") { newCheckManagementCheck[CheckNumber] = CheckNumberVal; }
if (IssueReasonVal != null && IssueReasonVal != "NULL" && IssueReasonVal != "") { newCheckManagementCheck[IssueReason] = IssueReasonVal; }
if (CheckYearVal != null && CheckYearVal != "") { newCheckManagementCheck[CheckYear] = CheckYearVal; }
if (IssuingDepartmentValue != null && IssuingDepartmentValue != "NULL" && IssuingDepartmentValue != "") { newCheckManagementCheck[IssuingDepartment] = IssuingDepartmentValue; }
if (StatusCodeValue != null && StatusCodeValue != "NULL" && StatusCodeValue != "") { newCheckManagementCheck[StatusCode] = StatusCodeValue; }
if (CorporateStatusPriValue != null && CorporateStatusPriValue != "NULL" && CorporateStatusPriValue != "") { newCheckManagementCheck[CorporateStatusPri] = CorporateStatusPriValue; }
if (BusinessNamePriValue != null && BusinessNamePriValue != "NULL" && BusinessNamePriValue != "") { newCheckManagementCheck[BusinessNamePri] = BusinessNamePriValue; }
if (FirstNamePriValue != null && FirstNamePriValue != "NULL" && FirstNamePriValue != "") { newCheckManagementCheck[FirstNamePri] = FirstNamePriValue; }
if (MiddleNamePriValue != null && MiddleNamePriValue != "NULL" && MiddleNamePriValue != "") { newCheckManagementCheck[MiddleNamePri] = MiddleNamePriValue; }
if (LastNamePriValue != null && LastNamePriValue != "") { newCheckManagementCheck[LastNamePri] = LastNamePriValue; }
if (AddressLine1PriValue != null && AddressLine1PriValue != "NULL" && AddressLine1PriValue != "") { newCheckManagementCheck[AddressLine1Pri] = AddressLine1PriValue; }
if (AddressLine2PriValue != null && AddressLine2PriValue != "NULL" && AddressLine2PriValue != "") { newCheckManagementCheck[AddressLine2Pri] = AddressLine2PriValue; }
if (CityPriValue != null && CityPriValue != "NULL" && CityPriValue != "") { newCheckManagementCheck[CityPri] = CityPriValue; }
if (ZipCodePriValue != null && ZipCodePriValue != "NULL" && ZipCodePriValue != "") { newCheckManagementCheck[ZipCodePri] = ZipCodePriValue; }
if (OwnerTypeCodePriDescriptionValue != null && OwnerTypeCodePriDescriptionValue != "NULL" && OwnerTypeCodePriDescriptionValue != "") { newCheckManagementCheck[OwnerTypeCodePriDescription] = OwnerTypeCodePriDescriptionValue; }
if (OwnerRelationshipPriValue != null && OwnerRelationshipPriValue != "NULL" && OwnerRelationshipPriValue != "") { newCheckManagementCheck[OwnerRelationshipPri] = OwnerRelationshipPriValue; }
if (OwnerStatusPriValue != null && OwnerStatusPriValue != "NULL" && OwnerStatusPriValue != "") { newCheckManagementCheck[OwnerStatusPri] = OwnerStatusPriValue; }
if (SSNTaxIDPriValue != null && SSNTaxIDPriValue != "NULL" && SSNTaxIDPriValue != "") { newCheckManagementCheck[SSNTaxIDPri] = SSNTaxIDPriValue; }
if (AdditionalInfoPriValue != null && AdditionalInfoPriValue != "NULL" && AdditionalInfoPriValue != "") { newCheckManagementCheck[AdditionalInfoPri] = AdditionalInfoPriValue; }
if (CorporateStatusSecValue != null && CorporateStatusSecValue != "NULL" && CorporateStatusSecValue != "") { newCheckManagementCheck[CorporateStatusSec] = CorporateStatusSecValue; }
if (BusinessNameSecValue != null && BusinessNameSecValue != "NULL" && BusinessNameSecValue != "") { newCheckManagementCheck[BusinessNameSec] = BusinessNameSecValue; }
if (FirstNameSecValue != null && FirstNameSecValue != "NULL" && FirstNameSecValue != "") { newCheckManagementCheck[FirstNameSec] = FirstNameSecValue; }
if (MiddleNameSecValue != null && MiddleNameSecValue != "NULL" && MiddleNameSecValue != "") { newCheckManagementCheck[MiddleNameSec] = MiddleNameSecValue; }
if (LastNameSecValue != null && LastNameSecValue != "") { newCheckManagementCheck[LastNameSec] = LastNameSecValue; }
if (AddressLine1SecValue != null && AddressLine1SecValue != "NULL" && AddressLine1SecValue != "") { newCheckManagementCheck[AddressLine1Sec] = AddressLine1SecValue; }
if (AddressLine2SecValue != null && AddressLine2SecValue != "NULL" && AddressLine2SecValue != "") { newCheckManagementCheck[AddressLine2Sec] = AddressLine2SecValue; }
if (CitySecValue != null && CitySecValue != "NULL" && CitySecValue != "") { newCheckManagementCheck[CitySec] = CitySecValue; }
if (ZipCodeSecValue != null && ZipCodeSecValue != "NULL" && ZipCodeSecValue != "") { newCheckManagementCheck[ZipCodeSec] = ZipCodeSecValue; }
if (OwnerTypeCodeSecDescriptionValue != null && OwnerTypeCodeSecDescriptionValue != "NULL" && OwnerTypeCodeSecDescriptionValue != "") { newCheckManagementCheck[OwnerTypeCodeSecDescription] = OwnerTypeCodeSecDescriptionValue; }
if (OwnerRelationshipSecValue != null && OwnerRelationshipSecValue != "NULL" && OwnerRelationshipSecValue != "") { newCheckManagementCheck[OwnerRelationshipSec] = OwnerRelationshipSecValue; }
if (OwnerStatusSecValue != null && OwnerStatusSecValue != "NULL" && OwnerStatusSecValue != "") { newCheckManagementCheck[OwnerStatusSec] = OwnerStatusSecValue; }
if (SSNTaxIDSecValue != null && SSNTaxIDSecValue != "NULL" && SSNTaxIDSecValue != "") { newCheckManagementCheck[SSNTaxIDSec] = SSNTaxIDSecValue; }
if (AdditionalInfoSecValue != null && AdditionalInfoSecValue != "NULL" && AdditionalInfoSecValue != "") { newCheckManagementCheck[AdditionalInfoSec] = AdditionalInfoSecValue; }
// FOR LOOKUPS USE THIS
if (BankGuidValue != null && BankCodeValue != "NULL" && BankGuidValue != "") { newCheckManagementCheck[BankGuid] = new EntityReference(RelatedBankInfoEntity, new Guid(BankGuidValue)); }
if (IssueReasonCodeGuidValue != null && IssueReasonCodeGuidValue != "NULL" && IssueReasonCodeGuidValue != "") { newCheckManagementCheck[IssueReasonCodeGuid] = new EntityReference(RelatedIssueReasonCodeEntity, new Guid(IssueReasonCodeGuidValue)); }
if (IssuingDepartmentCodeGuidValue != null && IssuingDepartmentCodeGuidValue != "NULL" && IssuingDepartmentCodeGuidValue != "") { newCheckManagementCheck[IssuingDepartmentCodeGuid] = new EntityReference(RelatedIssuingDepartmentEntity, new Guid(IssuingDepartmentCodeGuidValue)); }
if (NumberOfOwnerGuidValue != null && NumberOfOwnerGuidValue != "NULL" && NumberOfOwnerGuidValue != "") { newCheckManagementCheck[NumberOfOwnerGuid] = new EntityReference(RelatedNumberOfOwnerEntity, new Guid(NumberOfOwnerGuidValue)); }
if (SubmissionStatusGuidValue != null && SubmissionStatusGuidValue != "NULL" && SubmissionStatusGuidValue != "") { newCheckManagementCheck[SubmissionStatusGuid] = new EntityReference(RelatedSubmissionStatusEntity, new Guid(SubmissionStatusGuidValue)); }
if (CorporateStatusCodePriGuidValue != null && CorporateStatusCodePriGuidValue != "NULL" && CorporateStatusCodePriGuidValue != "") { newCheckManagementCheck[CorporateStatusCodePriGuid] = new EntityReference(RelatedCorporateStatusCodePriEntity, new Guid(CorporateStatusCodePriGuidValue)); }
if (PrefixPriGuidValue != null && PrefixPriGuidValue != "NULL" && PrefixPriGuidValue != "") { newCheckManagementCheck[PrefixPriGuid] = new EntityReference(RelatedPrefixPriEntity, new Guid(PrefixPriGuidValue)); }
if (SuffixPriGuidValue != null && SuffixPriGuidValue != "NULL" && SuffixPriGuidValue != "") { newCheckManagementCheck[SuffixPriGuid] = new EntityReference(RelatedSuffixPriEntity, new Guid(SuffixPriGuidValue)); }
if (StatePriGuidValue != null && StatePriGuidValue != "NULL" && StatePriGuidValue != "") { newCheckManagementCheck[StatePriGuid] = new EntityReference(RelatedStatePriEntity, new Guid(StatePriGuidValue)); }
if (OwnerTypeCodePriGuidValue != null && OwnerTypeCodePriGuidValue != "NULL" && OwnerTypeCodePriGuidValue != "") { newCheckManagementCheck[OwnerTypeCodePriGuid] = new EntityReference(RelatedOwnerTypePriEntity, new Guid(OwnerTypeCodePriGuidValue)); }
if (OwnerRelationshipCodePriGuidValue != null && OwnerRelationshipCodePriGuidValue != "NULL" && OwnerRelationshipCodePriGuidValue != "") { newCheckManagementCheck[OwnerRelationshipCodePriGuid] = new EntityReference(ReleatedOwnerRelationshipPriEntity, new Guid(OwnerRelationshipCodePriGuidValue)); }
if (OwnerStatusCodePriGuidValue != null && OwnerStatusCodePriGuidValue != "NULL" && OwnerStatusCodePriGuidValue != "") { newCheckManagementCheck[OwnerStatusCodePriGuid] = new EntityReference(RelatedOwnerStatusPriEntity, new Guid(OwnerStatusCodePriGuidValue)); }
if (CorporateStatusCodeSecGuidValue != null && CorporateStatusCodeSecGuidValue != "NULL" && CorporateStatusCodeSecGuidValue != "") { newCheckManagementCheck[CorporateStatusCodeSecGuid] = new EntityReference(RelatedCorporateStatusCodeSecEntity, new Guid(CorporateStatusCodeSecGuidValue)); }
if (PrefixSecGuidValue != null && PrefixSecGuidValue != "NULL" && PrefixSecGuidValue != "") { newCheckManagementCheck[PrefixSecGuid] = new EntityReference(RelatedPrefixSecEntity, new Guid(PrefixSecGuidValue)); }
if (SuffixSecGuidValue != null && SuffixSecGuidValue != "NULL" && SuffixSecGuidValue != "") { newCheckManagementCheck[SuffixSecGuid] = new EntityReference(RelatedSuffixSecEntity, new Guid(SuffixSecGuidValue)); }
if (StateSecGuidValue != null && StateSecGuidValue != "NULL" && StateSecGuidValue != "") { newCheckManagementCheck[StateSecGuid] = new EntityReference(RelatedStateSecEntity, new Guid(StateSecGuidValue)); }
if (OwnerTypeCodeSecGuidValue != null && OwnerTypeCodeSecGuidValue != "NULL" && OwnerTypeCodeSecGuidValue != "") { newCheckManagementCheck[OwnerTypeCodeSecGuid] = new EntityReference(RelatedOwnerTypeSecEntity, new Guid(OwnerTypeCodeSecGuidValue)); }
if (OwnerRelationshipCodeSecGuidValue != null && OwnerRelationshipCodeSecGuidValue != "NULL" && OwnerRelationshipCodeSecGuidValue != "") { newCheckManagementCheck[OwnerRelationshipCodeSecGuid] = new EntityReference(ReleatedOwnerRelationshipSecEntity, new Guid(OwnerRelationshipCodeSecGuidValue)); }
if (OwnerStatusCodeSecGuidValue != null && OwnerStatusCodeSecGuidValue != "NULL" && OwnerStatusCodeSecGuidValue != "") { newCheckManagementCheck[OwnerStatusCodeSecGuid] = new EntityReference(RelatedOwnerStatusSecEntity, new Guid(OwnerStatusCodeSecGuidValue)); }
// FOR DATES USE THIS
if (CheckDateVal != null && CheckDateVal != "") { newCheckManagementCheck[CheckDate] = DateTime.Parse(CheckDateVal); }
// FOR MONEY USE THIS
if (CheckAmountValue >= 0) { newCheckManagementCheck[CheckAmount] = new Money((decimal)(CheckAmountValue)); }
CreateService.Update(newCheckManagementCheck);
Console.WriteLine("and updated.");
}
catch (Exception ex) { throw new Exception("Error in IF/TRY Failed to Update Entities: " + ex.Message); }
}
else{}
}
protected void PerformLookupGUIDUpdate(IOrganizationService UpdateService,
LocalPluginContext UpdateLocalContext,
Entity PassedEntity,
string PassedCheckInfoID, string PassedUpdateField, string PassedRelatedEntityName)
{
IPluginExecutionContext context = UpdateLocalContext.PluginExecutionContext;
UpdateService = UpdateLocalContext.OrganizationService;
ITracingService TracingSvc = UpdateLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
//EVERYTHING UP TO THIS POINT WORKS
PassedEntity[PassedUpdateField] = new EntityReference(PassedRelatedEntityName, new Guid(PassedCheckInfoID));
UpdateService.Update(PassedEntity);
//EVERYTHING BELOW THIS POINT WORKS
}
catch (Exception ex) { throw new Exception("Error in IF/TRY Failed to Update Entities: " + ex.Message); }
}
else
{
try
{
TracingSvc.Trace("Error! Code went to ELSE/TRY in PerformUpdate method and should have never went here.");
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY in PerformUpdate method and should have never went here. Code went to Catch but should never have entered this entire section." + ex.Message); }
}
}
//END - DONT PUT CODE BELOW HERE
}
}