6 Replies · Latest reply on Apr 21, 2017 5:30 AM by dhathri chidananda

    I want to insert record to 'cmdb_rel_ci' table through Business rule

      I have written before business rule.

       

      var gr=new GlideRecord('x_42426_automation_automation_rules');

      gr.addQuery('automation_category','calculate_relationship');

      gr.orderBy('order');

      gr.query();

      gs.addInfoMessage('test 1');

        while(gr.next())

        {

        var attribute=gr.getValue('target_attribute');

        var val=gr.getValue('target_value');

        var query = gr.getValue('query_string');

         gs.addInfoMessage('test 2');

      var match=GlideFilter.checkRecord(current,query);

       

         if (match)

         {

         var childT=current.name;

         var manages='Managed by';

      gs.addInfoMessage(val);

         setRelationshipValues(val, childT, manages);

         }

      }

       

      function setRelationshipValues(parent, child, relationshipType) {  

      gs.addInfoMessage(parent);

      gs.addInfoMessage(child);

      gs.addInfoMessage(relationshipType);

        var rel_ci = new GlideRecord("cmdb_rel_ci");  

        rel_ci.initialize();  

        rel_ci.parent = parent;   

        rel_ci.child = child;   

        rel_ci.type = relationshipType;  

        if(rel_ci.parent!=rel_ci.child)

        {

        rel_ci.insert(); }

      }

       

      its inserting empty record in 'cmdb_rel_ci' table. Could anyone please help on this.

        • Re: I want to insert record to 'cmdb_rel_ci' table through Business rule
          Chuck Tomasi

          Hi,

           

          A couple things I see... this should be an AFTER business rule rather than BEFORE because you are creating related records. BEFORE is for doing operations on a current record BEFORE you commit. Reference: Business Rules Best Practices - ServiceNow Wiki 

           

          What are you debug statements looking like? Do the messages with parent/child/relationshipType show valid values?

           

          You might also consider putting that if in the setRelationshipValue() before you spend the time to declare and populate the record. Something more like this:

           

          function setRelationshipValues(parent, child, relationshipType) { 

              gs.addInfoMessage(parent);

              gs.addInfoMessage(child);

              gs.addInfoMessage(relationshipType);

           

              if (parent == child)

                return;

           

             var rel_ci = new GlideRecord("cmdb_rel_ci"); 

             rel_ci.initialize(); 

             rel_ci.parent = parent;  

             rel_ci.child = child;  

             rel_ci.type = relationshipType; 

             rel_ci.insert(); }

          }

          --Chuck Tomasi
          --ServiceNow
          Sign up for Knowledge 17 - Our biggest and best ever!
          May 7-11, Orlando FL
          http://knowledge.servicenow.com/

            • Re: I want to insert record to 'cmdb_rel_ci' table through Business rule
              dhathri chidananda

              Hi Chuck,

               

              I have made it after BR and also modified the code.

               

              var gr=new GlideRecord('x_42426_automation_automation_rules');

              gr.addQuery('automation_category','calculate_relationship');

              gr.orderBy('order');

              gr.query();

              gs.addInfoMessage('test 1');

                while(gr.next())

                {

                var attribute=gr.getValue('target_attribute');

                var val=gr.getValue('target_value');

                var query = gr.getValue('query_string');

                 gs.addInfoMessage('test 2');

              var match=GlideFilter.checkRecord(current,query);

               

                 if (match)

                 {

                 var childT=current.name;

                 var manages='Managed by';

              gs.addInfoMessage(val);

                 setRelationshipValues(val, childT, manages);

                 }

              }

               

              function setRelationshipValues(parent, child, relationshipType) {

                  gs.addInfoMessage(parent);

                  gs.addInfoMessage(child);

                  gs.addInfoMessage(relationshipType);

               

                  if (parent == child)

                    return;

               

                 var rel_ci = new GlideRecord("cmdb_rel_ci");

                 rel_ci.initialize();

                 rel_ci.parent = parent; 

                 rel_ci.child = child; 

                 rel_ci.type = relationshipType;

                 rel_ci.insert();

              }

               

              Still its inserting empty record, and also parent, child and relationship type in 'cmdb_rel_ci' table are reference fields...I'm hardcoding the values taken from other table...can you please through some light on this.

            • Re: I want to insert record to 'cmdb_rel_ci' table through Business rule
              dhathri chidananda

              Thank you very much Benn and Chuck .... the code is finally working as expected