12 Replies · Latest reply on Dec 7, 2017 3:43 AM by latvamik

    Create new records from several places

      Hi guys,

       

      Being relatively "green" on what comes to scripting in ServiceNow, I'm asking for any hints from you.

       

      I have a requirement to create records in a custom table from "anywhere". This "Anywhere" could be for example a workflow or scheduled job.

       

      What element should I use if I want to create records and bypass the values from other places, Script Include?

       

      Example: Last job in my workflow is to create a new record. I want to send several values and store these values to a newly created record. I want to do the same from several places so I don't want to multiply my script in several places.

       

      If a script include is a solution for this need, could someone please guide me how to start creating one and how to define the variables that can be passed to the Script Include. Basically I know how to create a record in a script and put the data there.

        • Re: Create new records from several places
          Sushant Malsure

          yes script include is the way to go.

           

          create a script include and a function in it.

          this function should accepts the parameters (which you will pass from workflow) and these parameters can be fields which are mandatory on custom table where you want to create a record.

          So basically you will be calling script include function with parameter 'N' number of times ; script include will create a record for you in custom table with values you provided as parameter

          1 of 1 people found this helpful

          Regards,
          Sushant Ram Malsure

          • Re: Create new records from several places
            ashutoshm1

            HI,

             

            It is always good to use script include if you want to perform the same work.

             

            Script includes can be invoke from several places in service now.

             

            Now this totally depends how will you pass values from several places to this script include. But the core logic will remain the same i.e. to create a record.

             

            you will have to define a sequence in which you will pass on values to this script includes.

             

            If you want an example i will provide that, let me know.

             

             

            Thank you,
            Ashutosh Munot

            Please Hit Correct, Helpful or like,if you are satisfied with this response.

            1 of 1 people found this helpful
            • Re: Create new records from several places
              gowrisankar sativada

              Hi lat,

               

              Yes, Script Includes are the only coding blocks in ServiceNow which should be perfectly utilized for Reusing.

              Script includes

               

              Function in a Script Include:

               

              1. var myCustomClass = Class.create(); 
              2. myCustomClass.prototype = { 
              3.     initialize: function() { 
              4.     }, 
              5.      
              6.     myCustomFunction: function () { 
              7.     ... 
              8. }

               

              Calling a Function "myCustomFunction" from Script Include with name "myCustomClass":

               

              var myVariable = new myCustomClass(); 

              myCustomClass.myCustomFunction(); 

               

              You can call script includes from all Server Side Elements, as well as Client Side using GlideAjax:

              GlideAjax - ServiceNow Wiki

               

              Below are few places where you can call a script include:

               

              Business Rules

              Workflows

              Reference Qualifiers

              Scheduled Jobs

              Client Scripts

              1 of 1 people found this helpful

              Happy to help, Award Correct Answer if my Solution Works, Mark helpful if it helps...

              • Re: Create new records from several places
                ashutoshm1

                Please Mark Response as Correct accordingly to close this thread.


                Thank you,

                 

                 

                Ashutosh

                • Re: Create new records from several places
                  latvamik

                  Thanks, extremely fast response!

                   

                  Can you provide me with a short example:

                  1. how to create a Script include to accept several parameters (eg. parm1, parm2...) and

                  2. how to call it and send those parameters.

                   

                  I believe I'm then able to create a Script include to handle the parameters and create a record to store these parameters to a table.

                   

                  EDIT: Even better, is there an existing ootb script include that I could check as an example?

                    • Re: Create new records from several places
                      ashutoshm1

                      Hi,

                       

                      So if you want to call glide ajax from client side you have to use GlideAjax.

                      GlideAjax - ServiceNow Wiki

                       

                      If call script include from client it should be client callable. There is a check box known as client callable on script include.

                       

                      If you want to call from server side then you have to crete a object and then call. See below link:

                      Can we call a script include from a business rule

                      This is how you call it from server code and workflows.

                       

                      1. var myVariable = new myCustomClass();  //mycustomclass is  script include name
                      2. myCustomClass.myCustomFunction(''pass parameters here.);  //mycustomefunction is function name in script include

                      Script include:

                      1. var myCustomClass = Class.create(); 
                      2. myCustomClass.prototype = { 
                      3.     initialize: function() { 
                      4.     }, 
                      5.      
                      6.     myCustomFunction: function () { 
                      7.     ... 

                       

                      Thank you,

                      Ashutosh munot

                      1 of 1 people found this helpful
                      • Re: Create new records from several places
                        gowrisankar sativada

                        Calling a Function "myCustomFunction" from Script Include with name "myCustomClass":

                         

                        var myVariable = new myCustomClass();

                        myCustomClass.myCustomFunction(current,'test');

                         

                        Here I am passing a

                         

                        Parm1: current record object(from a business rule)

                        parm2: string with value "test"

                         

                        Function in a Script Include:

                         

                        1. var myCustomClass = Class.create();
                        2. myCustomClass.prototype = {
                        3.     initialize: function() {
                        4.     },
                        5.     
                        6.     myCustomFunction: function (current,value) {
                        7.    gs.log(current.<field_name>) //This will give you the field value i.e.if I pass current object of Incident Record,current.number gives me incident number like "inc002341"
                        8.      gs.log(value) //Gives me test value
                        9. }
                        1 of 1 people found this helpful

                        Happy to help, Award Correct Answer if my Solution Works, Mark helpful if it helps...