Hi,
We have a plugin with the following setup:
- Entity: Opportunity
- Plugin Event: Create
- Step Stage: Post-Operation
- Execution Mode: Synchronous
Description:
- We execute various logic that may update different attributes of an opportunity
- We initialize an opportunity entity and as the code executes, different attributes are set such as "new_originalownerid", "new_link", "new_usermodifiedon".
- After all that logic executes, at the very end, we check if that entity has any attributes set. If so, we use the service to update the opportunity
Problem:
- Some fields are updated and some are not. How is this possible if it's one service call and there's no if/else that blocks the business logic from executing
- i.e. new_originalownerid will be updated, but the new_creditbuilderlink will not be updated
Code:
public class Opportunity_Create_Post_Sync : PluginBase { #region IPlugin Members public override void ExecuteLogic() { Entity opportunity = null; try { opportunity = (Entity)Context.InputParameters["Target"]; opportunity = Service.Retrieve(opportunity.LogicalName, opportunity.Id, new ColumnSet(true)); Entity updateOpportunity = new Entity(opportunity.LogicalName); updateOpportunity.Id = opportunity.Id; #region Populate different information on opportunity //### Generate App ID Post_Opportunity_AppID.GenerateOpportunityAppID(Service, opportunity, updateOpportunity); //### Populate Original Owner if (opportunity.GetAttributeValue<EntityReference>("new_originalownerid") == null) { updateOpportunity["new_originalownerid"] = opportunity.GetAttributeValue<EntityReference>("ownerid"); } //### Check if Opportunity has Inside Sales Agents (Used on Opportunity View for Inside Sales) bool hasInsideSales = Post_OpportunityUpdate_HasInsideSales.CheckUserRoleAndUpdateFlag(Service, opportunity, updateOpportunity); //### Populate OriginalCreatedBy EntityReference OriginalCreatedBy = Post_OpportunityCreated_OriginalCreatedBy.SetOpportunityOriginalCreatedBy(Service, opportunity, updateOpportunity); //### Update opportunity source to Telemarketing when calling agents are share credit if (PublicFunctions.IsUserInRoles(Service, OriginalCreatedBy.Id, Constants.ROLE_INSIDE_SALES_AGENT) && hasInsideSales && opportunity.GetAttributeValue<OptionSetValue>("opportunityratingcode")?.Value != Constants.OppSourceTelMarketing) { updateOpportunity["opportunityratingcode"] = new OptionSetValue(Constants.OppSourceTelMarketing); } //### Save CreditBuilder Link using OpportunityId string creditBuilderUrl = PluginBase.GetKeyValue<string>("CRMCREDITBUILDER_URI_BASE") + "/Default.aspx?id={0}"; updateOpportunity["new_creditbuilderlink"] = string.Format(creditBuilderUrl, opportunity.Id.ToString()); //### Populate Group Campaign ID and name Post_OpportunityCreateUpdate_GroupCampaign.UpdateGroupCampaign(opportunity, updateOpportunity); if (opportunity.GetAttributeValue<EntityReference>("modifiedby").Id != new Guid(Constants.CRM_GUID_ID_CRM_ADMIN)) { updateOpportunity["new_usermodifiedon"] = opportunity.GetAttributeValue<DateTime>("modifiedon").ToLocalTime(); } if (updateOpportunity.Attributes.Count > 0) { Service.Update(updateOpportunity); } #endregion //### If there are share credit users, auto add them to "share users" if (opportunity.GetAttributeValue<EntityReference>("new_sharecreditid") != null || opportunity.GetAttributeValue<EntityReference>("new_sharecredit2id") != null) { Post_OpportunityShareCredit_SetSharingPermission.SetSharingPermission(Service, opportunity); } } catch (Exception ex) { ErrorHandler.SendErrorMessage(typeof(Opportunity_Create_Post_Sync).Name, MethodBase.GetCurrentMethod().Name, $"opportunityEntity: {Newtonsoft.Json.JsonConvert.SerializeObject(opportunity)} <br/> ", ex); } } #endregion }
Thank you for taking the time to answer this question!
*This post is locked for comments