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.

Survey Form on Email notification

sushma suresh
Tera Contributor

Hi Community Team,

My requirement is , need to customise the survey feedback form in email notification ,once we click on submit it should redirect to outlook with those form details .

 

sushmasuresh_0-1701258487366.png

 

sushmasuresh_1-1701258534705.png

 

but I am getting values like undefined. for this wrote email script as well as HTML .

In notification body source code  i have wrote HTML code .

Html Code:

 

<form id="survey-form" action="mailto:dev170026@service-now.com?SUBJECT=Re:${current.number} - Submitted&amp;body=Name%3D${name}%0ARating%3D${dropdown}%0AHelpful%3D${radio}%0AComments%3D${comments}" method="post"><label id="name-label" for="name"> What is your name? <input required="" type="text" placeholder="Enter Your Name" /> </label> <label id="email-label" for="email"> What is your email address? <input id="email" required="" type="email" placeholder="Enter Your Email Address" /> </label> <label id="number-label" for="number"> What is your age? <input id="number" max="90" min="13" type="number" placeholder="Enter Your Age" /> </label> <label for="dropdown"><label for="dropdown"> Which option best describes your current role?<select id="dropdown">
<option>Student</option>
<option>Full Time Job</option>
<option>Part Time Job</option>
<option>Freelancer</option>
<option>Prefer Not to say</option>
</select></label></label>
<div class="form-group">
<p>Would you recommend IZT to a friend?</p>
<label> <input name="radio" type="radio" value="definitely" />Definitely </label> <br /><label> <input name="radio" type="radio" value="maybe" />Maybe </label> <br /><label> <input name="radio" type="radio" value="not-sure" />Not Sure </label></div>
<div class="form-group">
<p>What would you like to see improved? (Check all that apply)</p>
<label> <input type="checkbox" value="front-end" />Front-end projects </label> <br /><label> <input type="checkbox" value="backend" />Back-end projects </label> <br /><label> <input type="checkbox" value="Challenges" />Challenges </label></div>
<div class="form-group">
<p>Any comments or suggestions?</p>
<textarea cols="30" rows="4" placeholder="Enter Your Comments Here..."></textarea></div>
<label for="dropdown"><button id="requestsubmit" style="border-radius: 4px; border: 1px solid #4f52bd; padding: 6px 16px; color: #4f52bd;" type="submit" value="Submit">${mail_script:survey_emails_inbound} </button> </label></form>

 

Email script :

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
    gs.log("ini",current,"temp",template);
    // Get the GlideRecord for the current email
    var gr = email.getGlideRecord();
gs.log("sush");
    // Get the body of the email
    var emailBody = gr.body;

    // Parse the email body to extract form data (assuming a simple key-value structure)
    var formData = parseEmailBody(emailBody);

    // Extract values from the parsed form data
    var name = formData.name;
    var emailValue = formData.email;
    var age = formData.number;
    var role = formData.dropdown;
    var recommendation = formData.radio;
    var improvements = formData.checkbox; // Assuming a checkbox group
    var comments = formData.comments;

    // Build the email subject and body
    var subjectTxt = encodeURI('Re:' + current.number + ' - Submitted');
    var bodyTxt = encodeURI(
        'Name: '+ name + '\n' +
        'Email: ' + emailValue + '\n' +
        'Age: ' + age + '\n' +
        'Role: ' + role + '\n' +
        'Recommendation: ' + recommendation + '\n' +
        'Improvements: ' + improvements + '\n' +
        'Comments: ' + comments
    );

    // Print the mailto link
    template.print('<a title="click here" href="mailto:dev170026@service-now.com?SUBJECT=' + subjectTxt + '&amp;body=' + bodyTxt + '">Submit</a>');

    // Function to parse email body (customize based on your email structure)
    function parseEmailBody(body) {
        gs.log("returned value"+body);
        // Add your parsing logic here to extract form data
        // For example, split by newline and then split by ':'
        var lines = body.split('\n');
        var formData = {};
        lines.forEach(function(line) {
            var parts = line.split(':');
            if (parts.length === 2) {
                formData[parts[0].trim()] = parts[1].trim();
            }
        });
        return formData;
    }

})(current, template, email, email_action, event);
 
 Above attached snip is the form I created, after submitting the form ,the details in the form should redirect to outlook as shown Above.

 

Then I can perform Email inbound action, but the value retrieved as undefined.

Help out to retrieve the correct data from the form to the outlook

 

Thanks,

Sushma 

 

0 REPLIES 0