<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Making a variable mandatory only on state choice changes in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173578#M2193</link>
    <description>&lt;P&gt;In this example, 'state' is undefined.&amp;nbsp; Since this is running onChange of the State field, you can use newValue to retrieve the value of the state field.&amp;nbsp; Also, you'll want to include an else block so that the variable remains not mandatory if the user switched to the Closed Complete State then another State before saving the record.&amp;nbsp; The variable.variableName syntax is optional and can be simplified to just use the variable name:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var variableValue = g_form.getValue('variableName');
    if (variableValue == '' &amp;amp;&amp;amp; newValue == 3) {
        g_form.setMandatory('variableName', true);
    } else {
		g_form.setMandatory('variableName', false);
    }
	&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Feb 2025 19:40:32 GMT</pubDate>
    <dc:creator>Brad Bowman</dc:creator>
    <dc:date>2025-02-10T19:40:32Z</dc:date>
    <item>
      <title>Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173195#M2186</link>
      <description>&lt;P&gt;Hi All&lt;BR /&gt;After submitting the catalog item I want to make a variable set mandatory in sc_req_item table workspace only when state choice is changes to closed complete. I have tried on change client script as well but the field name cant be state in catalog item. Can someone help on this.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 13:14:03 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173195#M2186</guid>
      <dc:creator>Sonal2</dc:creator>
      <dc:date>2025-02-10T13:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173274#M2187</link>
      <description>&lt;P&gt;You can do this with a Client Script (not Catalog) that applies to the sc_req_item table, not the Catalog Item.&amp;nbsp; Use g_form methods with the name of the RITM field or Catalog Item variable.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 13:52:19 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173274#M2187</guid>
      <dc:creator>Brad Bowman</dc:creator>
      <dc:date>2025-02-10T13:52:19Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173296#M2188</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/248307"&gt;@Sonal2&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Agree with Brad.&lt;/P&gt;
&lt;P&gt;It should be onChange client script on State field and simply check if variable is populated&lt;/P&gt;
&lt;P&gt;If not then make it mandatory&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var variableValue = g_form.getValue('variables.variableName');
    if (variableValue == '' &amp;amp;&amp;amp; state == 3) {
        g_form.setMandatory('variables.variableName', true);
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;If my response helped please mark it correct and close the thread so that it benefits future readers.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 14:08:22 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173296#M2188</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2025-02-10T14:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173375#M2189</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/265966"&gt;@Ankur Bawiskar&lt;/a&gt;&amp;nbsp;its not working.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 14:53:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173375#M2189</guid>
      <dc:creator>Sonal2</dc:creator>
      <dc:date>2025-02-10T14:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173384#M2190</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/265966"&gt;@Ankur Bawiskar&lt;/a&gt;&amp;nbsp;, This script is not working&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sonal2_0-1739199591886.png" style="width: 400px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/418307iCFC27770F5B3F533/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Sonal2_0-1739199591886.png" alt="Sonal2_0-1739199591886.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 15:00:08 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173384#M2190</guid>
      <dc:creator>Sonal2</dc:creator>
      <dc:date>2025-02-10T15:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173578#M2193</link>
      <description>&lt;P&gt;In this example, 'state' is undefined.&amp;nbsp; Since this is running onChange of the State field, you can use newValue to retrieve the value of the state field.&amp;nbsp; Also, you'll want to include an else block so that the variable remains not mandatory if the user switched to the Closed Complete State then another State before saving the record.&amp;nbsp; The variable.variableName syntax is optional and can be simplified to just use the variable name:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var variableValue = g_form.getValue('variableName');
    if (variableValue == '' &amp;amp;&amp;amp; newValue == 3) {
        g_form.setMandatory('variableName', true);
    } else {
		g_form.setMandatory('variableName', false);
    }
	&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:40:32 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173578#M2193</guid>
      <dc:creator>Brad Bowman</dc:creator>
      <dc:date>2025-02-10T19:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Making a variable mandatory only on state choice changes</title>
      <link>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173740#M2194</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/248307"&gt;@Sonal2&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;small mistake in my script.&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var variableValue = g_form.getValue('variables.variableName');
    if (variableValue == '' &amp;amp;&amp;amp; g_form.getValue('state') == 3) {
        g_form.setMandatory('variables.variableName', true);
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;If my response helped please mark it correct and close the thread so that it benefits future readers.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2025 03:00:29 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/making-a-variable-mandatory-only-on-state-choice-changes/m-p/3173740#M2194</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2025-02-11T03:00:29Z</dc:date>
    </item>
  </channel>
</rss>

