API Questions on Kentico API.
Version 6.x > API > Document Type using ID instead of name View modes: 
User avatar
Member
Member
JasonT - 8/21/2012 11:43:49 AM
   
Document Type using ID instead of name
Hi all, I'm having a bit of an issue with the name of a document type I have created. It keeps using the ID of a drop-down control instead of the value. Here's what I have set up:

2 custom tables - one named Addresses (w/fields ItemID, AddressTitle, Address1, Address2), the other named PhoneNumbers (ItemID, Phone, PhoneType)
- Under the Search fields tab, AddressTitle and Phone are set as the Title field, respectively

1 Document Type named Address (w/fields AddressList, PhonesList) that I'm planning on using to associate several phone numbers with an address
- AddressList uses "SELECT ItemID, AddressTitle FROM customtable_Addresses ORDER BY AddressTitle ASC" as its data source
- PhonesList uses "SELECT ItemID, Phone FROM customtable_PhoneNumbers ORDER BY Phone ASC" as its data source

When I create a new Address in a folder it shows the address and phone information correctly. So I select an address from the drop-down and select the phone numbers associated with that address from the phones list-box.

However, when I hit Save it creates the new Address using the ItemID from the Address drop-down as its name rather than the AddressTitle. I have selected AddressList in the Document name source field, but am not sure how to set AddressTitle as the name of the new Address.

Ideas?

P.S. - Looking forward a little towards the goal of this, I'm planning on using this Address in another document type named Services.

What I'm thinking is that the user will select a drop-down of the address they want to associate w/a particular service. That selection will then (somehow) populate another drop-down with all of the phone numbers that have been associated with that address, allowing the user to pick the correct phone numbers for that particular service.

So my other question here is, is this the proper way of setting this up in Kentico or is there a more efficient way?

Thanks!

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/22/2012 2:19:57 AM
   
RE:Document Type using ID instead of name
Hello,


1. It is not easy to achieve because Kentico uses the value of the drop-down list and does not look up the correct text in the field settings. There are two possibilities:

a. you will use "SELECT AddressTitle, AddressTitle FROM customtable_Addresses ORDER BY AddressTitle ASC"

so the text and value will be the same.

b. you will use custom handler and change the document name before inserting. You can find the AddressTitle according to the ItemID which will be the current document name and you will change the name before inserting.


2. I would recommend to develop a custom form control for this (Developing form controls). You can place the drop-down lists into one control and change their visibility and content when the selected index of one of them is changed.


Best regards,
Helena Grulichova

User avatar
Member
Member
JasonT - 8/22/2012 9:46:40 AM
   
RE:Document Type using ID instead of name
Hi Helena, thanks for your reply.

kentico_helenag wrote: a. you will use "SELECT AddressTitle, AddressTitle FROM customtable_Addresses ORDER BY AddressTitle ASC"

so the text and value will be the same.


Yeah, I was afraid of that. I had done this earlier and it will unfortunately lead to an ambiguous column error, so one of the columns must be given an alias when using ORDER BY (I used the first one). This is kind of a clunky way to go about getting this to work properly. Can you put in a request for a better solution to this? Maybe have another drop-down under "Document name source field:" that allows you to set the column for the document name?

kentico_helenag wrote: b. you will use custom handler and change the document name before inserting. You can find the AddressTitle according to the ItemID which will be the current document name and you will change the name before inserting.


This is an interesting idea and certainly more elegant. I'd rather not fire a global event handler for just this one document type though.

kentico_helenag wrote: 2. I would recommend to develop a custom form control for this (Developing form controls). You can place the drop-down lists into one control and change their visibility and content when the selected index of one of them is changed.


Sounds good. I came across this in some other posts as well but wanted to see if there was another way before getting into it. I'll give this a go and get back to you.

Thanks again for your help!

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 8/23/2012 9:10:30 AM
   
RE:Document Type using ID instead of name
Hello,


You are welcome. You can suggest new features on kentico.uservoide.com - please add your requirement there.


Best regards,
Helena Grulichova

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 12:47:50 PM
   
RE:Document Type using ID instead of name
SELECT AddressTitle as Value , AddressTitle as Name FROM customtable_Addresses ORDER BY AddressTitle ASC

Problem solved.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 12:50:32 PM
   
RE:Document Type using ID instead of name
Of course I would mess that up.

SELECT AddressTitle as Value , AddressTitle as Name FROM customtable_Addresses ORDER BY Name ASC.

I don't think that's clunky at all. If you really do think it's clunky, could you explain why?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 12:56:20 PM
   
RE:Document Type using ID instead of name
Just to explain why I don't think it's clunky: Behind the scenes the dropdown doesn't care what the column names are. It just uses the index to access them. Also, to give you confidence, I've used aliases in the form field queries like this for many many document types and it works great. Here is an example of making it so you have an empty choice

Select '' as Value, ' None' as Name
UNION
Select AddressTitle as Value, AddressTitle as Name from customtable_Addresses ORDER BY Name ASC

Just write a valid query that populates the dropdown list the way you need it and don't worry about the rest.