john_roberts
Mega Guru

I haven't had my hands in code for a while so I was looking for a reason to have a good ol' fashion geeky relapse. This past week one of my favorite customers (ok, so they're all my favorite) gave me that opportunity. They asked about using the workflow field for a use that didn't have anything to do with an automated workflow, a custom data driven workflow field.
They wanted to display the standard workflow stage widget but have the stages custom built from data related to each record. I didn't know off the top of my head so I did a little digging and came up with the following.
find_real_file.png


Here's my sample use case.
As a project manager…. (yeah, I've been a PO for almost a year now so I actually start all my sentences in this form)
I would like to display a summary of who the project reviewers are for a given project and what the state of their review is (requested, completed, not needed). Each project could have unique reviewers and reviewer states. This information should be visible in the project list view.

Solution:
We could solve this a number of ways including possibly using a real workflow and approvals, but in order to show you how generic this could be I avoided both of those. Plus, the customer already has the related data populated so they'd be able to just start displaying it instead of having to migrate data to a new structure.

What's needed:
Data - let's start by creating the table that will be used to drive the workflow widget values
* Table name: u_project_review
* Field: u_project, reference to pm_project
* Field: u_reviewer, reference to sys_user
* Field: u_state, choice (drop down w/none)
* Add choices to u_state (Requested, Completed, Not Needed)

New workflow field on Project (pm_project) table
* Field: u_review_status, type=workflow, choice=drop down w/none, set to read only since it's managed dynamically
* Add this field to the project list view

Logic
Now we need a way to drive the workflow field list widget from the related project reviewer data.
* Create New Business Rule
* Name (must be precise based on table and field names) = pm_project_u_review_statusGetChoices
* Table: global
* Active: true
* Script:



//build a dynamic workflow field choice list based on related data

//this function will be called by the workflow field during rendering
function pm_project_u_review_statusGetChoices() {
//get related project review records for this project (current)
var gr = new GlideRecord("u_project_review");
gr.addQuery("u_project", current.getUniqueValue());
//probably want to set an order that makes sense to you, we'll use reviewer
gr.orderBy("u_reviewer");
gr.query();

if (!gr.hasNext()) {
//no reviewers show let it be known
answer.add("not_needed", gs.getMessage("No reviewers needed")).image = "images/workflow_complete.gifx";
}
while (gr.next()) {
//set default icon to pending
var icon = "images/workflow_pending.gifx";
var text = "Requested";
//set other image based on state value
var stateValue = gr.getValue("u_state");
if (stateValue == "Completed") {
icon = "images/workflow_complete.gifx";
text = "Complete";
}
else if (stateValue == "Not Needed") {
icon = "images/workflow_skipped.gifx";
text = "Not Needed";
}
//build text for choice label
var display = gr.getDisplayValue("u_reviewer") + " (" + text + ")";
//add the choice to the list, and set the icon
answer.add(gr.getUniqueValue(), display).image = icon;
}
}


Reviewer Data
* Add the project review related list to the project form
* Go ahead and create some project reviewer records with valid project, reviewer, and state values.


And we're done
Checkout the project list now that we have values
find_real_file.png
Other thoughts….
The field value of u_review_status doesn't really make sense at all since it's a collection of data, for that reason I would leave the field off the form and not use it for reporting (it doesn't really have a value stored in the record at this point). I'm sure there's plenty of clever folks out there that could take this to the next level. My $0.02, I'd have the updating of project review records set the actual value of the review status field on project, this would be a summary of all reviewer states. This value would be completely independent from the rendering of the workflow widget.

9 Comments
Slava Savitsky
Giga Sage

Hi John,
Thanks for sharing this cool stuff. The only thing that remains unclear is how this function is actually invoked. You only defined a function without explicitly calling it in any other script or page? Could you please give me some insight into what happens in the background? Thank you.


john_roberts
Mega Guru

Ah, that's the fun part. The magic is in the name of the function. For choice fields, we attempt to call a function based on the name of the table and field. So nothing needed to build the connection, other than getting the name exact.


JayaBharathi
Kilo Contributor

Thanks for Sharing

Hi John,

Is the stage field defined on sc_request table the same way?? If so, I am unable to find the associated global function definition. It would be great, if you help me on this.

Thanks,
Jaya Bharathi Tallada.


john_roberts
Mega Guru

Jaya, I believe the default stages for sc_request are stored as standard choice options on the stage field.


JayaBharathi
Kilo Contributor

Yes John, Stages are stored as standard choices, but i wanted to know the function which displays the widget(Sets the images, Messages beside the stages).

Thanks in advance.

Thanks,
Jaya Bharathi Tallada


john_roberts
Mega Guru

Icons and status text are mostly handled in the WorkflowIconsSCR and WorkflowIconsStages script includes.


j-mo
Tera Contributor

Is anyone having issues with this in Fuji?   The stage icons are no longer showing since upgrading from Eureka.   It looks like the following:



icons.png



Thoughts?


DrewW
Mega Sage

The icons are actually a font if I understand correctly.   Have you flushed your browser cache and had the page refresh?


j-mo
Tera Contributor

Hi Drew,



Thank you for the response.   When I started viewing the source code it appears as if fuji is replacing the workflow image urls, ex:


"images/workflow_pending.gifx" would be replaced automatically with "images/heisenberg_icons/stage_icons/images/workflow_pending.gifx" behind the scenes.   As you can see, it takes the image URL I provided and slaps the heisenberg url in front.   To remedy this, I had to adapt to the new FUJI workflow icons and deal with the fact that SN automatically adds the images/heiesenberg_icons/stage_icons portion.   My new code looks like this:


var icon = "icon-empty-circle";


icon = "icon-check-circle";


icon = "icon-arrow-right";



This appears to be working for now.