I have a CRM 2011 plugin that executes Post-Operation on Update.
The plugin pulls data from each field on the form, and stores each field value in its own variable.
This data will later be manipulated and inserted into a newly created record, but I cannot do that or go that far until I can first get all the data from the fields and store them in variables without incident.
Now, I can get all my text box fields, and I can get the Guid values for all my lookup fields.
Some lookup fields may not have anything selected, so my code even handles that properly, and returns a “NULL” in place of what would be an exception/error.
My problem is that I am unable to get the value of the money field.
I have a function named GetFieldMoneyDataAsDecimal
And I pass it the following (service, localContext, new_s_checkstaging, "new_checkamount");
Using the following:
var CheckAmount = GetFieldMoneyDataAsDecimal(service, localContext, new_s_checkstaging, "new_checkamount");
GetFieldMoneyDataAsDecimal is then supposed to receive that information, and use it to GET the value of the money field. In a sample record, I have 3267.22 in the money field, so my expected result is that will ultimately be placed in CheckAmount is 3267.22
This isn’t happening and I am at a loss for how to proceed.
I already tried using the plugin profiler here -- https://support.microsoft.com/en-us/help/2778280/step-by-step-working-with-crm2011-plugin-profiler but I am not getting any useful information, and am instead getting even more error so I am clearly doing something very wrong.
My code is shown below
// 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 new_s_checkstaging = (Entity)context.InputParameters["Target"]; //THERE IS A TON OF CODE HERE THAT I AM NOT SHOWING – BUT WILL SHOW LATERON IN THIS PROBLEM DESCRIPTION //ALL THAT CODE WORKS PERFECTLY AND GETS ME THE VALUES OF ALL THE TEXT AND LOOKUP FIELDS I NEED //AND RETURNS THOSE VALUES IN THE EXPECTED FORMAT, SO IT SEEMS I AM JUST HAVING PROBLEMS WITH THE //CODE THAT GETS THE CURRENCY VALUES FROM THE CURRENCY FIELDS AS SHOWN BELOW. //Get Field Data - CURRENCY var CheckAmount = GetFieldMoneyDataAsDecimal(service, localContext, new_s_checkstaging, "new_checkamount"); //CODE TO TEST THE VALUE OBTAINED TO MAKE SURE ITS CORRECT throw new Exception("CheckAmount is: " + CheckAmount); } 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 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 TranscationID = PassedEntity.GetAttributeValue<string>("new_testentityinfoid"); //// if (String.IsNullOrEmpty(MyFieldValue)) // if (((Money)PassedEntity.Attributes[PassedField]).Value < 0) // { // try // { Money MyFieldValue = (Money)PassedEntity.GetAttributeValue<Money>(PassedField); decimal actualAmount = MyFieldValue.Value; return actualAmount; // } // catch (Exception ex) { throw new Exception("Error in IF/TRY TransactionID Check: " + ex.Message); } //} //else //{ // try // { // var result2 = 0; // return result2; // } // catch (Exception ex) { throw new Exception("Error in ELSE/TRY TransactionID Check: " + ex.Message); } //} } //THIS IS WHERE THE ERROR HAPPENS, AND THE CATCH EXCEPTION IS THROWN catch (Exception ex) { throw new Exception("Error in GetTransactionMethod: " + ex.Message); } } return 0; }
Any assistance in helping me to identify the exact problem and how to rectify it would be greatly appreciated.
The code that does work, is the code that gets all of the values of the text fields as well as all of the Lookup Fields Guid values. I just need to do the same thing with all the money fields, and that’s my problem.
The get text and get Guid from Lookup fields code that works great is shown below.
//Get Field Data - TEXT var CheckStagingID = GetFieldStringData(service, localContext, new_s_checkstaging, "new_chkstagenum"); var AddressLine1Pri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_addressline1pri"); var AddressLine1Sec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_addressline1sec"); var AddressLine2Pri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_addressline2pri"); var AddressLine2Sec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_addressline2sec"); var BankCode = GetFieldStringData(service, localContext, new_s_checkstaging, "new_bankcode"); var BusinessNamePri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_businessname"); var BusinessNameSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_businessnamesec"); var CheckCatalogNum = GetFieldStringData(service, localContext, new_s_checkstaging, "new_checkcatalognumber"); var CheckNum = GetFieldStringData(service, localContext, new_s_checkstaging, "new_checknum"); var CheckYear = GetFieldStringData(service, localContext, new_s_checkstaging, "new_checkyear"); var CheckStagingNumber = GetFieldStringData(service, localContext, new_s_checkstaging, "new_chkstagenum"); var CityPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_citypri"); var CitySec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_citysec"); var FirstNamePri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_firstnamepri"); var FirstNameSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_firstnamesec"); var IssueReason = GetFieldStringData(service, localContext, new_s_checkstaging, "new_issuereasonlong"); var IssuingDepartment = GetFieldStringData(service, localContext, new_s_checkstaging, "new_issuingdepartmentlong"); var LastNamePri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_lastnamepri"); var LastNameSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_lastnamesec"); var MiddleNamePri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_middlenamepri"); var MiddleNameSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_middlenamesec"); var OwnerTypeDescriptionPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_otypelongpri"); var OwnerTypeDescriptionSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_otypelongsec"); var OwnerRelationshipPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownerrelationship"); var OwnerRelationshipSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownerrelationshipsec"); var OwnerStatusPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownerstatuslong"); var OwnerStatusSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownerstatuslongsec"); var CorporateStatusPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownertypelong"); var CorporateStatusSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ownertypelongsec"); var PropertyOwnerType = GetFieldStringData(service, localContext, new_s_checkstaging, "new_potype"); var RecordOwner = GetFieldStringData(service, localContext, new_s_checkstaging, "new_recowner"); var StatusCode = GetFieldStringData(service, localContext, new_s_checkstaging, "new_scode"); var SSNTAXIDNum = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ssntaxid"); var SSNTAXIDNumSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_ssntaxidsec"); var ZipCodePri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_zipcodepri"); var ZipCodeSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_zipcodesec"); //Get Field Data - LOOKUP var Bank = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_bank"); var IssueReasonCode = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_issuereason"); var CheckID = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_checkmanagementid"); var IssuingDepartmentCode = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_issuingdepartment"); var PrefixPri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_nameprefixpri"); var PrefixSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_nameprefixsec"); var SuffixPri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_namesuffixpri"); var SuffixSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_namesuffixsec"); var NumofOwners = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_numberofowners"); var OwnerTypePri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_otypecodepri"); var OwnerTypeSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_otypecodesec"); var OwnerRelationshipCodePri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownerrelationshipcode"); var OwnerRelationshipCodeSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownerrelationshipcodesec"); var OwnerStatusCodeSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownerstatuscodesec"); var OwnerStatusCodePri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownerstatuspri"); var CorporateStatusCodeSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownertypecodesec"); var CorporateStatusCodePri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_ownertypepri"); var StatePri = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_statepri"); var StateSec = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_statesec"); var SubmissionStatus = GetLookupFieldGuid(service, localContext, new_s_checkstaging, "new_s_checkstaging", "new_submissionstatus"); //Get Field Data - MEMO var AdditionalInformationSec = GetFieldStringData(service, localContext, new_s_checkstaging, "new_additionalInformationasec"); var AdditionalInformationPri = GetFieldStringData(service, localContext, new_s_checkstaging, "new_additionalInformationpri"); //throw new Exception("The Bank GUID is: " + Bank + // " ,the Issue Reason Code is: " + IssueReasonCode + // ", the Check ID is: " + CheckID + // ", the Issuing Dept Code is: " + IssuingDepartmentCode + // ", the Prefix(Pri) is: " + PrefixPri + // ", the Prefix(Sec) is: " + PrefixSec + // ", the Suffix(Pri) is: " + SuffixPri + // ", the Suffix(Sec) is: " + SuffixSec + // ", the NumofOwners is: " + NumofOwners + // ", the OwnerType(Pri) is: " + OwnerTypePri + // ", the OwnerType(Sec) is: " + OwnerTypeSec + // ", the OwnerRelationshipCode(Pri) is: " + OwnerRelationshipCodePri + // ", the OwnerRelationshipCode(Sec) is: " + OwnerRelationshipCodeSec + // ", the OwnerStatusCode(Pri) is: " + OwnerStatusCodePri + // ", the OwnerStatusCode(Sec) is: " + OwnerStatusCodeSec + // ", the CorporateStatusCode(Pri) is: " + CorporateStatusCodePri + // ", the CorporateStatusCode(Sec) is: " + CorporateStatusCodeSec + // ", the State(Pri) is: " + StatePri + // ", the State(Sec) is: " + StateSec + // ", the SubmissionStatus is: " + SubmissionStatus + // ", the AdditionalInformationPri is: " + AdditionalInformationPri + // ", the AdditionalInformationSec is: " + AdditionalInformationSec); //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) { //PassedEntity = (Entity)context.InputParameters["Target"]; //Entity new_s_testentity = (Entity)context.InputParameters["Target"]; try { //Lookup Attribute: new_MyLookupattribute, //Current Entity: new_myentity, ColumnSet myAttributes = new ColumnSet(new String[] { LookupFieldName }); Entity myEntityHavingLookup = EntityService.Retrieve(CurrentEntity, PassedEntity.Id, myAttributes); if (((Microsoft.Xrm.Sdk.EntityReference)(myEntityHavingLookup.Attributes[LookupFieldName])).Id != null) { 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; } else { return "NULL"; } } //catch (Exception ex) { throw new Exception("Error in GetTransactionMethod: " + ex.Message); } catch {return "NULL"; } } 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 MyFieldValue = PassedEntity.GetAttributeValue<string>(PassedField); //var TranscationID = PassedEntity.GetAttributeValue<string>("new_testentityinfoid"); 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 GetTransactionMethod: " + ex.Message); } } return null; }
*This post is locked for comments