{"id":1416,"date":"2016-08-20T14:11:16","date_gmt":"2016-08-20T19:11:16","guid":{"rendered":"https:\/\/danvanfleet.com\/?p=1416"},"modified":"2016-08-20T14:11:16","modified_gmt":"2016-08-20T19:11:16","slug":"select-apply-template-throws-infinite-loop-detected-error","status":"publish","type":"post","link":"https:\/\/danvanfleet.com\/index.php\/select-apply-template-throws-infinite-loop-detected-error\/","title":{"rendered":"Select Apply Template throws Infinite Loop detected error"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignleft size-medium wp-image-1417\" src=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/InfiniteLoopDetected-300x145.png?resize=300%2C145\" alt=\"error window Apply Template Failed, Infinite loop detected\" width=\"300\" height=\"145\" srcset=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/InfiniteLoopDetected.png?resize=300%2C145&amp;ssl=1 300w, https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/InfiniteLoopDetected.png?resize=150%2C73&amp;ssl=1 150w, https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/InfiniteLoopDetected.png?w=366&amp;ssl=1 366w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>When applying a templete in SoftPro Select, an <strong>Apply Template Failed<\/strong> error appears showing a couple Infinite loop detected errors. This can be caused by quite literally just about anything. It is coming from either a formula or Iron Python rule attempting to access a part of the file that doesn&#8217;t quite exist yet.<\/p>\n<p>&nbsp;<\/p>\n<p><!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"size-medium wp-image-1418 alignright\" src=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/SelectSaveError-300x186.png?resize=300%2C186\" alt=\"Select Save Error, Violation of Primary Key constraint, cannot insert duplicate key\" width=\"300\" height=\"186\" srcset=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/SelectSaveError.png?resize=300%2C186&amp;ssl=1 300w, https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/SelectSaveError.png?resize=150%2C93&amp;ssl=1 150w, https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2016\/08\/SelectSaveError.png?w=366&amp;ssl=1 366w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>Attempting to save the file at this point can result in a &#8220;Violation of PRIMARY\u00a0KEY constraint&#8221; in &#8220;some multiple table&#8221; complaining about a &#8220;duplicate key&#8221;. Sadly that error doesn&#8217;t help find where the Infinite loop detected happened. I&#8217;ve seen it close, but never on the mark. \u00a0This error can get quite long, but remains repetitive with each Violation having a corresponding terminated . The program error log can be helpful, &lt;ctrl&gt;&lt;shift&gt;&lt;L&gt; prior to the error.<\/p>\n<h2>What causes it<\/h2>\n<p>Let&#8217;s start with a bit of what is happening when an <strong>Apply Template<\/strong> is used. As Select is creating the\u00a0file, it doesn&#8217;t necessarily happen in a direct chain of events. The file is being built very fast in pieces, if part &#8220;A&#8221; attempts to use part &#8220;B&#8221; before part &#8220;B&#8221; exists, life can be very bad for the file.<\/p>\n<p>That minor\u00a0error\u00a0blows the whole overlay up, and we want it to. Remember Select is transactional in nature, it won&#8217;t process any part of an event unless all parts of the event succeed. Everything has a pass fail check on it. That important concept is why a partial file isn&#8217;t created. \u00a0Somewhat a\u00a0side effect is that nature\u00a0also\u00a0raises the complexity,\u00a0making\u00a0generating useful errors more difficult. \u00a0The piece of code which figures out there&#8217;s a problem, is quite far away from where on the users interface the problem originated. This is part of the reason\u00a0we see these imprecise errors in different places. Hats off to the dev team though, we&#8217;ve seen appreciated improvement in the last couple revisions, go dev go.<\/p>\n<h2>Check before you set<\/h2>\n<p>When creating Iron Python formulas, you learn very quickly to check for nulls. It&#8217;s the usual development process to make sure you have something, before you do something with it. I suppose that&#8217;s useful in the real world too, there&#8217;s no reason to swing a bat if you&#8217;re not holding a bat. \u00a0In the<strong> Apply Template<\/strong> case, things are happening in parallel, but still have to work, so null checking becomes\u00a0more important. Select formulas come into play when direct index calls are made.<\/p>\n<p>Imagine this simple formula on a <strong>Lender<\/strong> Contact <strong>Proposed insured clause:<\/strong> field<br \/>\n<code><br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\n<\/code><\/p>\n<p>It works just fine during testing and initial roll out. Select knows during the processing of the file to not put anything in the field if a LoanPolicy doesn\u2019t exist. <strong>Apply Template<\/strong> is FAST, you might not think of it that way, but trust me, it&#8217;s blazingly fast for what it&#8217;s doing. I don&#8217;t think the same rules that exist in the user interface are fully in play. Depending on the data, the Lender Contact can come into a\u00a0file either before the LoanPolicy, or without a LoanPolicy and cause a formula like this to stop the process.<\/p>\n<h2>How to prevent it.<\/h2>\n<p>Check to see you actually have something before doing any direct index calls. You might even need to check your something really has a parent something. You may be thinking, What was that again.<\/p>\n<p>Let&#8217;s change our code as an example.<br \/>\n<code><br \/>\nIf (Not IsEmpty({{Order.Title.LoanPolicies[1].ProposedInsured}})) Then<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nEndIf<br \/>\n<\/code><\/p>\n<p>Now we seem to be checking to make sure the ProposedInsured actually exists, so the problem will be fixed.<\/p>\n<p>It&#8217;s not.<\/p>\n<p>The problem here is there isn&#8217;t a LoanPolicy when the error is generated. We&#8217;re asking for LoanPolicy[1] or the first loan policy. \u00a0If there isn&#8217;t one, an error is generated before .ProposedInsured is looked at. Putting that hard coded number 1 in there is the root cause.<\/p>\n<h3>Working Example<\/h3>\n<p><code>If ({{Order.Title.LoanPolicies.Count}} &gt; 0) Then<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nEndIf<br \/>\n<\/code><\/p>\n<h3>Is there another way?<\/h3>\n<p>There is always another way. For this sort of thing I&#8217;d consider using a foreach or Iterate Loop, and process only the first one. The foreach by nature will be able to deal with the null reference when no Loan Policy exists. \u00a0Plus it&#8217;s nice to know what .Count is, but it&#8217;s way more useful to know how to Iterate.<\/p>\n<p>That looks like this:<\/p>\n<p><code><br \/>\nIterate {{order.Title.LoanPolicies}} From 1 To 1<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nLoop<br \/>\n<\/code><\/p>\n<p>So we&#8217;re saying, Hey Select, if you have any LoanPolicies hanging about, start at the first one, and do one of them. \u00a0Note: if Title were a multiple, it would be possible for this to fail when\u00a0there were no Title&#8217;s on the file, so check the parent when necessary.<\/p>\n<p>Another iteration that works is a little more verbose, thus more educational.<br \/>\n<code><br \/>\nIterate {{order.Title.LoanPolicies}} From 1 To # \u00a0 \u00a0 \u00a0 \u00a0{{order.Title.LoanPolicies}}<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nBreak<br \/>\nLoop<br \/>\n<\/code><\/p>\n<p>So here we&#8217;re saying loop through all of the LoanPolicies if any exist, but quit (break) after the first one is processed.<\/p>\n<h3>A little better<\/h3>\n<p>Finally to cross a\u00a0t or dot the i, we make sure we actually have a value before attempting to use it.<br \/>\n<code><br \/>\nIterate {{order.Title.LoanPolicies}} From 1 To #{{order.Title.LoanPolicies}}<br \/>\nIf (Not IsEmpty({{Order.Title.LoanPolicies[1].ProposedInsured}})) Then<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nEndIf<br \/>\nBreak<br \/>\nLoop<\/code><\/p>\n<p>As it goes, there&#8217;s another good way.<\/p>\n<p><code><br \/>\nIf ( Not IsEmpty ({{Order.Title.LoanPolicies}})) Then<br \/>\n{{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\nEndif<br \/>\n<\/code><\/p>\n<p>This is a good example of checking the existence of something before using it. It is also how parent and grandparent items can be checked for null.<\/p>\n<p>The moral of the story is a single line formula that contains a hard index is a potential problem. It will work some of the time, it won&#8217;t for Policies and other unknown areas. So be safe, do some checking to make sure things actually exist.<\/p>\n<p>A quick note, I&#8217;m not using value= syntax in this example you\u00a0probably should, so the active line would be<br \/>\n<code><br \/>\nvalue = {{Order.Title.LoanPolicies[1].ProposedInsured}}<br \/>\n<\/code><\/p>\n<h2>How to fix it<\/h2>\n<p>Search the formulas for <strong>[1]<\/strong>, any one of them could be the problem with some noted exceptions. Create a SoftPro support ticket to see\u00a0all your formulas in one text file. All Orders have a base set of multiples that would never cause this problem. \u00a0Order.Properties is one of them. All Select Order dot based &#8220;files&#8221; always have a property multiple. \u00a0I think there are a couple more,\u00a0if you have that list, send it to &#100;&#x61;n&#x40;&#x76;&#102;&#x69;n&#102;&#x6f;&#46;&#x63;o&#109;, or better yet, post a comment.<\/p>\n<p>I now have 130 contacts in this test file, anyone want to delete them?  A special thanks to Kayla who assisted in figuring out what was causing the initial error that lead to this blog entry. I don&#8217;t typically blog on support assisted fixes, in this case the value of understanding that formulas are more complex than they may appear, was worth bending that rule.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When applying a templete in SoftPro Select, an Apply Template Failed error appears showing a couple Infinite loop detected errors. This can be caused by quite literally just about anything. It is coming from either a formula or Iron Python rule attempting to access a part of the file that doesn&#8217;t quite exist yet. &nbsp;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3,23],"tags":[6,39,17],"class_list":["post-1416","post","type-post","status-publish","format-standard","hentry","category-general","category-softpro-2","tag-error","tag-select","tag-tips","entry"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wyVb-mQ","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/1416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/comments?post=1416"}],"version-history":[{"count":11,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/1416\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/1416\/revisions\/1429"}],"wp:attachment":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/media?parent=1416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/categories?post=1416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/tags?post=1416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}