CA Service Management

 View Only

Expand all | Collapse all

How can I access values entered in the properties of a ticket on the screen in order to perform validations? How can I set values in the fields of the cr object based on the values of the properties?

  • 1.  How can I access values entered in the properties of a ticket on the screen in order to perform validations? How can I set values in the fields of the cr object based on the values of the properties?

    Posted 21 days ago
    Edited by Jason McClellan 18 days ago

    Is it possible to assign a value to a referenced field based on the values entered in the properties?

    I want to use a value entered in a property to search for a Configuration Item (CI) that contains this string in its name. The goal is to prevent the ticket from being saved if there is no CI with the value entered in the property or, if it exists, to fill in the field with that value.

    I tried to do this in several ways:

    1. Create a cr:: method triggered (i tryed PRE_VALIDATE/POST VALIDATE/PRE_CI), access the properties using the get_properties call, and with the property value, insert it into the field using the "set_val" call.
    Error: the properties have not yet been saved to the database at the time of saving the ticket, so this method only works on request edit.

    1.1 POST_CI -> Error when setting affected_resource: AHD03025: Attempt to modify non_CO value

    2. Update the CI field through a cr_prp:: trigger,
    Ex .mod 

    MODIFY cr PRE_CI  z_busca_ic_pelo_valor_da_prop(persistent_id) 1750 FILTER ( ( EVENT("INSERT") ) || ( EVENT ("UPDATE") )  );


    Error: Error when setting affected_resource: AHD03025: Attempt to modify non_CO value

    2.1. Through a cr_prp trigger that calls a cr method, passing as a parameter the CI id I want to assign to the field and updating it via the "set_val" call
    Error: Error when setting affected_resource: AHD03025: Attempt to modify non_CO value

    2.2. Also by calling a cr:: method inside cr_prp::, using the "update_object" call in two different ways like this link (update_object)
    Neither "udate_object" approachs works: the first method does not update the field, and the second method causes the SDM server to crash.

    SDM Version: 17.4.2.0

    Below are examples of the code I tried to implement:

    EXAMPLE 1

    //1
    cr::z_search_ic_by_prop_value(...)
    {
        logf(SIGNIFICANT, "-----------entered z_search_ic_by_prop_value-------------------------------------");
        string prop_name, prop_value, where_clause, ic_id;
        int count, i;
        object props_list, prop, ic_list, ic_obj;
    
        // get the ticket properties
        send_wait(0, top_object(), "call_attr", "api", "get_properties", persistent_id);
        logf(SIGNIFICANT, "first sendwait = %s", persistent_id);
        if (!msg_error())
        {
            props_list = msg[0];
            count = msg[1];
    
            // iterate over the ticket properties
            for (i = 0; i < count; i++)
            {
                // call to access the i-th property in the list
                send_wait(0, props_list, "dob_by_index", "DEFAULT", i, i);
                logf(SIGNIFICANT, "second sendwait");
    
                prop = msg[0];
                prop_name = prop.label;
                prop_value = prop.value;
                logf(SIGNIFICANT, "prop_name = %s", prop_name);
                logf(SIGNIFICANT, "prop_value = %s", prop_value);
                if (prop_name == "Patrimonio Prodabel" && prop_value != "")
                {
                    // Build the where clause to search the IC by name
                    where_clause = format("name = '%s'", prop_value);
    
                    // Search the IC in the nr (network resource) table
                    send_wait(0, top_object(), "call_attr", "nr", "sync_fetch", "STATIC", where_clause, -1, 0);
                    logf(SIGNIFICANT, "The search '%s' was performed in the 'nr' table", where_clause);
                    string error_text;
                    if (!msg_error())
                    {
                        ic_list = msg[0];
                        if (msg[1] > 0)
                        {
                            // Get the first IC from the list of found ICs
                            send_wait(0, ic_list, "dob_by_index", "DEFAULT", 0, 0);
                            logf(SIGNIFICANT, "fourth sendwait");
    
                            ic_obj = msg[0];
    
                            // Get the IC id
                            send_wait(0, ic_obj, "get_attr_vals", 1, "id");
                            if (!msg_error())
                            {
                                ic_id = msg[3];
    
                                // Set the affected_resource field of the ticket using set_attr and the IC id
                                send_wait(0, this, "call_attr", "affected_resource", "set_val", ic_id, "SURE_SET");
                                if (msg_error())
                                {
                                    logf(ERROR, "Error setting affected_resource: %s", msg[0]);
                                }
                            }
                            else
                            {
                                logf(ERROR, "Error getting IC id: %s", msg[0]);
                            }
                        }
                        else
                        {
                            error_text = format("Could not find an IC for Asset %s", prop_value);
                            logf(ERROR, error_text);
                            set_error(1);
                            set_return_data(error_text);
                            return;
                        }
                    }
                    else
                    {
                        error_text = format("The call returned an error for prop_value %s", prop_value);
                        logf(ERROR, error_text);
                        set_error(1);
                        set_return_data(error_text);
                        return;
                    }
                }
            }
        }
    
        logf(SIGNIFICANT, "-----------exited z_search_ic_by_prop_value-------------------------------------");
    };
    

    *------------------------------------------------------------------------*

    EXAMPLE 2

    cr_prp::z_obter_valor_das_prop(...)
    // cr_prp::z_get_prop_values(...)
    {
        logf(ERROR, "------------------ ENTERED z_get_prop_values --------------");
        object tickets_list, ticket_object;
        string where_clause;
        int found;
        found = 0;
    
        // Search only by the field persistent_id = owning_cr
        logf(SIGNIFICANT, "Entered attempt: persistent_id = owning_cr");
        where_clause = format("persistent_id = '%s'", owning_cr);
        logf(SIGNIFICANT, "where %s", where_clause);
    
        send_wait(0, top_object(), "call_attr", "cr", "dob_by_persid", 0, owning_cr);
        if (!msg_error())
        {
            logf(SIGNIFICANT, "Found using persistent_id = owning_cr! Result: %s", msg[0]);
        }
        ticket_object = msg[0];
    
        // Search the CI (Configuration Item) by name equal to the asset value
        object cis_list, ci_object;
        string ci_where, ic_id;
        ci_where = format("name = '%s'", value);
        send_wait(0, top_object(), "call_attr", "nr", "sync_fetch", "STATIC", ci_where, -1, 0);
        if (!msg_error() && msg[1] > 0)
        {
            logf(SIGNIFICANT, "CI found for value: %s", value);
            cis_list = msg[0];
    
            // Get the CI object
            send_wait(0, cis_list, "dob_by_index", "DEFAULT", 0, 0);
            if (!msg_error())
            {
                ci_object = msg[0];
    
                // Get the persistent_id of the CI
                send_wait(0, ci_object, "get_attr_vals", 1, "id");
                if (!msg_error())
                {
                    ic_id = msg[3];
                    logf(SIGNIFICANT, "IC id: %s", ic_id);
                    ic_id = format("nr:%s", ic_id);
                    cr::z_seta_ic(ic_id, owning_cr);
                    // send_wait(0, ticket_object, "call_attr", "affected_resource", "set_val", ic_id, "SURE_SET");
                }
                else
                {
                    logf(ERROR, "Error getting IC id: %s", msg[0]);
                }
            }
            else
            {
                logf(ERROR, "Error getting CI object: %s", msg[0]);
            }
        }
        else
        {
            logf(ERROR, "IC not found for value: %s", value);
        }
        logf(ERROR, "------------------ EXITED z_get_prop_values --------------");
    }
    
    cr::z_seta_ic(string ic_id2, string cr_id)
    // cr::z_set_ic(string ic_id2, string cr_id)
    {
        logf(ERROR, "---------------ENTERED Z_SET_IC-----------------");
    
        object ticket;
        uuid who;
        object group_leader;
    
        // Search the ticket by persistent_id
        send_wait(0, top_object(), "call_attr", "cr", "dob_by_persid", 0, cr_id);
        ticket = msg[0];
        logf(SIGNIFICANT, "ticket: %s", ticket.description);
        logf(SIGNIFICANT, "ticket persid: %s", ticket.persistent_id);
        logf(SIGNIFICANT, "ticketid: %s", ticket.id);
    
        // Get the current user
        send_wait(0, top_object(), "call_attr", "cnt", "current_user_id");
        who = msg[0];
        logf(SIGNIFICANT, "who: %s", who);
    
        send_wait(0, top_object(), "get_co_group");
        if (msg_error())
        {
            logf(ERROR, "Error getting group_leader: %s", msg[0]);
            return;
        }
        group_leader = msg[0];
        logf(SIGNIFICANT, "group_leader: %s", group_leader);
    
        // Update the affected_resource field using update_object
        // I tried to set the summary field, which is a simple text field, and I was not successful either
    
        // THIS WAY DONT THROW MESSAGE ERROR
        send_wait(0, top_object(), "call_attr", "api", "update_object", who, "", ticket, group_leader, 0, "summary", "summary UPDATED, TESTE DA SILVA, DESCRIPTION 123");
        if (!msg_error())
        {
            logf(SIGNIFICANT, "seems it worked 1");
        }
        else
        {
            logf(SIGNIFICANT, "error 1 %s", msg[0]);
        }
    
        // THIS WAY STOPS SDM
        // send_wait(0, top_object(), "call_attr", "api", "update_object", who, cr_id, NULL, group_leader, 0, "cr.summary", "summary UPDATED, TESTE DA SILVA, DESCRIPTION 123");
        // if (!msg_error())
        // {
        //     logf(SIGNIFICANT, "seems it worked 2");
        // }
        // else
        // {
        //     logf(SIGNIFICANT, "error 2 %s", msg[0]);
        // }
    
        logf(ERROR, "------------------EXITED Z_SET_IC--------------");
    }
    



    -------------------------------------------



  • 2.  RE: How can I access values entered in the properties of a ticket on the screen in order to perform validations? How can I set values in the fields of the cr object based on the values of the properties?

    Posted 2 days ago

    Someone have any suggestion?

    -------------------------------------------