Setting a hidden Record Producer field's value using dom manipulation in service portal

aditya_agrawal
ServiceNow Employee
ServiceNow Employee

Hello,
I am unable to access the value of a hidden variable field named `captcha` in the record-producer script using producer.captcha, it's showing as empty.
I am setting it's value using javascript in the frontend through this script. : 

document.querySelector('input[name=captcha]').value = grecaptcha.getResponse();

This value is set by another widget (custom captcha widget) which renders alongside the record producer. 
I have verified that the input value is being set in the frontend, but I don't get it why the value is showing empty in the record producer script.

Any help/suggestions please.
Thank You

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@aditya_agrawal 

document object won't work in portal so your script will work only in native

Can you share entire script and screenshot of that catalog client script configuration?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you ankur for your reply,

Here's the record_producer script :  

gs.info("producer.captcha = " + producer.captcha);
current.case_type = 'supplier_onboarding';
current.short_description = 'Self registration by supplier ' + producer.supplier_name;
current.description = 'Self registration by supplier ' + producer.supplier_name;
current.priority = 5;
if (producer.captcha && verifyCaptcha()) {
    gs.addInfoMessage("Details have been submitted successfully");
} else {
    current.setAbortAction(true);
	gs.addErrorMessage("Captcha not verified");
}

function verifyCaptcha() {
    var url = 'https://www.google.com/recaptcha/api/siteverify';
    var data = {
        secret: gs.getProperty('glide.record_producer.captcha_secret', ''),
        response: producer.captcha,
    };
    var options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    };

    fetch(url, options).then(function(response) {
        return response.json().success;
    });
	return false;
}

Here the first line logs empty string for producer.captcha .

Actually I'm not using catalog client script with the record producer, instead am writing script in record producer itself .

aditya_agrawal_0-1687158307077.png


I see that using catalog client script would be a better practice for reusability, can you suggest me how do I achieve my requirement in this case .

Regards
Aditya

@aditya_agrawal 

you want to set value in that hidden variable? if yes then only way is to use onLoad catalog client script or Default value

using record producer script you just access the variable value using the syntax producer.variableName

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you Ankur, yet again,
So, it's not possible to bind a captcha response to the hidden form field? Actually whenever captcha is verified or expired, I am calling a callback, where I want to set this hidden field's value, like so  : 

  function setCaptchaResponse(){
    //some code to set a hidden form field's value
  }


I think, this won't be achieved using onload or default value .


Thank you