Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy Request Widget

BenjaminY
Tera Contributor

Hello,

I am tasked with created a copy widget after a request has been submitted on our custom ticket conversation page. Right now the button is failing in the client script. Please advise on how I can fix this or a more efficient way to create the widget all together.

HTML:

 <button
              class="btn btn-success btn-block"
          ng-if="c.data.showCopy"
              ng-click="c.action('copy_request')">
Copy Request
      </button>

Client Script:
if (action === 'copy_request') {
c.data.action = 'copy_request';
c.server.update().then(function (response) {
c.data.action = '';
if (response && response.data && response.data.redirect_url) {
$window.location.href = response.data.redirect_url;
} else if (response && response.data && response.data.error) {
spUtil.addErrorMessage(response.data.error);
} else {
spUtil.addErrorMessage('Could not open the new request.');
}
});
return;

Server Script:
 
// ACTION: user clicked the copy button
  if (input && input.action === 'copy_request') {
    var srcId = $sp.getParameter('sys_id') || '';
    var src=new GlideRecord('x_g_dh5_ccht_ccht_task');
 
    if (!srcId || !src.get(srcId)) {
      data.error = 'Source record not found.';
      return;
    }
 
    // Guard: only allow for Initial or Extension
    var reqType = (src.getValue('request_type_ind') || src.getValue('request_type_ind') || '').toLowerCase().trim();
    if (!(reqType === 'Request' || reqType === 'extension')) {
      data.error = 'Copy is only available for Initial or Extension requests.';
      return;
    }
 
    // ===============================
    // CONFIG: per-type catalog item + variable maps
    // - catId: sys_id of the Record Producer / Catalog Item
    // - varMap: table field -> catalog variable name (not label)
    //   You can add { display: true } to use getDisplayValue(source)
    // ===============================
    var TYPE_CFG = {
      'request': {
        catId: '338a6ee41b0321544d16ebdbac4bcbdb',
        varMap: [
          { source: 'witness_specialist_first_name', target: 'victim_witness_specialist_first_name' },
        ]
      },
      'extension': {
        catId: 'd7433e2d1bc761104d16ebdbac4bcb57',
        varMap: [
          { source: 'witness_specialist_first_name', target: 'ext_case_agent_last_name_first_name' },
        ]
      }
    };
 
    var cfg = TYPE_CFG[reqType];
    if (!cfg || !cfg.catId) {
      data.error = 'No catalog configuration found for type: ' + reqType;
      return;
    }
 
    // ===============================
    // BUILD variables and redirect to catalog item (prefilled, not ordered)
    // ===============================
    try {
      var cart = new sn_sc.CartJS();
      var vars = {};
 
      (cfg.varMap || []).forEach(function (m) {
        if (!m || !m.source || !m.target) return;
 
        var val = (m.display === true) ? src.getDisplayValue(m.source) : src.getValue(m.source);
        if (val !== null && val !== undefined && (val + '') !== '') {
          vars[m.target] = val;
        }
      });
 
      // Add to cart (pre-fills variables; does NOT submit)
      var addRes = cart.addToCart(cfg.catId, 1, vars);
 
      // Normalize cart item id across platform versions
      var cartItemId = '';
      if (addRes) {
        if (addRes.sys_id) {
          cartItemId = addRes.sys_id;
        } else if (addRes.result && addRes.result.sys_id) {
          cartItemId = addRes.result.sys_id;
        } else if (addRes.items && addRes.items.length && addRes.items[0].sys_id) {
          cartItemId = addRes.items[0].sys_id;
        }
      }
 
      var base = gs.getProperty('glide.servlet.uri') + 'sp?id=sc_cat_item&sys_id=' + cfg.catId;
      data.redirect_url = cartItemId ? (base + '&sysparm_id=' + cartItemId) : base;
 
    } catch (e) {
      data.error = 'Error preparing catalog item: ' + (e.message || e);
    }
  }
 


1 REPLY 1

Nawal Singh
Tera Guru

Hi @BenjaminY ,

The widget that you have shared is OOB or custom one, if it's OOB then clone and used it , if not then please debug where it's stuck , and shared the issue so we can help you further.

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh