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.

Trigger Import set from business rule

amtg
Kilo Contributor

I have a scoped application 'itfm' which has a table say 'itfm_table'. whenever a record is inserted in this table, i need to trigger an import set that imports the data in an excel file into some other table which is currently in global scope. The question is :

1> How to bring a servicenow scoped application into my personal scope which has another application.

2> How to trigger an import set and associated transform maps on insert of a record in my scoped application. Can a After Insert business rule trigger an import set. do i need to create the import set in my personal scoped application to trigger that from a business rule in my scoped table.

7 REPLIES 7

Chuck Tomasi
Tera Patron

Hi Amit,



You may be able to do this via the Import Set API. I haven't tried it and don't know what the cross scope issues might be, but it's the easiest way I know of to get information in to the import set table and transformed.



Import Set API - POST /now/import/{tableName}


Hi Chuck,



I am trying to write the below business rule code :




      //gs.setProperty('itfm.poc.import_plan', budget);


      var grProp = new GlideRecord('sys_properties');


      grProp.get('a759ea87dbddee00785c36fffe9619c9');


      grProp.value = current.sys_id;


      grProp.update();


     


      // attach sheet to data source


      var grDS = new GlideRecord('sys_data_source');


      grDS.addQuery('name', 'Collection Sheet');


      grDS.setLimit(1);


      grDS.query();


     


      if (grDS.next()) {              


              var dataSourceID = grDS.getValue('sys_id');


             


              // Delete existing attachments


              var gr2 =   new GlideRecord('sys_attachment');


              gr2.addQuery('table_name', 'sys_data_source');


              gr2.addQuery('table_sys_id', dataSourceID);


              gr2.deleteMultiple();


             


              // Copy attachment from change to data source


              GlideSysAttachment.copy('x_swre_itfm_import_collection_sheet', current.getValue('sys_id'), 'sys_data_source', dataSourceID);


             


      } else {


              gs.error('Could not find data source');


              return;


      }




      var grSIS = new GlideRecord ('scheduled_import_set');


     


      if (grSIS.get('44a36e83dbddee00785c36fffe961954')) {              


              SncTriggerSynchronizer.executeNow(grSIS);      


              gs.addInfoMessage('The records are inserted');      


      }        


     



Getting the Below two errors :




Write operation against 'sys_properties' from scope 'x_swre_itfm' has been refused due to the table's cross-scope access policy


Delete operation against 'sys_attachment' from scope 'x_swre_itfm' has been refused due to the table's cross-scope access policy



can you help me correcting this code or with some sample alternative code?



That's the cross scope access doing it's job. That's the protection that keeps scoped apps from stomping on each other (or global apps.)



Think hard about this before you do it... The answer is to open up access to the related global tables. This could pose a security risk from other scoped apps on your system.



Navigate to System Definition> Tables. Locate and find the sys_properties table and open that record.



In the section "Application Access" check the Can update box and your script will be able to write the properties there.



Do the same for delete against sys_attachment.



find_real_file.png


HI Chuck,



I considered this option but since it would be a global scope change, i did not do it. Instead, i created a new property in my application scope and tried to set the property 'value'. The errors i was getting previously are fixed now. Two issues i am facing are:


1> there are 2 records being created in my target table, one with record producer variable values and the other with all blank fields and just the attached excel file. I believe somewhere in above script i would be inserting a new record with just the attachment. not sure where.


2> The import set was not triggered and business rule giving the below error:



java.lang.SecurityException: SncTriggerSynchronizer is not allowed in scoped applications


  Caused by error in Business Rule: 'Import' at line 38



  35: var grSIS = new GlideRecord ('scheduled_import_set');


  36:  


  37: if (grSIS.get('44a36e83dbddee00785c36fffe961954')) {  


==> 38: SncTriggerSynchronizer.executeNow(grSIS);  


  39: gs.addInfoMessage('The records are inserted');  


  40: }



can you suggest something to fix these two issues?