Catalog Item not prefilling the form

Aaron Duncan
Mega Sage

I have a catalog item that needs to prefill two fields, the short description and description.

 

I have the link setup to send the data over like so.

 

/sp?id=sc_cat_item&sys_id=72ba83d1b35313007a6de81816a8dc54&sysparm_variables=%7B&var_short_description=NDA%20Only%20Request%26var_description%3DType%20in%20the%20name%20of%20the%20vendor%7D

 

On the receiving catalog item, I have a Catalog Client Script that is setup in the following way. When I am testing it, the form loads, but the data doesn't insert into the field. I think it's with the function starting line 18 (see code below) but not exactly sure what is going on there. Can anyone help?

 

AaronDuncan_0-1776101779235.png

 

 

function onLoad() {
    // Wait for g_url to exist (Portal loads async)
    var interval = setInterval(function() {
        if (typeof g_url !== "undefined") {

            // Stop checking
            clearInterval(interval);

            var sd = getParameterValue('var_short_description');
            var desc = getParameterValue('var_description');
            // g_form.addInfoMessage('var_short_description');
            if (sd) g_form.setValue('short_description', sd);
            if (desc) g_form.setValue('description', desc);
        }
    }, 200);
}

function getParameterValue() {

    var objVariables = getParameterValue('sysparm_variables');
    console.debug('sysparm_variables URI parameter value: ' + objVariables);
    if (!objVariables || objVariables == '' || objVariables.indexOf('{') != 0) { //If variables is empty or not properly formatted, stop here. console.debug('aborting due to a missing or problematic sysparm_variables URI parameter');
        return;
    }
    objVariables = JSON.parse(objVariables);
    console.debug('sysparm_variables URI parameter value: ' + JSON.stringify(objVariables));
    for (prop in objVariables) {
        if (!objVariables.hasOwnProperty(prop)) {
            break;
        }
        propVal = objVariables[prop];
        console.debug('Processing property ' + prop + ' with value ' + propVal);
        if (g_form.hasField(prop)) {
            console.debug('Setting variable ' + prop + ' to value ' + propVal);
            g_form.setValue(prop, propVal);
        } else {
            console.warn('Variable ' + prop + ', specified in the sysparm_variables URI parameter, does not exist on this catalog item form.');
        }
    }
}

function getParameterValue(name) {
var objVariables = getParameterValue('sysparm_variables');
 console.debug('sysparm_variables URI parameter value: ' + objVariables);
 if (!objVariables || objVariables == '' || objVariables.indexOf('{') != 0) { //If variables is empty or not properly formatted, stop here. console.debug('aborting due to a missing or problematic sysparm_variables URI parameter');
 return;
 } objVariables = JSON.parse(objVariables);
 console.debug('sysparm_variables URI parameter value: ' + JSON.stringify(objVariables));
 for (prop in objVariables) { if (!objVariables.hasOwnProperty(prop)) { break;
 } propVal = objVariables[prop];
 console.debug('Processing property ' + prop + ' with value ' + propVal);
 if (g_form.hasField(prop)) { console.debug('Setting variable ' + prop + ' to value ' + propVal);
 g_form.setValue(prop, propVal);
 } else { console.warn('Variable ' + prop + ', specified in the sysparm_variables URI parameter, does not exist on this catalog item form.');
 } } } function getParameterValue(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
 var regexS = "[\\?&]" + name + "=([^&#]*)";
 var regex = new RegExp(regexS);
 //Check if portal (gel=undefined) or classic UI, and get the HREF data appropriately in either case. var hrefPath = (typeof gel == 'undefined') ? this.location.href : top.location.href;
 var results = regex.exec(hrefPath);
 console.debug('HREF: ' + hrefPath);
 if (results == null) { return "";
 } else { return unescape(results[1]);
 } }

// }

 

1 ACCEPTED SOLUTION

Try replacing window.location.href with one of these alternatives that work inside the SP sandbox:

function getURLParam(name) {
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var href = document.URL;  // accessible in SP sandbox
    var results = regex.exec(href);
    return results ? decodeURIComponent(results[1]) : '';
}

document.URL (or document.location.href) is accessible because document isn't blocked by the SP sandbox the way window is. If document also turns out to be restricted on your version/patch, you can fall back to the same trick the original script was using — this.location.href — but wrapped in a try/catch:

function getURLParam(name) {
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var href = '';
    try {
        href = document.URL;
    } catch (e) {
        try {
            href = top.location.href;
        } catch (e2) {
            href = this.location.href;
        }
    }
    var results = regex.exec(href);
    return results ? decodeURIComponent(results[1]) : '';
}

 

View solution in original post

6 REPLIES 6

Try replacing window.location.href with one of these alternatives that work inside the SP sandbox:

function getURLParam(name) {
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var href = document.URL;  // accessible in SP sandbox
    var results = regex.exec(href);
    return results ? decodeURIComponent(results[1]) : '';
}

document.URL (or document.location.href) is accessible because document isn't blocked by the SP sandbox the way window is. If document also turns out to be restricted on your version/patch, you can fall back to the same trick the original script was using — this.location.href — but wrapped in a try/catch:

function getURLParam(name) {
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var href = '';
    try {
        href = document.URL;
    } catch (e) {
        try {
            href = top.location.href;
        } catch (e2) {
            href = this.location.href;
        }
    }
    var results = regex.exec(href);
    return results ? decodeURIComponent(results[1]) : '';
}

 

Come to find out even the document.URL and document.location.href were blocked. However, the try catch with this.location.href worked. From there I had to change the g_form.setValue to short_description and description. It works perfectly now.

 

Thank you @Naveen20 !