<?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: Business rule to validate/check dates in SPM forum</title>
    <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026880#M26291</link>
    <description>&lt;P&gt;As Jim stated, I have achieved what I wanted, but with 2 pieces of code. I was annoyed at having 2 error messages displayed on each date field - but simply commented out the field error on one BR, and let the other one do the job. I will leave this here as someone else might find the BR useful. Thanks for the reply Jim.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Mark S.&lt;/P&gt;&lt;BR /&gt;</description>
    <pubDate>Wed, 29 Oct 2014 21:24:26 GMT</pubDate>
    <dc:creator>Baggies</dc:creator>
    <dc:date>2014-10-29T21:24:26Z</dc:date>
    <item>
      <title>Business rule to validate/check dates</title>
      <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026878#M26289</link>
      <description>&lt;P&gt;I am trying to create a business rule to validate the work_start and work_end dates on a Change Request. I can do this with this BR:&lt;/P&gt;&lt;P&gt;Table: Change Request&lt;/P&gt;&lt;P&gt;Order: 40&lt;/P&gt;&lt;P&gt;When: Before&lt;/P&gt;&lt;P&gt;Insert - true&lt;/P&gt;&lt;P&gt;Update - true&lt;/P&gt;&lt;P&gt;Condition - current.work_start.changes() || current.work_end.changes()&lt;/P&gt;&lt;P&gt;Script:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if ((!current.work_start.nil()) &amp;amp;&amp;amp; (!current.work_end.nil())) {&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; var start = current.work_start.getGlideObject().getNumericValue();&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; var end = current.work_end.getGlideObject().getNumericValue();&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (start &amp;gt; end) {&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; current.work_start.setError('start must be before end');&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; current.work_end.setError('start must be before end');&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.addErrorMessage('Work Start date must be before Work End date');&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current.setAbortAction(true);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This works just fine when I am on the change form, and hit save or update -great!&lt;/P&gt;&lt;P&gt;Now, the other requirement, is that when I close the Implementation Change Task, I also want the validation to run against the parent Change Request. If the dates are not validated, then do NOT close the Implementation Change Task, and ensure that the Implementation Change Task remains as its original state, and not "Closed" (ie Open or Work in Progress). (This part is fixed by adding the current.state = previous.state; before the abort action.&lt;/P&gt;&lt;P style="min-height: 8pt; height: 8pt; padding: 0px;"&gt; &amp;nbsp;&lt;/P&gt;&lt;P&gt;So I have copied, and modified the original BR to run on the task:&lt;/P&gt;&lt;P&gt;Name: Validate Work Dates - from Task&lt;/P&gt;&lt;P&gt;Table: Change Task&lt;/P&gt;&lt;P&gt;Order: 35&lt;/P&gt;&lt;P&gt;When: Before&lt;/P&gt;&lt;P&gt;Insert - true&lt;/P&gt;&lt;P&gt;Update - true&lt;/P&gt;&lt;P&gt;Condition - current.work_start.changes() || current.work_end.changes()&lt;/P&gt;&lt;P&gt;Script:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if ((!current.change_request.work_start.nil()) &amp;amp;&amp;amp; (!current.change_request.work_end.nil())) {&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; var start = current.change_request.work_start.getGlideObject().getNumericValue();&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; var end = current.change_request.work_end.getGlideObject().getNumericValue();&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (start &amp;gt; end) {&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; gs.log("**work start date is: &amp;nbsp; " + current.change_request.work_start);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; current.change_request.work_start.setError('start must be before end');&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; current.change_request.work_end.setError('start must be before end');&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; gs.addErrorMessage('*** Work Start date must be before Work End date on Change Request ***');&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current.state = previous.state; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current.setAbortAction(true);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;So I now have 2 working business rules running. My issues are:&lt;/P&gt;&lt;P&gt;How to only run one business rule when needed, I thought I could do this with the order&lt;/P&gt;&lt;P style="min-height: 8pt; height: 8pt; padding: 0px;"&gt; &amp;nbsp;&lt;/P&gt;&lt;P&gt;There are a few wiki/forum articles on this, and I have tried a few of them, in fact, this &amp;nbsp; is where I got the script from. Appreciate any input, I will post best answers, thanks, Mark S.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Oct 2014 20:11:38 GMT</pubDate>
      <guid>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026878#M26289</guid>
      <dc:creator>Baggies</dc:creator>
      <dc:date>2014-10-29T20:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: Business rule to validate/check dates</title>
      <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026879#M26290</link>
      <description>&lt;P&gt;You seem to have solved your issue with both of those Business Rules, so I'm not sure what the concern is. &amp;nbsp; Only one of the BRs will run at a time, it all depends on what record is being saved. &amp;nbsp; If you are attempting to save the Change Task, only the second BR will run. &amp;nbsp; The first will only run if you are attempting to save the original Change Request record.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Are you concerned about maintaining the two bits of code? &amp;nbsp; You don't have much choice but to do it this way. &amp;nbsp; You could get fancy and create a Script Include with the logic in it to check in both scenarios, but you would still need the Business Rules to call the Script Include.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Oct 2014 20:42:08 GMT</pubDate>
      <guid>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026879#M26290</guid>
      <dc:creator>Jim Coyne</dc:creator>
      <dc:date>2014-10-29T20:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: Business rule to validate/check dates</title>
      <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026880#M26291</link>
      <description>&lt;P&gt;As Jim stated, I have achieved what I wanted, but with 2 pieces of code. I was annoyed at having 2 error messages displayed on each date field - but simply commented out the field error on one BR, and let the other one do the job. I will leave this here as someone else might find the BR useful. Thanks for the reply Jim.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;Mark S.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Oct 2014 21:24:26 GMT</pubDate>
      <guid>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026880#M26291</guid>
      <dc:creator>Baggies</dc:creator>
      <dc:date>2014-10-29T21:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Business rule to validate/check dates</title>
      <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026881#M26292</link>
      <description>&lt;P&gt;I understand now. &amp;nbsp; I did not notice the...&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;PRE __default_attr="javascript" __jive_macro_name="code" class="_jivemacro_uid_14146181626067150 jive_text_macro jive_macro_code" jivemacro_uid="_14146181626067150"&gt;
&lt;P&gt;current.change_request.work_start.setError('start must be before end');&lt;/P&gt;&lt;BR /&gt;
&lt;P&gt;current.change_request.work_end.setError('start must be before end');&lt;/P&gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;...lines in the second BR. &amp;nbsp; They are really not needed because you are viewing the Change Task at that point and not the Change Request. &amp;nbsp; Are those the ones you removed?&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Oct 2014 21:29:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026881#M26292</guid>
      <dc:creator>Jim Coyne</dc:creator>
      <dc:date>2014-10-29T21:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: Business rule to validate/check dates</title>
      <link>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026882#M26293</link>
      <description>&lt;P&gt;My client has a section on the task form for Change Request details - so that part is visible from the task. Thanks.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 30 Oct 2014 16:05:21 GMT</pubDate>
      <guid>https://www.servicenow.com/community/spm-forum/business-rule-to-validate-check-dates/m-p/1026882#M26293</guid>
      <dc:creator>Baggies</dc:creator>
      <dc:date>2014-10-30T16:05:21Z</dc:date>
    </item>
  </channel>
</rss>

