The CreatorCon Call for Content is officially open! Get started here.

Business Rule - Create copy of current item?

Edwin Fuller
Tera Guru

Hello everyone

I need to create a duplicate of the current item whenever a field is answered a certain way; Is this possible using business rule script? The duplicate would get created when the field "survey_question_1" equals "New ask based on response". Examples would be great!!

Current Business Rule script

var gdt= new GlideDateTime();

current.actual_completion_date.setValue(gdt);

if (current.survey_question_2 == 'New Ask based on response') {

}

1 ACCEPTED SOLUTION

Thank you Abhinay, the script you provided below works perfectly. My next ask is how can I create a copy without copying all of the fields, only the fields that I want/need?



if (current.survey_question_2 == 'New Ask based on response') {


var gr= new GlideRecord("x_hemas_connectus2_x_hemas_connectus_connectus");


gr.newRecord();


gr.insert();


}


View solution in original post

19 REPLIES 19

veena_kvkk88
Mega Guru

Hi Edwin,



It would be an after business rule and goes something like this: (Untested code)



var gdt= new GlideDateTime();


current.actual_completion_date.setValue(gdt);


if (current.survey_question_2 == 'New Ask based on response') {


      current.insert();


}



This would create an exact copy of the current record, if you need any changes, that can be incorporated as well. (Related lists won't be carried over - additional scripting required)



Edit: Unless there are any primary key violations. In that case you can assign new values for those fields and then insert.


When I try this, it works but is giving an exception because the business rule is firing out a second time after the duplicate is created. (Since the duplicate will have the same choice as well)


The only solution I can think of is: Writing this as an after update business rule, so that it won't run again when new record is inserted. The drawback is that this won't take effect on insert. Let's hope other community experts have a better solution for this.


Thanks - Do you know how to get the next number up?



find_real_file.png


Try this:


  1. var gdt= new GlideDateTime();  
  2. current.actual_completion_date.setValue(gdt);  
  3. if (current.survey_question_2 == 'New Ask based on response') {  
  4.         current.number = getNextObjNumberPadded();
  5.         current.insert();  
  6. }