Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clone case along with variables

Hafsa1
Mega Sage

I have a requirement to duplicate a customer service case along with all its variables in variable editor.

Suppose a case is created and when analyst make field Private(custom field)  as true, then a duplicate case should get created and assigned to SC-HR team with all values same as current record along with all it's variable. How to achieve it?

3 REPLIES 3

madicherlas
Giga Contributor

Hello,

Create a Ui action on the same table and write a server script to copy fields from existing record to new record.

>>>In your case write a before business rule with condition custom field marked to true and use the below script..<<<

var newRecord = new GlideRecord(current.getTableName()); // Create a new GlideRecord for the current table
newRecord.initialize(); // Initialize the new record

// Loop through all fields of the current record and copy their values
var fields = current.getFields();
for (var i = 0; i < fields.size(); i++) {
var field = fields.get(i);
var descriptor = field.getED();
var fieldName = descriptor.getName();

// Exclude system fields (e.g., sys_id, sys_created_on, sys_updated_on) and empty fields if desired
if (!fieldName.startsWith('sys_') && current[fieldName] != null) {
newRecord[fieldName] = current[fieldName];
}
}

newRecord.insert(); // Insert the new record

 

Please Mark my answer helpful. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Hafsa1 

you can use OOTB Cart API OR CatItem API and set all the variables again on fresh HR case

see these links

How to Submit record producer using script ServiceNow 

Submit the record producer from script 

CatItem - Scoped 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Hafsa1 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader