Copy Request Widget
						
					
					
				
			
		
	
			
	
	
	
	
	
Options
			
				
					
	
			
		
	- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago - last edited 6 hours ago
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:
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		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:
             
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);
    }
  }
		0 REPLIES 0
	
		
		
			
			
			
					
	
			