CA Service Management

Expand all | Collapse all

Concat an auto-incremental ID to CI name

  • 1.  Concat an auto-incremental ID to CI name

    Posted May 25, 2015 11:33 AM

    Hi folks,

     

    Is someone have an idea on how to mimic the ticket reference number allocation on CI?

     

    I want to automatically add a Sequence number at the end of a CI name.

     

    Any idea ?

     

    Thanks.



  • 2.  Re: Concat an auto-incremental ID to CI name

    Posted May 26, 2015 02:42 AM

    Hi,

    I think that you should avoid same named CIs by another method but...

     

    I tried to get all of my CIs by SPEL, query that return 26727 CIs was acquired for 3 seconds.

    05/26 11:25:26.40  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1010 SQL Query has returned (5000) rows for (domsrvr/superuser). Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:27.27  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1010 SQL Query has returned (10000) rows for (domsrvr/superuser). Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:28.18  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1010 SQL Query has returned (15000) rows for (domsrvr/superuser). Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:28.93  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1010 SQL Query has returned (20000) rows for (domsrvr/superuser). Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:29.66  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1010 SQL Query has returned (25000) rows for (domsrvr/superuser). Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:29.93  sqlagt:select8       5976 SIGNIFICANT  prov_base.c           1025 SQL Query has finished (26727) rows for (domsrvr/superuser).  Clause (SELECT ca_owned_resource.last_update_date, ca_owned_resource.own_resource_uuid FROM ca_owned_resource WHERE ca_owned_resource.own_resource_uuid IS NOT NULL) Input (<None>)
    05/26 11:25:29.94  spelsrvr             5200 SIGNIFICANT  TEST                  3 Amount = 26727
    

     

    SPEL code was:

    send_wait(0, top_object(), "call_attr", "nr", "sync_fetch", "STATIC", "id is not NULL", -1, 0);
    logf(SIGNIFICANT,"Amount = %d",msg[1]);
    

     

    So this method can cause system lag.

     

    Another way is to create new table, where will be CI reference and row id (which will be a sequence postfix).

    But here is a lot complexities, how to prevent manual sequence edit and to avoid sequence duplication.



  • 3.  Re: Concat an auto-incremental ID to CI name

    Posted May 26, 2015 09:48 AM

    My first idea was to create a new table with a ID stored in it. So you tell me that i should avoid this plan, but right now we're using a Excel file to keep sequence in it, the technician have to check in the file to get the correct sequence when creating a new CI. Painfull.

     

    So, any advice? Or i'm going with the new table approach?

     

    And, if i look into the ca_owned_resource, there's a field named own_resource_id, if this field was accessible via SpelCode it'll be as easy as 123. I think i will create a SQL trigger to concat this value to the resource_name.



  • 4.  Re: Concat an auto-incremental ID to CI name

    Posted May 26, 2015 10:07 AM

    So if have only few CIs and you keep them actual manually, you can create new attribute or use OOTB field like asset_num.

    Then define unique asset nums for all of your CIs.

     

    Your SPEL basic variant could look like:

        mod file:

    MODIFY nr POST_VALIDATE z_concat_name() 1337;
    

     

         spl file:

    nr::z_concat_name(...)
    {
      if( (!is_null(name)) && (!is_null(asset_num)) ){
        int result;
        result = sindex(name, asset_num);
        if (result==-1) {
          send_wait(0, this, "call_attr", "name", "set_val", format("%s.%s",name,asset_num), "SURE_SET");
        }
      }
    }
    

     

    I also would hide sequence postfix on detail edit but this is optionaly.



  • 5.  Re: Concat an auto-incremental ID to CI name

    Posted May 26, 2015 10:24 AM

    Pretty cool but even if i use this method, there's no mechanism to auto increment my sequence number. And i can't use the first spelCode you provided for the same reason you said, performance issue.

     

    SDM is already generating an unique integer ID in own_resource_id. But i can't find this field in the dataModel, but it's in the database (table ca_owned_resource).

     

     

    And in my case, some CI Class would require an generated suffix ID, but some not. So i'm going for : Create a CI, concat the ca_owned_resource.own_resource_id value to the resource_name via SQL Trigger, only if the boolean value in the CI's class allow me to do so, Disable the resource_name field, based on a checkbox in the CI's class. No sequence edit, no duplicate.

     

    Perhaps this is not the best way to achieve it, but i'm running out of time and idea :/



  • 6.  Re: Concat an auto-incremental ID to CI name
    Best Answer

    Posted May 26, 2015 10:41 AM

    In our environment we haven't any own_resource_id (only own_resource_uuid which is UUID (binary format)).


    Okay, going back to the new table:

    For example call it z_nr_concat;

    Add SREL to nr called "nr";


    Here is another script from my script tray :)


    This will fill z_nr_concat table with CIs, so you can use id from this table.

    nr::z_nr_concat(...)
    {
      object zfound, zobj;
      int zcount, result_id;
    
    
      if(is_null(name)){
      logf(SIGNIFICANT, "Can't proceed. Values are empty.");
      return;
      }
    
    
      send_wait(0, top_object(), "call_attr", "z_nr_concat", "sync_fetch", "STATIC", format("nr=U'%s'", id), -1, 0);
      zcount = msg[1];
    
      if (zcount == 0) {
      object group_leader, new_nr;
      send_wait(0, top_object(), "get_co_group");
      if (msg_error()) {
      logf(ERROR, "New NR: error '%s' on '%s'", msg[0], peristent_id);
      return;
      }
      group_leader = msg[0];
      send_wait( 0, top_object(), "call_attr", "z_gts", "get_new_dob", NULL, NULL, group_leader);
      if (msg_error()) {
      logf(ERROR, "New NR: error '%s' on '%s'", msg[0], peristent_id);
      return;
      }
    
    
      new_nr = msg[0];
      new_nr.nr = id;
      send_wait(0, group_leader, "checkin");
      if (msg_error()) {
      logf(ERROR, "New NR: error '%s' on '%s'", msg[0], peristent_id);
      send_wait(0, group_leader, "uncheck");
      return;
      }
      result_id = new_nr.id;
      } else {
      zfound = msg[0];
      send_wait(0, zfound, "dob_by_index", "DEFAULT", (int)(zcount-1), (int)(zcount-1));
      zobj = msg[0];
      result_id = new_nr.id;
      }
    
      int result;
      result = sindex(name, format(".%d",result_id));
      if (result==-1) {
      send_wait(0, this, "call_attr", "name", "set_val", format("%s.%s",name,result_id), "SURE_SET");
      }
    }
    
    
    

     

     

    This is not tested but if it works you'll get autoincrement that will attach to your name on CI modification.



  • 7.  Re: Concat an auto-incremental ID to CI name

    Posted May 27, 2015 10:18 AM

    Works fine, needed some correction. Thanks a lot.

     

    Here is the working version if someone want it :

     

    I can't figure out how to embbed code, sorry

     

    object zfound, zobj;

    int zcount, result_id;

     

     

    if(is_null(name))

    {

    logf(SIGNIFICANT, "Can't proceed. Values are empty.");

    return;

    }

     

     

    send_wait(0, top_object(), "call_attr", "z_nr_concat", "sync_fetch", "STATIC", format("nr=U'%s'", id), -1, 0);

    zcount = msg[1];

     

    if (zcount == 0)

    {

    object new_nr;

    send_wait(0, top_object(), "get_co_group");

    if (msg_error())

    {

    logf(ERROR, "New NR: error '%s' on '%s'", msg[0], peristent_id);

    return;

    }

     

    group_leader = msg[0];

    send_wait( 0, top_object(), "call_attr", "z_nr_concat", "get_new_dob", NULL, NULL, group_leader);

     

    if (msg_error())

    {

    logf(ERROR, "New NR: error '%s' on '%s'", msg[0], persistent_id);

    return;

    }

    new_nr = msg[0];

    new_nr.nr = id;

    send_wait(0, group_leader, "checkin");

     

    if (msg_error())

    {

    logf(ERROR, "New NR: error '%s' on '%s'", msg[0], peristent_id);

    send_wait(0, group_leader, "uncheck");

    return;

    }

    result_id = new_nr.id;

    }

    else

    {

    zfound = msg[0];

    send_wait(0, zfound, "dob_by_index", "DEFAULT", (int)(zcount-1), (int)(zcount-1));

    zobj = msg[0];

    result_id = zobj.id;

    }

     

    int result;

    result = sindex(name, format("-%d",result_id));

    if (result==-1)

    {

    name=format("%s-%d",name,result_id);

    }



  • 8.  Re: Concat an auto-incremental ID to CI name

    Posted May 27, 2015 10:22 AM

    sounds great :)

    you need to switch to advanced editor to embbed code,

    also I reccomend to use SET_VAL except of simple "=".

    send_wait(0, this, "call_attr", "name", "set_val", format("%s-%s",name,result_id), "SURE_SET");




  • 9.  Re: Concat an auto-incremental ID to CI name

    Posted May 27, 2015 10:26 AM

    Modified. Thanks.

     

    Advance mode? Where?



  • 10.  Re: Concat an auto-incremental ID to CI name

    Posted May 27, 2015 11:00 AM

    have searched for this function for a long time ))

    ps:sorry for interface translation it cann't be switched



  • 11.  Re: Concat an auto-incremental ID to CI name

    Posted May 27, 2015 11:07 AM
    Oh Okay you can't access this advanced editor when answering from the home page :)
    Thank you!