Community
Showing results for 
Search instead for 
Do you mean 
Reply

Field update events lost after company update

New Member
Posts: 7
Country: Canada

Field update events lost after company update

I have added some custom fields to companies and I want to do some calculations when any one of a number of fields change.  The calculation will update another field.  So I have field A and B and when either changes, I want to update field C.

 

 I get a FieldChanged event whenever the user types in one of the fields. If it's field A or B then I change the value of field C.  But, Of course, I don't see the change in field C.  To get that, I need to update the Company.  Then whenever I change A or B, I see the new value in field C.  However, as soon as I update the company, I no longer get any FieldChanged messages.  The result is that the first update to A or B takes effect but no changes to A or B after that apply.

 

I am setting the handler for the FieldChanged event on a CompanyChanged event.  So of course if I click to another company and click back, I start to receive field changed events again until either A or B is changed.

 

How can I capture field changed events and update fields without constantly having to change companies?

Employee
Posts: 1,163
Country: USA

Re: Field update events lost after company update

A couple of thoughts come to mind. First, I wonder if instead of updating the company, we can simply refresh the view so the data will show up. I'm actually not super confident with this, I think the data may still be missing because it may not have been committed at that point.

 

My second thought was to register for the FieldChanged event in the ViewLoaded event, so you could update the company and then also refresh the view so you'll be monitoring the fields for changes again.

Matthew Wood
Act! SDK Support
Community Moderator
New Member
Posts: 7
Country: Canada

Re: Field update events lost after company update

On your first suggestion, you're right. Just refreshing the view doesn't do anything since the data isn't commtted yet.

 

On your second suggestion, ViewLoaded is not invoked after doing a application.CurrentView.RefreshView();   SO that one doesn't work either.

 

Any other thoughts?

 

One idea I had was to put the field containing the result on another tab and try to force the update on changing the tab but I can't find a TabChanging event.  

 

 

Employee
Posts: 1,163
Country: USA

Re: Field update events lost after company update

I re-registered for the FieldChanged event within the CompanyUpdated event and it work every time for me after that. If I made numerous field changes to these two fields without moving between companies I'd eventually get an OutOfMemoryException, but I think that's because I wasn't un-registering from the events appropraitely.

Matthew Wood
Act! SDK Support
Community Moderator
New Member
Posts: 7
Country: Canada

Re: Field update events lost after company update

Hmm. It still doesn't seem to work for me. Here's some code snippets

 

 

protected void Application_CurrentCompanyChanging(object sender, EventArgs e)
{
Company company = application.ApplicationState.CurrentCompany;

// dettach from contact events
company.Fields.FieldChanged -= FieldCollection_FieldChanged;
company.Updated -= Company_Updated;
}

 

 

protected void Application_CurrentCompanyChanged(object sender, EventArgs e)
{
Company company = application.ApplicationState.CurrentCompany;

// attach to contact events
company.Fields.FieldChanged += FieldCollection_FieldChanged;

company.Updated += Company_Updated;
}

 

 

protected void Company_Updated()
{
Company company = application.ApplicationState.CurrentCompany;
company.Fields.FieldChanged += FieldCollection_FieldChanged;
}

 

 

protected void FieldCollection_FieldChanged(string fieldName)
{
if (fieldName.StartsWith("TBL_COMPANY.CUST_NMSE_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSF_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSEPACK_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSFPACK_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_EDITVER_"))
{
Company company = application.ApplicationState.CurrentCompany;
 object f = company.CompanyFields[fieldName, true];
 company.CompanyFields["Company.Key", false] = "Key Changed: " +

company.CompanyFields["Company.NMSE", false] + "-" +

company.CompanyFields["Company.NMSF", false];
company.Update();
 }

}

 

 

Can you see what's wrong?  I'm getting

1. the first field changed event for field "NMSE"

2. a field changed event for field "Key"

3. An CompanyUpdated event

4. Another companyUpdated event

and then I get no more field enets until I change companies.

 

Your help is appreacited!!!!!

 

 

Employee
Posts: 1,163
Country: USA

Re: Field update events lost after company update

The only major difference I see initially is how we're setting the value for fields:

 

Company myComp = ActApp.ApplicationState.CurrentCompany;
myComp.Updated += new MutableEntity.UpdatedEventHandler(myComp_Updated);
DBFieldDescriptor myField1 = ActApp.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.DESCRIPTION");
DBFieldDescriptor myField2 = ActApp.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.NAME");
DBFieldDescriptor myField3 = ActApp.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.SHIPPING_LINE1");
string val1 = myField1.GetValue(ActApp.ApplicationState.CurrentCompany).ToString();
myField3.SetValue(ActApp.ApplicationState.CurrentCompany, val1 + val2);
 
ActApp.ApplicationState.CurrentCompany.Update();  
Matthew Wood
Act! SDK Support
Community Moderator
New Member
Posts: 7
Country: Canada

Re: Field update events lost after company update

That'still not it. I changed the way I way calculating the field to be like yours and that made no difference.  I also put the addition of the Update handler into the field changed code like you did and still it made no difference.  Once I update the company I never get field changed evets again until I change company.

 

My field changed event now looks like:

 

protected void FieldCollection_FieldChanged(string fieldName)
{
if (fieldName.StartsWith("TBL_COMPANY.CUST_NMSE_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSF_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSEPACK_") ||
   fieldName.StartsWith("TBL_COMPANY.CUST_NMSFPACK_") ||
  fieldName.StartsWith("TBL_COMPANY.CUST_EDITVER_"))
{
Company company = application.ApplicationState.CurrentCompany;
company.Updated += Company_Updated;
DBFieldDescriptor NMSE_Field = application.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.CUST_NMSE_121037192");
DBFieldDescriptor NMSF_Field = application.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.CUST_NMSF_121045301");
DBFieldDescriptor Key_Field = application.ActFramework.Companies.GetCompanyFieldDescriptor("TBL_COMPANY.CUST_Key_104714563");
String NMSE_val = NMSE_Field.GetValue(application.ApplicationState.CurrentCompany).ToString();
String NMSF_val = NMSF_Field.GetValue(application.ApplicationState.CurrentCompany).ToString();
Key_Field.SetValue(application.ApplicationState.CurrentCompany, "Key= " + NMSE_val + ", " + NMSF_val);

company.Update();
}

}

 

It doesn't seem likely that the fact that I'm using custom fields would make a difference.  Nor does the fact that I'm filtering the field updates and only processing the ones I want, whereas you are processing even for the field that contains the result.  

 

Other ideas?  Could you send me your example project along with the compiled plug-in so I can verify it works on my system? (Trying to eliminate everything here!)

 

Thanks

MBH
New Member
Posts: 1
Country: United_Kingdom

Re: Field update events lost after company update

Hi Les

 

Did you get anywhere with this. I'm trying to do something similar but just keep pulling my hair out.

 

I'd appreciate some direction toowards any type of solution.

 

Thanks