What's the best way to place a time constraint on a catalog item?

patricklatella
Mega Sage

I need to place a time constraint on a catalog item.   For example, the item is available upon approved request, but only for a 10 day period...what's the best way to apply this constraint?

1 ACCEPTED SOLUTION

You do not need a script include to make sure that the end date is not before the start date, only the lines I added - checked and verified in my instance.


If you have all the script I provided, you should be covered.


Please make sure you added the lines to the correct script.


Also, reload the catalog item page after adding and the lines and saving the script.


If it still does not work, please share your script with the lines included.



harel


View solution in original post

41 REPLIES 41

oharel
Kilo Sage

Hi Patrick,



I'd run a daily scheduled BR to check what's going on from the moment of approval.


The question is - what are you going to check? what happens after 10 days? does it matter if users have been working on the item for 10 days from the moment of approval until the 10 days have passed?


For example, if the request has been in state "approved" for 10 days and not updated   - you can have something like this:


function checkState() {


  var encQuery = 'active=true^sys_updated_onRELATIVELE@dayofweek@ago@10^state=2';


  var gr = new GlideRecord('sc_req_item');


  gr.addEncodedQuery(encQuery);


  gr.query();



  while (gr.next()) {


  //do something


  }


}


Feel free to elaborate so we can fine tune it according to your needs.


thanks


harel


Please mark as correct or helpful based on impact


thanks Harel!


my question is kind of generic, but basically I've got a group of catalog items (let's call them "Item Group") that can only be requested for 10 day periods, at which time access needs to automatically expire.   Is the business rule you reference put on the workflow granting approval to the catalog item?   Or is the BR put on the catalog item itself?  



Or...could another approach be: I also need to make it so the user needs to identify the beginning and ending access dates for the catalog item that they are requesting...how would I add the "requested dates" field to the catalog item and how can I restrict it so that the item can only be requested for a maximum of 10 days.   Another question I've got is how to restrict the user from requesting more than 1 of these catalog items from my designated "Item Group" at a time?  


One by one:


Q: a group of catalog items (let's call them "Item Group") that can only be requested for 10 day periods:


Do you mean that they should appear for only 10 days? I am not getting you and it seems to be a major point here.



Q: Is the business rule you reference put on the workflow granting approval to the catalog item?   Or is the BR put on the catalog item itself?


It is on the requested item, running on sc_req_item table. As it is an example to give you an idea, I did not bother specifying everything.


You can also do it in a workflow: once the state is approved, add a 10 days timer and an if statement that checks the state of the requested item. If state is approved, do something; if it is not, do something else.



Q: how would I add the "requested dates" field to the catalog item and how can I restrict it so that the item can only be requested for a maximum of 10 days?


You can add fields to the catalog item. One called Today and the other "Expires on". You can set them both to read only and give them a default value: javascript:gs.now() and javascript:gs.daysAgo(-10), or if you need fixed values, that can also be arranged.


When you say expire - do you mean expire for a particular user from the moment s/he opened it, or to everyone? What happens when the item expires? does it automatically close? does it disappear from the list (becomes inactive)?



Q: Another question I've got is how to restrict the user from requesting more than 1 of these catalog items from my designated "Item Group" at a time?


That can be arranged using a script that checks who the current user is and whether there is also an open requested item for the user of the specific type. If there is none - proceed. If there is, not allow this one to open and give the number of the open one.


It all depends on what you want to do.



harel


SEE MY REPLY IN BOLD.  



One by one:


Q: a group of catalog items (let's call them "Item Group") that can only be requested for 10 day periods:


Do you mean that they should appear for only 10 days? I am not getting you and it seems to be a major point here.



What I mean is that the items can only be used in 10 day increments...say for example for a work vehicle.   I need to able to have the user request the vehicle for a maximum of 10 days, and for them to be required to designate the dates they want the vehicle when they submit the request.   But for them to be restricted to requesting the vehicle for a maximum of 10 days.   As well I need to make it so a user can only submit a request for 1 vehicle at a time within a given time frame...or in other words the requested dates on 2 different vehicles cannot overlap.   The catalog item for the vehicle can be available in the Service Catalog all the time to everyone.   The "Item Group" in this example would be a selection of work vehicles.



Q: Is the business rule you reference put on the workflow granting approval to the catalog item?   Or is the BR put on the catalog item itself?


It is on the requested item, running on sc_req_item table. As it is an example to give you an idea, I did not bother specifying everything.


You can also do it in a workflow: once the state is approved, add a 10 days timer and an if statement that checks the state of the requested item. If state is approved, do something; if it is not, do something else.



Q: how would I add the "requested dates" field to the catalog item and how can I restrict it so that the item can only be requested for a maximum of 10 days?


You can add fields to the catalog item. One called Today and the other "Expires on". You can set them both to read only and give them a default value: javascript:gs.now() and javascript:gs.daysAgo(-10), or if you need fixed values, that can also be arranged.


When you say expire - do you mean expire for a particular user from the moment s/he opened it, or to everyone? What happens when the item expires? does it automatically close? does it disappear from the list (becomes inactive)?



As mentioned above, I just mean that the item can only be requested for a maximum of 10 days at a time.   The item can remain available to all users all the time regardless of whether someone has requested the item.   i.e. multiple users can request the same item for the same (or overlapping) time frames, but they cannot request more than 1 item from the "Item Group" in a given time frame.   I think you address that below.



Q: Another question I've got is how to restrict the user from requesting more than 1 of these catalog items from my designated "Item Group" at a time?


That can be arranged using a script that checks who the current user is and whether there is also an open requested item for the user of the specific type. If there is none - proceed. If there is, not allow this one to open and give the number of the open one.


It all depends on what you want to do.



So to clarify, can you give an example of the script that would be used to restrict a user from submitting a request for more than 1 of these catalog items in a given time block.   So a user can submit requests for more than one item at a time from this "Item Group", they just cannot have overlapping designated time frames.



thanks so much