In today’s post, I’ll be outlining a procedure to add an Address field to an Account Asset (Product) record.
Goal/Description:
To add an address field to Account assets (products) that allows a user to define an existing Account Address for an Asset (AccountProduct) record. The addressed will be stored in the AccountProduct.Userfield1 field.
- Detail/Steps:
- Make Userfield1 available for Account Products
- In Application Architect, right click the AccountProduct entity and select UpdateProperties. If it is not already selected, select the Userfield1 field to include the property on the entity. If Userfield1 is already selected, skip to b.
- Double click the AccountProduct entity to get the Properties list. Find the Userfield 1 property in the list and make sure the “Include” checkbox is checked.
- Save changes to the entity, and build interfaces.
- Add a new Lookup control to the AddEditAccountProduct quickform.
- On wizard set the following:
- What information will you be looking up?
- Address
- Which property will receive the result of this lookup?
- Userfield1
- Select how the lookup control will display
- Dialog
- Allow lookup selection to be cleared
- Check if you want users to be able to clear and address for the asset.
- Select properties that you want to see in the result grid.
- Pick which ever properties you want in the lookup. I’d suggest Address1, city, state, postalcode. The lookup will default to search the first item in the list.
- Click OK.
- What information will you be looking up?
- Verify/Set additional properties:
- Caption:
- Whatever you would like. “Address” would be appropriate.
- Initialize lookup
- True – This will fill the lookup upon opening (you don’t have to hit “Search”)
- Return Primary Key
- True (Should be this by default. This will cause the addressid to be stored)
- Lookup Binding Mode
- “String” (Should be this by default)
- Lookup Properties
- You can change the order of the fields contained in the lookup. The search on the lookup will default to the first item in the list. There has to be one lookup field, but you can set the ExcludeFromFilter property = False for all other Fields in case the field you want to use is not the first in the list.
- Seed Property
- “EntityId” (Case sensitive. This will allow us to restrict the lookup result to the AccountID.)
- OnChangeAction
- Add a C# Snippet action with no code. This will force a post-back.
- Caption:
- Set Onload Event for Form
- Create a new C# Snippet Action item
- On Repaint Event = True
- CSharp Code (See below)
- Create a new C# Snippet Action item
- On wizard set the following:
- Make Userfield1 available for Account Products
CSharp Code:
The code we add is needed to do the following:
- Set the Seed value of the control, to restrict the addresses returned by accountid
- Set The display of the Address control to show whichever field you want to see on the form. (This can be skipped, but the control will then show the String Expression defined on the Saleslogix Extended tab of the Entity page. By default, this is the Address Description.
Code Sample:
//Create a entity object for AccountProduct Sage.Entity.Interfaces.IAccountProduct acp = this.BindingSource.Current as Sage.Entity.Interfaces.IAccountProduct; if (acp != null && acp.Account != null) //Check to see if objects are null, otherwise we'll get a null reference exception error when adding a new asset. { //Set seed value to accountid lueAddress.SeedValue = acp.Account.Id.ToString(); } //If you are ok displaying the value from the String Expression defined under the entities SalesLogix extended tab, the rest of this code can be skipped. //Create empty variable to hold display value string addrValue = string.Empty; //Check to see if the address lookup is blank. if (lueAddress.LookupResultValue != null) { //Get the addressID for the address currenly defined for the asset (if any). string sAddressID = lueAddress.LookupResultValue.ToString(); //Use the addressid to create another entity object for the address entity. Sage.Entity.Interfaces.IAddress addr = Sage.Platform.EntityFactory.GetById<Sage.Entity.Interfaces.IAddress>(sAddressID) as Sage.Entity.Interfaces.IAddress; //Set the variable to disired display field (Address1 in this example). if (addr != null) { addrValue = addr.Address1; } } //The lookup control is a composite control, so we need to loop through the base control types to find the correct property on which to set the display value. foreach (Control c in lueAddress.Controls) { if (c.ClientID.EndsWith("_LookupText")) { //Set the display to show the field defined above (currently Address1) ((TextBox)c).Text = addrValue; } }
Once the Changes have been completed, save the quickform, Build the Web platform, and re-deploy the Web portal.