Need to show 'Add' button only for few states.

archie5
Tera Expert

Need to show 'Add' button only for few states. Currently this Add button on Affected CIs is visible when state is draft, I need to show this for few other states.

This is the condition mentioned in the UI action - RP.isManyToMany() && (new ChangeProposed(parent)).canAddCI() && current.canCreate() && parent.active == true && gs.getProperty('com.snc.task.associate_ci').indexOf(parent.sys_class_name) > -1

I tried adding current.state==-4 etc but that didn't work. Am I missing something?

find_real_file.png

1 ACCEPTED SOLUTION

Most out of the box scripts are marked as SNC at the end of them, and typically there will be a non-SNC version that allows you to extend that script, and "add" to it, or "overwrite" existing functions/variables.

Since you all have altered the OOB Change States, I'm assuming you've also had to alter the State Handler script (ChangeRequestStateHandler which is the extended version of ChangeRequestStateHandlerSNC) in order to allow for the Change to move from State to State.

So I think the correct way to handle this would be to add the new States to the ChangeRequestStateHandler Script Include.

Then create new functions for those states in the ChangeRequest script include that are modeled after the existing isSTATE() functions in the ChangeRequestSNC script include.

Then you can use those functions in the ChangeProposed script include if statement condition.

 

ORRRRR....

I think there may be a quick and dirty way to do it. BUT, it comes with a disclaimer. I'll have to say I don't recommend this, and I don't know if it will break any other functionality. I'll let you decide.

Instead of using the other APIs to check the state, use the GlideRecord object in the ChangeProposed script (this._grChgReq). This way you could input the states as you like with their back-end values. I don't know the back-end values for all of your custom states, but they should be numeric. Here's an example, -5 is the backend value for New and -2 is Scheduled. Add to this:

canAddCI: function() {
		// If it's not a change task, return true and let other conditions decide
		// This has to be done as the Add CI action could be used on all types of tasks
		if (!this._isChangeTask)
			return true;
		
		// If it's a change in the new state, return allow adding of CIs

// 2019-10-30 -- Customizing to allow additonal Change states to add CIs
		if (this._grChgReq.state == '-5' || this._grChgReq.state == '-2'))
			return true;
		
		if (this._log.atLevel(GSLog.DEBUG))
			this._log.debug("[canAddCI] Cannot add CIs to Change Request " + this._grChgReq.number + ":" + this._grChgReq.getUniqueValue());
		
		// In all other cases, don't allow addition of CIs
		return false;
	},

 

If my answer helped you, or solved your issue, please help me out by marking it as Helpful or Correct!

Thanks,

Josh

View solution in original post

17 REPLIES 17

Karthik Now
Kilo Contributor
Access parent form elements from the related list using "parent"
try using
parent.state==-4 ​

johnfeist
Mega Sage
Mega Sage

Hi Archie,

From what you are showing, I assume that you are looking at the Change Request table.  OOTB there is no draft state for Change Request.  There is also no OOTB draft state with a value of 4.  Did you add that state to change?  OOTB the only state(s) for Change Request that have a value of 4 are a couple of closed states.  Bottom line, make sure that you have a good value, especially if you created that state, make sure the values don't conflict.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Josh Virelli
Tera Guru

Hey Archie,

It's a little more complex unfortunately. So, in this UI action, current actually refers to the table the UI Action is on. Which is the [task_ci] table. Parent would be the table you are adding it to, i.e. Change Request in this case. I would be careful here, because that UI Action will affect all Task tables, not just Change.

Change states and the logic surrounding them are handled by a bunch of Script Includes. In that Condition line you'll notice this portion:

(new ChangeProposed(parent)).canAddCI() 

ChangeProposed is an Out of the Box script Include, and canAddCI() is a function in there that determines if a CI can be added to the task. Typically I would recommend against customizing OOB scripts, but you may have to in this case.

Here's what that function looks like in the ChangeProposed Script Include

	canAddCI: function() {
		// If it's not a change task, return true and let other conditions decide
		// This has to be done as the Add CI action could be used on all types of tasks
		if (!this._isChangeTask)
			return true;
		
		// If it's a change in the new state, return allow adding of CIs

		if (this._changeRequest.isNew())
			return true;
		
		if (this._log.atLevel(GSLog.DEBUG))
			this._log.debug("[canAddCI] Cannot add CIs to Change Request " + this._grChgReq.number + ":" + this._grChgReq.getUniqueValue());
		
		// In all other cases, don't allow addition of CIs
		return false;
	},

Basically, if the Task is not a change, return true. If it is a change, it will only return true if it is a new Change. This calls another script include to check the states. That script include is ChangeRequestSNC. Thankfully ServiceNow built in functions to check for all the states, not just new.

So I think we can achieve what you are looking to do, by adding in additional conditions to the canAddCI function.

I recommend you add comments, so that on upgrades you will remember why it's customized. You should be able to add an state by adding an "|| this._changeRequest.isSTATE" to that condition line. Like this:

	canAddCI: function() {
		// If it's not a change task, return true and let other conditions decide
		// This has to be done as the Add CI action could be used on all types of tasks
		if (!this._isChangeTask)
			return true;
		
		// If it's a change in the new state, return allow adding of CIs

// 2019-10-30 -- Customizing to allow additonal Change states to add CIs
		if (this._changeRequest.isNew() || this._changeRequest.isAssess())
			return true;
		
		if (this._log.atLevel(GSLog.DEBUG))
			this._log.debug("[canAddCI] Cannot add CIs to Change Request " + this._grChgReq.number + ":" + this._grChgReq.getUniqueValue());
		
		// In all other cases, don't allow addition of CIs
		return false;
	},

Let me know if that works for you. I tested in a Personal Dev and it appears to be good to go.

If my answer helped you, or solved your issue, please help me out by marking it as Helpful or Correct!

Thanks,

Josh

Thank you sooooo much for this solution..

However, the script include 'ChangeRequestSNC' is read-only in my instance.

find_real_file.png