How to clone RITM with all variables

sam1212
Mega Expert

Hi All,

I was trying to clone a RITM with all its variable what ever is entered . Like a clone button which creates a new RITM with all the variables. I am stuck with the copy of variables.

I heard their is OOB   clone available in some discussions but did not pointed exactly what it   do and how to use it.

Any help is appreciated .

1 ACCEPTED SOLUTION

sam1212
Mega Expert

Hi Guys here is the code to copy the RITM with all variables to new RITM without workflow issues ,




var user=current.request.requested_for;


deleteCart();



var realCart = getCart();


var cartID = realCart.sys_id;


gs.log("cartID..",cartID);


addToCart(cartID, current.cat_item, 1);


addOptions(cartID);


doOrder();


deleteCart();



function addToCart(cartid, cat_item, quantity) {


      var gr = new GlideRecord('sc_cart_item');


      gr.initialize();


      gr.cart = cartid;


      gr.cat_item = cat_item;


      gr.quantity = quantity;


      gr.insert();


}


function addOptions(cartID) {


     


     


      var kids = new GlideRecord('sc_cart_item');


      kids.addQuery('cart', cartID);


      kids.query();


      if (kids.next()) {


              gs.log("kids",kids.sys_id);


              var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');


             


              sc_item_option_mtom.addQuery('request_item', current.sys_id);


             


              sc_item_option_mtom.query();


             


     


              while(sc_item_option_mtom.next()){


                     


                      var sc_item_option = new GlideRecord('sc_item_option');


                     


                      sc_item_option.addQuery('sys_id', sc_item_option_mtom.sc_item_option);


                     


                      sc_item_option.query();


                     


                     


                      if(sc_item_option.next()){


                             


                              var sc_item_option_insert = new GlideRecord('sc_item_option');


                             


                              sc_item_option_insert.initialize();


                             


                              sc_item_option_insert.item_option_new=sc_item_option.item_option_new;


                             


                              sc_item_option_insert.value=sc_item_option.value;


                              sc_item_option_insert.cart_item.setValue(kids.sys_id);


                              sc_item_option_insert.insert();


                     


                      }


                     


                     


              }


             


             


      }


}


function deleteCart() {


     


      var cart = getCart();


      var id = cart.sys_id;


      var kids = new GlideRecord('sc_cart_item');


      kids.addQuery('cart', cart.sys_id);


      kids.deleteMultiple();


}


function getCart() {


     


      var cart = new GlideRecord('sc_cart');


     


      var userid = user;


      cart.addQuery('user', userid);


      cart.query();


      if (cart.next()) {


              // we already have a cart all is well


      }


      else {


              cart.initialize();


              cart.user = userid;


              cart.insert();


      }


      return cart;


}


function doOrder() {


      var req = new GlideappRequestNew();


      var result=   req.copyCart(null,user);


     


}


View solution in original post

13 REPLIES 13

sam1212
Mega Expert

As we know workflow generally trigger on creation of RITM but now the issue is workflow goes to begin state but it stops their it does not run further.


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Check Share as there appear to be a few solutions. I dont have any experience with them but it's worth investigatin.   Here is an example:


ServiceNow Share


Works perfectly on my HP11b. Thank you


sam1212
Mega Expert

Hi Guys here is the code to copy the RITM with all variables to new RITM without workflow issues ,




var user=current.request.requested_for;


deleteCart();



var realCart = getCart();


var cartID = realCart.sys_id;


gs.log("cartID..",cartID);


addToCart(cartID, current.cat_item, 1);


addOptions(cartID);


doOrder();


deleteCart();



function addToCart(cartid, cat_item, quantity) {


      var gr = new GlideRecord('sc_cart_item');


      gr.initialize();


      gr.cart = cartid;


      gr.cat_item = cat_item;


      gr.quantity = quantity;


      gr.insert();


}


function addOptions(cartID) {


     


     


      var kids = new GlideRecord('sc_cart_item');


      kids.addQuery('cart', cartID);


      kids.query();


      if (kids.next()) {


              gs.log("kids",kids.sys_id);


              var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');


             


              sc_item_option_mtom.addQuery('request_item', current.sys_id);


             


              sc_item_option_mtom.query();


             


     


              while(sc_item_option_mtom.next()){


                     


                      var sc_item_option = new GlideRecord('sc_item_option');


                     


                      sc_item_option.addQuery('sys_id', sc_item_option_mtom.sc_item_option);


                     


                      sc_item_option.query();


                     


                     


                      if(sc_item_option.next()){


                             


                              var sc_item_option_insert = new GlideRecord('sc_item_option');


                             


                              sc_item_option_insert.initialize();


                             


                              sc_item_option_insert.item_option_new=sc_item_option.item_option_new;


                             


                              sc_item_option_insert.value=sc_item_option.value;


                              sc_item_option_insert.cart_item.setValue(kids.sys_id);


                              sc_item_option_insert.insert();


                     


                      }


                     


                     


              }


             


             


      }


}


function deleteCart() {


     


      var cart = getCart();


      var id = cart.sys_id;


      var kids = new GlideRecord('sc_cart_item');


      kids.addQuery('cart', cart.sys_id);


      kids.deleteMultiple();


}


function getCart() {


     


      var cart = new GlideRecord('sc_cart');


     


      var userid = user;


      cart.addQuery('user', userid);


      cart.query();


      if (cart.next()) {


              // we already have a cart all is well


      }


      else {


              cart.initialize();


              cart.user = userid;


              cart.insert();


      }


      return cart;


}


function doOrder() {


      var req = new GlideappRequestNew();


      var result=   req.copyCart(null,user);


     


}


jaytee
Kilo Contributor

Good Day



Hi Sam, may we request for sharing the solution please? I mean, did you put the code in UI action or something else? We are looking for this kind of solution for cloning an RITM that is stuck to an old workflow and create a new RITM with exact value it has before but using the new workflow.



Please advise, thank you