Custom application - UI Action to copy a record

sharonjanisch
Tera Contributor

Trying to make a UI Action in our custom application that will copy some fields from an existing record into a new record.   We have similar UI Actions for Incident and Change that work properly but I can't get this working.     I've tried changing the new GlideRecord parameter to various values but so far no luck.       My script is included below.     Can anyone tell me what I'm doing wrong here or if this has to be handled differently in a scoped application?     Thanks.

new GlideRecord('x_kamr_sec_ex_security_exceptions');

new GlideRecord(current);

new GlideRecord('security_exception');

current.update();

var excnew = new GlideRecord('security exception');

excnew.description = current.description;

excnew.short_description = current.short_description;

excnew.update();  

action.setRedirectURL(excnew);

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Sharon,



You are close. current refers to the current record you are displaying, so you need to instantiate another record/object of that same type.



var newRec = new GlideRecord(current.getTableName());



the initialize it/set default values:



newRec.newRecord();



Copy the fields you wish



newRec.short_description = current.short_description;


newRec.description = current.description;


// Add additional fields as needed here



Save the new record



newRec.update();



and go to the new record form



action.setRedirectURL(newRec);


View solution in original post

9 REPLIES 9

Thanks for the update Sharon. Shame on me for suggesting the legacy gs.log() method.



Use gs.info() instead.



To ensure you get output saved to the log table, create two debug properties for your scoped app. Type sys_properties.list in the navigation filter and create two new entries.



Name: logging.verbosity


Description: Amount of debugging to view


Type: string


Value: debug



Name: logging.destination


Description: Location to log


Type: string


Value: db



When you insert the gs.info() statements, take a look in System Logs> Script Log Statements. Change the filter from Source = ***Script to Source = your_scope_name.


No problem at all.     Your original answer for my code was correct.   I just had a typo in it that I kept missing and your answer about the gslog was enough for me to find what I needed to track it down.     I will try out your suggestion for the debug properties on my scoped application and see how that goes.     Thanks again.


Hi Chuck,

thanks for this solution!  Just what I needed.  One thing I wanted to ask, my custom app record has 30+ fields...is there a clever script I can run to copy them all without needing to use individual script lines like these?

 

newRec.short_description = current.short_description;

newRec.description = current.description;

You could use an array of field names and then use that to get/set values. Something like this:

var fieldList = ['name', 'short_description', 'user'];

for (var i = 0; i < fieldList.length; i++) {
  var f = fieldList[i];

  // getValue(f) retrieves the other record's field value
  // setValue(f, value) sets the current records field to that value
  current.setValue(f, gr.getValue(f));
}

Add field names to the list as needed. Change current & gr to your variables.

Thanks Chuck!  

If I'm understanding correctly, I would still need to enter every field into the script, correct?  My goal is to not need to update the script if new fields are added to my scoped app table, but rather for the script to just look for all the existing fields and then copy them into the new record.

Is this possible?  Thanks again!