Reputation: 271
I am building a customized add in for our CRM system. When end users meet certain conditions, I would like to only pop up a message box once. I am not sure whether I do correctly in the follow implementation:
I declare a trigger variables in global scope:
public class MyWorkspaceAddIn : Panel, IWorkspaceComponent2
{
private bool _readOnly;
private IRecordContext _recordContext;
private IGlobalContext _globalContext;
private bool _triggerPopup = true;
The pop up code in the following class:
void _incident_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{//check property and/or i.Prod and then show the popup
// MessageBox.Show(e.PropertyName); //Check what property name is
if (_incident.ProductID == 182 && _triggerPopup) //If product ID is 183 in incident
{
MessageBox.Show("The GO Classic will soon be end of life, make sure you propose the customer to buy a new device and offer maximum 20% discount to reward his/her loyalty (NOTE: refurbished devices are not included in this offer).");
_triggerPopup = false; //Do not pop up
}
I hope the message box will only pop up once.
Full code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.AddIn;
using RightNow.AddIns.AddInViews;
using RightNow.AddIns.Common;
using System.Windows.Forms;
////////////////////////////////////////////////////////////////////////////////
//
// File: MyWorkspaceAddIn.cs
//
// Comments:
//
// Notes:
//
// Pre-Conditions:
//
////////////////////////////////////////////////////////////////////////////////
namespace TriggerAddIn
{
[AddIn("My VAS AddIn", Version = "1.1.0.2")]
public class MyWorkspaceAddIn : Panel, IWorkspaceComponent2
{
private bool _readOnly;
private IRecordContext _recordContext;
private IGlobalContext _globalContext;
private bool _triggerPopup = true;
IIncident _incident; //Define IIncident outside dataLoaded event;
//Get reference when the incident open in workspace.
public MyWorkspaceAddIn(bool inDesignMode, IRecordContext recContext, IGlobalContext globalContext)
{
/*MessageBox.Show("AddIns Load My workspace plugin");*/
_recordContext = recContext;
_globalContext = globalContext;
//Check wheather users
if (!inDesignMode &&_recordContext != null)
{ //Add our custom event
_recordContext.DataLoaded += new EventHandler(_recordContext_DataLoaded);
}
}
//Custom Event handler
void _recordContext_DataLoaded(object sender, EventArgs e)
{
_incident = _recordContext.GetWorkspaceRecord(WorkspaceRecordType.Incident) as IIncident;
if (_incident != null)
{
_incident.PropertyChanged -= _incident_PropertyChanged;
_incident.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_incident_PropertyChanged);
}
}
void _incident_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{//check property and/or i.Prod and then show the popup
// MessageBox.Show(e.PropertyName); //Check what property name is
if (_incident.ProductID == 182 && _triggerPopup) //If product ID is 183 in incident
{
MessageBox.Show("The GO Classic will soon be end of life, make sure you propose the customer to buy a new device and offer maximum 20% discount to reward his/her loyalty (NOTE: refurbished devices are not included in this offer).");
_triggerPopup = false; //Do not pop up
}
/*
if (_incident.CategoryID == 2353)
{
Form1 myForm = new Form1();
myForm.ShowDialog();
}*/
}
#region IAddInControl Members
public Control GetControl()
{
return this;
}
#endregion
#region IWorkspaceComponent2 Members
public bool ReadOnly
{
get
{
return _readOnly;
}
set
{
_readOnly = value;
}
}
public void RuleActionInvoked(string actionName)
{
}
public string RuleConditionInvoked(string conditionName)
{
return "";
}
#endregion
}
[AddIn("My VAS Factory AddIn", Version = "1.1.0.2")]
public class MyWorkspaceAddInFactory : IWorkspaceComponentFactory2
{
private IGlobalContext _globalContext;
#region IWorkspaceComponentFactory2 Members
IWorkspaceComponent2 IWorkspaceComponentFactory2.CreateControl(bool inDesignMode, IRecordContext context)
{
return new MyWorkspaceAddIn(inDesignMode, context, _globalContext);
}
#endregion
#region IFactoryBase Members
public System.Drawing.Image Image16
{
get { return Properties.Resources.AddIn16; }
}
public string Text
{
get { return "Trigger add in for VAS"; }
}
public string Tooltip
{
get { return "Trigger add in for VAS Tooltip"; }
}
#endregion
#region IAddInBase Members
public bool Initialize(IGlobalContext context)
{
_globalContext = context;
return true;
}
#endregion
}
}
Upvotes: 1
Views: 1485
Reputation: 2005
This looks good but what you can also do is just implement your own event upon PropertyChanged
which will fire once eg. PropertyFirstChanged
then you will avoid additional code invocation.
Upvotes: 2
Reputation: 2032
You code seems correct to me, is there a specific question here other then if your code is correct?
Mind you, it will show the popup for EVERY instance of the MyWorkspaceAddIn. If you want the popup to show only once for the application life you should call a static variable in a different class.
Upvotes: 0