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

You will need an onChange client script that queries the cmdb_ci_computer table for the user's asset.


So:


Type: onChange


field: customer (or whatever the field the end user enters their value in)


Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var computer = new GlideRecord('cmdb_ci_computer');


  computer.addQuery('assigned_to', newValue);


  computer.query();


  if(computer.next()){


  g_form.setValue('u_computer', computer.sys_id);


  }


}



Note1:


This will work when the user changes the value of the field "customer". If you need the field "computer" to be be set on load of a form, copy the script to a new onLoad script and change "newValue" to you customer's field, for instance:


var customer = g_form.getValue('caller_id');


and


computer.addQuery('assigned_to', customer);



Note2:


u_computer is the field where the asset name will appear. It is a reference field to the cmdb_ci_computer table. If you want it to be a string field, change line 11 to:


g_form.setValue('u_computer', computer.name);



harel


Please mark as correct or helpful.


Also, next   time you can open a new question and get answers from smarter people


patricklatella
Mega Sage

thanks Harel,


so the field I need to auto populate is Current Computer, variable name current_computer, do I need an advanced reference qualifier for this?


patricklatella
Mega Sage

right now I have the Current Computer field set with an advance reference qualifier that is:



javascript: 'assigned_to='+current.variables.cur_user;



and this is making the correct choices appear when you click the magnifying glass icon...I just need the choice (if there is only one) to auto-populate



I entered what you provided and so far not working.   I also have a working script include called u_Computer_Requests_Ajax that is governing several catalog client scripts, would I need to add anything to that?


Catalog item - I missed that...


As for the u_Computer_Requests_Ajax - depends on what it does...



So you can add a catalog client script as follows (be sure to change the name of the fields according to what you have):


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }


  //Type appropriate comment here, and begin script below


  g_form.clearValue('computer'); //clear the computer field in case there's something in it


  var user = g_form.getValue('cur_user'); //the current user field


  var ga = new GlideAjax('getAsset');


  ga.addParam('sysparm_name', 'asset');


  ga.addParam('sysparm_user', user);


  ga.getXML(getResponse);



  function getResponse(response) {


  var answer = response.responseXML.documentElement.getAttribute('answer');


  g_form.setValue('computer', answer);


  }


}



and the corresponding script include:


Name: getAsset


Client callable: yes


var getAsset = Class.create();


getAsset.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  asset : function() {



  var user = this.getParameter('sysparm_user');


  var computer = new GlideRecord('cmdb_ci_computer');


  computer.addQuery('assigned_to', user);


  computer.query();



  var count = computer.getRowCount();


  while(computer.next()) {


  if(count == 1){


  var answer = computer.name;


  return answer;


  }


  }


},


      type: 'getAsset'


});



harel


patricklatella
Mega Sage

thanks,


when you say (be sure to change the name of the fields according to what you have):



which fields in the code should I be sure to check?   I apologize, my java skills are limited