Autopopulate two values in based on three fields.

Sirri
Tera Guru

Hi All,

 

we have requirement in catalog item there are three fields 

1)Requested for                                                                                                - Reference(sys_user)- [From Variable set]

2)Select the name of the person whose mailbox you need Full Access to  - Reference(sys_user)  - variable

3)Select the name of the Leaver whose mailbox you need Full Access to    - Reference(sys_user) - Variable

4)Confirmation                                                                                                     -Single Line Text(Readonly)

 

- When 2 & 3 fields are empty form should not submit please provide the code for this.

-When Both 2 & 3 are values filled form should not submit For this please provide code for this.

- In confirmation single line text we should want to populate 1&2 or 1&3 value As per the selection.

Please provide Clear code for this requirement.

 

Thank you

-

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @Sirri 

Yes you can achieve this all by using single onSubmit client script and validate everything in there, since I dont have your variable names so you can refer to the script below and replace the variables with your actual values, and let me know how it works for you or if you stuck somewhere.
onSubmit client script:

function onSubmit() {
    // Get values from the form fields
    var requestedFor = g_form.getValue('requested_for');
    var fullAccessToPerson = g_form.getValue('full_access_to_person');
    var leaverPerson = g_form.getValue('leaver_person');
    var confirmationField = g_form.getValue('confirmation_field');

    // Check if both fullAccessToPerson and leaverPerson are empty
    if (!fullAccessToPerson && !leaverPerson) {
        alert('Please fill either "Full Access to Person" or "Leaver Person" field.');
        return false; // Do not submit the form
    }

    // Check if both fullAccessToPerson and leaverPerson have values
    if (fullAccessToPerson && leaverPerson) {
        alert('Please fill either "Full Access to Person" or "Leaver Person," not both.');
        return false; // Do not submit the form
    }

    // Populate confirmationField based on the selection
    if (fullAccessToPerson) {
        g_form.setValue('confirmation_field', requestedFor + ' - ' + fullAccessToPerson);
    } else if (leaverPerson) {
        g_form.setValue('confirmation_field', requestedFor + ' - ' + leaverPerson);
    }

    // Allow the form submission
    return true;
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

View solution in original post

12 REPLIES 12

Aniket Chavan
Tera Sage
Tera Sage

Hello @Sirri 

Yes you can achieve this all by using single onSubmit client script and validate everything in there, since I dont have your variable names so you can refer to the script below and replace the variables with your actual values, and let me know how it works for you or if you stuck somewhere.
onSubmit client script:

function onSubmit() {
    // Get values from the form fields
    var requestedFor = g_form.getValue('requested_for');
    var fullAccessToPerson = g_form.getValue('full_access_to_person');
    var leaverPerson = g_form.getValue('leaver_person');
    var confirmationField = g_form.getValue('confirmation_field');

    // Check if both fullAccessToPerson and leaverPerson are empty
    if (!fullAccessToPerson && !leaverPerson) {
        alert('Please fill either "Full Access to Person" or "Leaver Person" field.');
        return false; // Do not submit the form
    }

    // Check if both fullAccessToPerson and leaverPerson have values
    if (fullAccessToPerson && leaverPerson) {
        alert('Please fill either "Full Access to Person" or "Leaver Person," not both.');
        return false; // Do not submit the form
    }

    // Populate confirmationField based on the selection
    if (fullAccessToPerson) {
        g_form.setValue('confirmation_field', requestedFor + ' - ' + fullAccessToPerson);
    } else if (leaverPerson) {
        g_form.setValue('confirmation_field', requestedFor + ' - ' + leaverPerson);
    }

    // Allow the form submission
    return true;
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Hi Aniket, 

 

 I'm getting below like this. Please help me regarding this

Sirri_0-1703679068578.png

 

Hello @Sirri ,

 

Understood, that is happening because those are the reference type variables so that's why the sys_id's are getting added that string variable, please try with the below code one and let me know how it works for you.

function onSubmit() {
    // Get display values from the form fields
    var requestedForDisplay = g_form.getDisplayValue('requested_for');
    var fullAccessToPersonDisplay = g_form.getDisplayValue('full_access_to_person');
    var leaverPersonDisplay = g_form.getDisplayValue('leaver_person');
    var confirmationField = g_form.getValue('confirmation_field');

    // Check if both fullAccessToPerson and leaverPerson are empty
    if (!fullAccessToPersonDisplay && !leaverPersonDisplay) {
        alert('Please fill either "Full Access to Person" or "Leaver Person" field.');
        return false; // Do not submit the form
    }

    // Check if both fullAccessToPerson and leaverPerson have values
    if (fullAccessToPersonDisplay && leaverPersonDisplay) {
        alert('Please fill either "Full Access to Person" or "Leaver Person," not both.');
        return false; // Do not submit the form
    }

    // Populate confirmationField based on the selection
    if (fullAccessToPersonDisplay) {
        g_form.setValue('confirmation_field', requestedForDisplay + ' - ' + fullAccessToPersonDisplay);
    } else if (leaverPersonDisplay) {
        g_form.setValue('confirmation_field', requestedForDisplay + ' - ' + leaverPersonDisplay);
    }

    // Allow the form submission
    return true;
}

Hi aniket,

 

Thank you

If I want  to see  the two users names in confirmation before the order. By using currently script I'm able to see after the order. Please let us know how we can achieve this.

Thank you