12-17-2015 02:26 AM
Hi all
Can any one tell me why this code will not work,l i get an error saying that Act has had an error please close the program
I have tested the line by line and it appears it the following lines when the // are removed
//object ob1 = mefd.GetValue(cOpp); //string str1 = mefd.GetValue(cOpp).ToString();
The code it launched when the opportunity detailed view is loaded and i am trying to get the value of User7, everything else appears to work even when i set "str1" manually
if (viewName == "Act.UI.IOpportunityDetailView") { // Function to return the name of current layout // Act.UI.LayoutDesigner.LayoutFileInfo InfoLayout1; // InfoLayout1 = oApp.UILayoutDesignerManager.GetCurrentLayout(Act.UI.LayoutDesigner.LayoutType.opportunity); // Get Current Opportunity Opportunity cOpp = oApp.ApplicationState.CurrentOpportunity; MutableEntityFieldDescriptor mefd = oApp.ActFramework.Opportunities.GetMutableEntityFieldDescriptor(StandardOpportunityField.User7); //object ob1 = mefd.GetValue(cOpp); //string str1 = mefd.GetValue(cOpp).ToString(); string str1 = "Investment"; MessageBox.Show("" + str1 + "\n\n" + mefd + "", oApp.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); if (str1 == "Investment") {
12-17-2015 03:23 PM
Based on your code it would be something like:
oApp.CurrentOpportunityChanged -= oApp_CurrentOpportunityChanged;
at the beginning of the routine and:
oAppCurrentOpportunityChanged += oApp_CurrentOpportunityChanged;
just before the end of the routine. You need to make sure that the second one runs if there is a problem with your routine so you would want to put a try...catch...finally in the routine.
Basically the try says "try this" and "catch" any error messages and "finally" do this. So the "finally" will always execute adding your event handler back in.
Something like this:
12-17-2015 05:42 AM
Did you check the Act Log viewer to find out why Act crashed?
I'd also check to make sure mefd is not null before calling any methods on it, and also "mefd.GetValue(cOpp)" can return a null value so make sure you check for that before calling the .ToString() method. I don't believe this would cause Act to crash but your plugin will run into issues if the value of the field is blank.
You can do something like the following
string str1 = "";
object oStr1 = mefd.GetValue(cOpp);
if (oStr1 != null)
str1 = oStr1.ToString();
12-17-2015 12:12 PM
Thanks for your reply
In previous version of the code i had Msg lines and one is still there, mefd comes back as "Opportunity Field 7", as for the log i would not know what i am looking at, i will put in the stuff you have recommended and see what happens
Thanks again for your reply
12-17-2015 12:31 PM
ok updated and it still produces the error and request shutdown of Act
object oStr1 = mefd.GetValue(cOpp); if (oStr1 != null) str1 = oStr1.ToString(); MessageBox.Show("" + str1 + "\n\n" + mefd + "", oApp.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
It certainly this part in getting the value of USER7 as the Message came back with only "mefd" and no "str1"
And when i changed this line from
string str1 = "";
to this
string str1 = "Investment";
Which is one of the options found in USER7, It worked fine with no error and performed the actions required
12-17-2015 12:45 PM
12-17-2015 12:54 PM
What event are you using to run the above code? Is it ActApplication.ViewLoaded ?
12-17-2015 12:59 PM
12-17-2015 01:01 PM
Nope still looping ...AAAARRRR
12-17-2015 01:11 PM
Ok change it and added a bit of loop breaker etc, its not sexy but appears to work
void oApp_CurrentOpportunityChanged(object sender, EventArgs e) { string viewName = oApp.CurrentViewName; // Returns // Act.UI.IOpportunityDetailView // Act.UI.IOpportunityListView // Act.UI.IOpportunityView // Act.UI.ICompanyDetailView // Act.UI.ICompanyListView // Act.UI.ICompanyView // Act.UI.IContactDetailView // Act.UI.IContactListView // Act.UI.IContactView // Act.UI.IGroupDetailView // Act.UI.IGroupListView // Act.UI.IGroupView if (viewName == "Act.UI.IOpportunityDetailView") { // Function to return the name of current layout Act.UI.LayoutDesigner.LayoutFileInfo InfoLayout1; InfoLayout1 = oApp.UILayoutDesignerManager.GetCurrentLayout(Act.UI.LayoutDesigner.LayoutType.opportunity); string cLayout = InfoLayout1.ToString(); string lstr1 = ""; if (cLayout == "Basic Opportunity Layout - 1024x768.oly") lstr1 = "Default"; if (cLayout == "JDFP Opportunity Layout 1.oly") lstr1 = "Investment"; if (cLayout == "JDFP Opportunity Layout 2.oly") lstr1 = "Superannuation"; if (cLayout == "JDFP Opportunity Layout 3.oly") lstr1 = "Insurance"; string str1 = ""; // Get Current Opportunity Opportunity cOpp = oApp.ApplicationState.CurrentOpportunity; MutableEntityFieldDescriptor mefd = oApp.ActFramework.Opportunities.GetMutableEntityFieldDescriptor(StandardOpportunityField.User7); object oStr1 = mefd.GetValue(cOpp); if (oStr1 != null) str1 = oStr1.ToString(); MessageBox.Show("" + str1 + "\n\n" + mefd + "", oApp.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); if (lstr1 != str1) { if (str1 == "Investment") { // Creates and obtains list of all current Opportunity layouts available Act.UI.LayoutDesigner.LayoutFileInfo[] InfoLayout2; InfoLayout2 = oApp.UILayoutDesignerManager.GetAvailableLayouts(Act.UI.LayoutDesigner.LayoutType.opportunity); // Returns // InfoLayout 0 Returns "Basic Opportunity Layout - 1024x768.oly" - Default // InfoLayout 1 Returns "Basic Opportunity Layout - 1366x768.oly" // InfoLayout 2 Returns "Basic Opportunity Layout - 1920x1080.oly" // InfoLayout 3 Returns "Basic Opportunity Layout - 800x600.oly" // InfoLayout 4 Returns "JDFP Opportunity Layout 1.oly" - Investment // InfoLayout 5 Returns "JDFP Opportunity Layout 2.oly" - Superannuation // InfoLayout 6 Returns "JDFP Opportunity Layout 3.oly" - Insurance oApp.UILayoutDesignerManager.SetCurrentLayout(InfoLayout2[4]); } else { if (str1 == "Superannuation") { Act.UI.LayoutDesigner.LayoutFileInfo[] InfoLayout2; InfoLayout2 = oApp.UILayoutDesignerManager.GetAvailableLayouts(Act.UI.LayoutDesigner.LayoutType.opportunity); oApp.UILayoutDesignerManager.SetCurrentLayout(InfoLayout2[5]); } else { if (str1 == "Insurance") { Act.UI.LayoutDesigner.LayoutFileInfo[] InfoLayout2; InfoLayout2 = oApp.UILayoutDesignerManager.GetAvailableLayouts(Act.UI.LayoutDesigner.LayoutType.opportunity); oApp.UILayoutDesignerManager.SetCurrentLayout(InfoLayout2[6]); } else { Act.UI.LayoutDesigner.LayoutFileInfo[] InfoLayout2; InfoLayout2 = oApp.UILayoutDesignerManager.GetAvailableLayouts(Act.UI.LayoutDesigner.LayoutType.opportunity); oApp.UILayoutDesignerManager.SetCurrentLayout(InfoLayout2[0]); } // End Insurance } // End Superannuation } // End Investment } else { //Nothing } // End of Opportunity View } }
12-17-2015 02:10 PM - edited 12-17-2015 03:25 PM
Usually when you go into an event handler the first thing you want to do is unsubscribe the event handler you are in from the event. Then when you exit your event handler you subscribe your event handler back to the event. That way your event handler doesn't get executed multiple times when an event gets fired multiple times while you are in it.
Stan