Create a new record in data source table

Sin
Giga Expert

Hi All,

I have one requirement , whenever I submit a request from catalog item, new record should be created in data source table and attachment from RITM should be copied to the created data source.

Written the below code to copy the attachment from RITM to data source.Here I have hard coded one data source sys_id to copy the attachment,How can I create a new data source for each request in data-source table?

 

(function executeRule(current, previous /*null when async*/) {
var copyAtt = new GlideSysAttachment();
copyAtt.copy('sc_req_item', current.sys_id, 'sys_data_source', '0ec1ab04db8bbf4019d81ffa68961900');
gs.log("test","zero");

})(current, previous);

find_real_file.png

 

find_real_file.png

Need some suggestion Guys!

Thanks in Advance! 

1 ACCEPTED SOLUTION

Thanks Siva !There are some small corrections in your code,below code works perfect!

 

(function executeRule(current, previous /*null when async*/) {

function createDS(){
var dat = new GlideRecord('sys_data_source');
dat.initialize();
dat.name= 'newDSource';
return dat.insert(); 
var dataSource = createDS();

//Copy Attachment
var copyAtt = new GlideSysAttachment();
copyAtt.copy('sc_req_item', current.sys_id, 'sys_data_source', dataSource);
gs.log("test","zero");

})(current, previous);

 

 

 

 

 

 

 

 

 

View solution in original post

3 REPLIES 3

siva_
Giga Guru

You can have a br work for only your catalog item and after insert of that req item you need to glide sys_data_source table and initialize and set up some field values what all are required 

The sample code would be 

 

function createDS(){

var dat = new GlideRecord('sys_data_source');

dat.initialize();

dat.fieldName1= value;

dat.fieldName2 = value;

return dat.insert; // you willl get the glide record object of the data source with sys_id value as areference here

}

 

var dataSource = createDS();

and then using dataSource.sys_id  in your code you specified in the question , you can copy the attachment from the RITM to the created data source 

 

Mark this response as correct if that really helps

Thanks,

Siva

 

Thanks Siva !There are some small corrections in your code,below code works perfect!

 

(function executeRule(current, previous /*null when async*/) {

function createDS(){
var dat = new GlideRecord('sys_data_source');
dat.initialize();
dat.name= 'newDSource';
return dat.insert(); 
var dataSource = createDS();

//Copy Attachment
var copyAtt = new GlideSysAttachment();
copyAtt.copy('sc_req_item', current.sys_id, 'sys_data_source', dataSource);
gs.log("test","zero");

})(current, previous);

 

 

 

 

 

 

 

 

 

You mean to say insert() i didnot use and kept insert?

 

That was not the original code snippet intented to use. Without knowing your field names ,its not possible for me to give you the exact script . I just gave you the pseudo code. 

Anyways if my response drived you to achieve your requirement, thats fine for me even though you donot mark it as correct.  🙂

 

Thanks,

Siva