Add "Idea Category" To Idea Form

jacobspacek
Giga Guru

There is a field within the idea portal called "Idea Category".

 

I have a request to add the "Idea Category" field to the idea form "idea.do".

 

I need the value in the form to be equal to the selected "Idea Category" chosen during the idea creation within the portal.

 

Field "Idea Category" exists within the "im_m2m_idea_category" table.

 

Failed to accomplish this request using form layout, form design or form builder.

 

Any attempts to build many to many relationships or reference qualifiers have also ended in failure.

 

What is the correct way to accomplish this request?

 

Why is "Idea Category" not available as a dot walkable field within configure idea form?

 

Attempted So Far

- Create many to many relationship between table "im_m2m_idea_category" and table "idea"... add new value as reference value to form (this fails to actually fill the value with the selected category but does allow the user to select a new one).

 

idea_portal.PNG

 

1 ACCEPTED SOLUTION

Try this approach:

 

  1. Create a UI Macro named "idea_categories"
    <?xml version="1.0" encoding="utf-8" ?>
    <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    	<!-- Get the categories -->
        <g2:evaluate var="jvar_categories" object="true">
    		var imIdeaCategory = new global.IMIdeaCategory();
    		var categories = imIdeaCategory.getCategoriesForIdea(current.getUniqueValue());
    		var categoryList = [];
    		if (Array.isArray(categories)) {
    			categories.forEach(function(category) {
    				if (category.hasOwnProperty('name')) {
    					categoryList.push(category.name);
    				}
    			});
    		}
    		categoryList;
        </g2:evaluate>
        <!-- Display the categories -->
    	<div class="idea-categories clearfix">
    		<div class="label_spacing">
    			<label dir="ltr" class="control-label col-xs-12 col-md-1_5 col-lg-2  col-xs-12 col-md-1_5 col-lg-2">
    				<span class="category-title">Categories</span>
    			</label>
    		</div>
    		<div class="col-xs-10 col-md-9 col-lg-8">
    			<j2:forEach items="$[jvar_categories]" var="jvar_category">
    			<span class="category-badge">$[jvar_category]</span>
    			</j2:forEach>
    		</div>
    	</div>
        <!-- Styles for the category buttons -->
        <style>
            .idea-categories {
    			margin: 0 0 10px;
            }
            .category-badge{
    			color: #2e2e2e;
    			background-color: #f5f5f5;
    			padding: 5px 10px;
    			display: inline-block;
    			border: 1px solid #ddd;
    			border-radius: 4px;
    			margin-right: 5px;
    			margin-bottom: 10px;
            }
        </style>
    </j:jelly>

     

  2. Create a UI Formatter (the value in "Formatter" must match the name of the UI Macro)SheldonSwift_0-1725561086348.png

     

  3. Add the "Categories" formatter to the Idea form (probably under "Title")

 

Good luck!

View solution in original post

10 REPLIES 10

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @jacobspacek - You might have to create a custom list field referencing im_category, then set its value after insert/update based on the matching record(s) in im_m2m_idea_category. I'm not sure there's a more straightforward way to go about it.

Thank you for such a quick reply!

 

"then set its value after insert/update based on the matching record(s) in im_m2m_idea_category"

 

If you would be so kind as to provide a little more detail on how to achieve this step I would sincerely appreciate it!

Is there a reason your users need to use the form view? Categories are already visible in the portal view. Also, you should also consider if users expect to modify categories from the form view, or if they'll be read-only outside of the Idea Portal.

 

Within the Idea Portal, it is important to note that the system first creates the idea, then iterates through the selected categories to generate a corresponding im_m2m_idea_category record for each one. Ideally, you would loop through the im_m2m_idea_category records only after that process is complete. However, since this logic resides within the script include "IMCreateEditIdeaDataServiceSNC" (which is read-only), modifying that routine isn't possible.

 

Given this limitation, the best approach might be to create a business rule on the im_m2m_idea_category table. Here’s an example of what the script might look like:

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var idea = current.getValue('idea');

    if (!idea)
        return;

    var ideaGr = new GlideRecord('idea');
    if (ideaGr.get(idea)) {

        var imIdeaCategory = new IMIdeaCategory();
        var categories = imIdeaCategory.getCategoriesForIdea(idea);

        var categoryIds = [];

        if (Array.isArray(categories)) {
            for (var i = 0; i < categories.length; i++) {
                if (categories[i].hasOwnProperty('id')) {
                    categoryIds.push(categories[i].id);
                }
            }

            ideaGr.setValue('u_category', categoryIds.join(','));
			ideaGr.update();
        } else {
            // Do something
        }
    }
})(current, previous);

 

 

I hope this helps, but I would be remiss if I didn’t suggest that you carefully consider the rationale for this request. With this design, you could end up updating the new idea up to five times—once for each category selected.

Good Morning,

 

Thank you for such a detailed response!

 

The request was made for 2 main reasons.

 

1) Grant internal users ability to quickly modify the selected "Idea Category" for an idea record within the Idea table list view. Portal users will frequently submit an idea with an incorrect "Idea Category" selection.

 

2) When internal users are looking at an idea record within the list view on the table within the platform the selected "Idea Category" value is not shown. In order to identify the "Idea Category" associated with the idea record users must manually look up the idea within the idea portal.

 

Thank you so much for the example business rule that could be applied on the "im_m2m_idea_category" table.

 

I also share your concerns... 

 

"With this design, you could end up updating the new idea up to five times—once for each category selected."

 

With this in mind, if the goal was changed to simply display the selected "Idea Category" on the form (as opposed to allowing the user view / modify) ... would this simplify the overall design?

 

Simply displaying a static "Idea Category" value on the form (which users would be unable to modify on the form) would satisfy most of the business requirements.