Thursday, 14 September 2023

servicenow - send email to create catalog request item(sc_req_item) and trigger workflow for both sc_request and sc_req_item and save sc_req_item customized variables

 https://www.servicenow.com/community/now-platform-forum/create-catalog-request-through-email-inbound-action/m-p/1138512


https://www.servicenow.com/community/developer-forum/create-catalog-request-item-through-inbound-email-action/m-p/1438688



1) Need to use email inboud action :

Inbound Action: Create Requested Item


Table: sc_req_item


Condition: <your condition here>



2) then use the following script :


(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
createRequest();
function createRequest() {
       var grRequest = new GlideRecord ("sc_request");
       grRequest.initialize();
       grRequest.requested_for = sys_email.user_id;
       grRequest.short_description = email.subject.toString();
       grRequest.description = "received from: " + email.origemail + "\n\n" + email.body_text;
       var requestSysId = grRequest.insert();
       current.requested_for = '';
       current.short_description = email.subject.toString();
       current.description = "received from: " + email.origemail + "\n\n" + email.subject.toString() + "\n\n" + email.body_text;
       current.cat_item = 'SYS_ID OF THE CATALOG ITEM';
       current.parent = requestSysId;
       current.request = requestSysId;
       current.request_type = 'others';
       current.application= '';
       current.assignment_group='';
    var w = new Workflow();
       wfSysId = w.getWorkflowFromName("EXACT WORKFLOW NAME");
       w.startFlow(wfSysId, current, current.operation());
       current.insert();
}
})(current, event, email, logger, classifier);

3) sc_req_item customized variables

How to access service catalog item variable :

https://www.servicenow.com/community/itom-forum/where-catalog-variable-s-value-are-stored/m-p/908331



ar item = new GlideRecord("sc_req_item");



   item.addQuery("request", requestSys_id);



   item.query();



   if (item.next()) { 



// grab all variable sets from the parent request



     var var_own = new GlideRecord('sc_item_option_mtom');



     var_own.addQuery('request_item', item.sys_id);     



     //var_own.addQuery('sc_item_option.item_option_new.u_approval_display', 'global');



     var_own.orderBy('sc_item_option.item_option_new.order');



     var_own.query();       



     



     while (var_own.next()){



           



             gs.print(var_own.sc_item_option.item_option_new.question_text + ": " + eval('item.variable_pool.'+var_own.sc_item_option.item_option_new.name+'.getDisplayValue()') + "\n");



       }



       



     }

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


so to use code to save variables 

1) first insert question and value to sc_item_option, search by sc_item_option.list, insert question and value per row, and record sysId
2) then use sys_id from 1) and sys_id of your sc_req_item and insert into sc_item_option_mtom


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


No comments:

Post a Comment