sidetrak

Service Stack Application Lookup

May 1st, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Name: Service Stack Application Lookup
  3. When: after
  4. Insert: true
  5. Update: true
  6. */
  7.  
  8. //Lookup stack applications
  9. var stackApp = new GlideRecord('u_service_stack_application');
  10. stackApp.addQuery('u_search_table', current.sys_class_name);
  11. stackApp.query();
  12. while(stackApp.next()) {
  13.    if(eval('current.' + stackApp.u_search_field) == stackApp.u_search_text) {
  14.       createRelationship();
  15.    }
  16. }
  17.  
  18. function createRelationship() {
  19.    var app = new GlideRecord(current.sys_class_name);
  20.    app.addQuery('sys_id', current.sys_id);
  21.    app.addQuery(stackApp.u_conditions);
  22.    app.query();
  23.    if(app.next()) {
  24.       var rel1 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
  25.       rel1.addQuery('parent.sys_id', stackApp.u_service_stack.sys_id);
  26.       rel1.addQuery('child.sys_id', app.sys_id);
  27.       rel1.query();
  28.       if(!rel1.next()) {
  29.          gs.addInfoMessage('Creating Relationship for ' + app.name);
  30.          var newRel = new GlideRecord('cmdb_rel_ci');//create relationship
  31.          newRel.initalize();
  32.          newRel.parent = stackApp.u_service_stack.sys_id;
  33.          newRel.child = app.sys_id;
  34.          newRel.type = stackApp.u_relationship_type;
  35.          newRel.insert();
  36.          //if Service Stack Application specifies parent field, create additional relationship
  37.          if(!stackApp.u_parent_ci_field.nil()){
  38.             var rel2 = new GlideRecord('cmdb_rel_ci');//check for existing relationships
  39.             rel2.addQuery('parent.sys_id', app.sys_id);
  40.             rel2.addQuery('child.sys_id', stackApp.u_parent_ci_field.sys_id);
  41.             rel2.query();
  42.             if(!rel2.next()) {
  43.                var newParentRel = new GlideRecord('cmdb_rel_ci');//create relationship with specified type
  44.                newParentRel.initalize();
  45.                newParentRel.parent = app.sys_id;
  46.                newParentRel.child = eval('app.' + stackApp.u_parent_ci_field).sys_id;;
  47.                newParentRel.type = stackApp.u_relationship_type;
  48.                newParentRel.insert();
  49.             }
  50.          }
  51.          
  52.       }
  53.    }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment