Topic
20 replies Latest Post - ‏2013-05-20T05:56:08Z by DonaldN
raj_kgdr
raj_kgdr
62 Posts
ACCEPTED ANSWER

Pinned topic Action Validation Hook

‏2013-04-23T11:21:01Z |

 

Dear Experts,

 

I am new to ClearQuest scripting/hooks. So Please help me.

I was trying to edit a Action validation hook in ClearQuest in which:

A parent record before it goes to the closed state should check for a condition where the state of it's Child record should be closed. If the child record is not closed, then the action "close" of the parent CR should not pass

Below is the script which I have used and which is not passing through.

If GetFieldValue("CR_Child").GetFieldValue("State").GetValue != "Closed" Then

cr_validation = "Child CR is not closed. The same must be done prior to closing the parent CR"

End If

 

But the above doesn't get validated and I am not able to proceed further?

Am I missing out something here? 

 

Thanks,

Raj

  • pdubovitsky
    pdubovitsky
    376 Posts
    ACCEPTED ANSWER

    Re: Action Validation Hook

    ‏2013-04-23T19:23:16Z  in response to raj_kgdr

    What is a type of "CR_Child" field? Is it a REFERENCE or a REFERENCE_LIST?

    Pavel

    • raj_kgdr
      raj_kgdr
      62 Posts
      ACCEPTED ANSWER

      Re: Action Validation Hook

      ‏2013-04-24T01:50:13Z  in response to pdubovitsky

      "CR_Child" is a reference list.

      • pdubovitsky
        pdubovitsky
        376 Posts
        ACCEPTED ANSWER

        Re: Action Validation Hook

        ‏2013-04-24T15:10:35Z  in response to raj_kgdr

        You cannot use reference list to access children in the same way as you use reference.

        There are two options:

        1) (preferred in most cases) run a query for child records with parent ID of the current record ID and not in "Closed" state. If this query returns any record, your condition is not satisfied, or

        2) you can use GetValueAsList method on the reference_list field and check in the loop state of all children by getting child entities using "GetEntity" API call.

        Pavel

        • raj_kgdr
          raj_kgdr
          62 Posts
          ACCEPTED ANSWER

          Re: Action Validation Hook

          ‏2013-04-25T02:20:25Z  in response to pdubovitsky

          Hi Pavel,

          Thanks for coming back on this. As I said earlier, I am new to this.

          Can you explain with an example please?? may be the preferred case.

           

          Thanks,

          Raj

          • raj_kgdr
            raj_kgdr
            62 Posts
            ACCEPTED ANSWER

            Re: Action Validation Hook

            ‏2013-04-25T03:33:37Z  in response to raj_kgdr

            Hi Pavel,

            I managed to write the below code and again failed to validate:

             

            set session = GetSession
            set querydef = session.BuildQuery("CR_ID")
            querydef.buildfield("CR_Child")
            set operator = querydef.BuildFilterOperator(AD_BOOL_OP_AND)
            operator.BuildFilter "state", AD_COMP_OP_EQ, "CLOSED"
            set resultset = session.BuildResultSet(querydef)
            resultset.execute
            status1 = resultset.MoveNext
             
            if status1 <> AD_NO_DATA_FOUND then
            Do while status1 = AD_SUCCESS
            cr_validation = "Child CR is not closed. The same must be done prior to closing the parent CR"
            End If

             

             

            • pdubovitsky
              pdubovitsky
              376 Posts
              ACCEPTED ANSWER

              Re: Action Validation Hook

              ‏2013-04-25T18:45:28Z  in response to raj_kgdr

              Hi,

              1. set querydef = session.BuildQuery("CR_ID")
                "CR_ID" - is it your record name? (based on the validation variable name, it should be called "CR")
                 
              2. Also, you need to add a condition for the parent ID field (backreference of the CR_Child). It should be equal to the current record ID.
                 
              3. It is better to test for child records that are not in the "Closed" state, i.e.
                operator.BuildFilter "state", AD_COMP_OP_NEQ, "CLOSED"
                and
                 
              4. test resulset in the following way:
                resultset.execute
                if resultset.MoveNext = AD_SUCCESS then
                  cr_validation = "Child CR is not closed. The same must be done prior to closing the parent CR"
                end if

              Pavel

              • raj_kgdr
                raj_kgdr
                62 Posts
                ACCEPTED ANSWER

                Re: Action Validation Hook

                ‏2013-04-26T04:26:06Z  in response to pdubovitsky

                Hi Pavel,

                Again Thanks. I followed your instruction and I think I am getting nearer.

                But I see the following now:

                Under the Parent record type: CR

                I have a field called CR_Child as a type: reference list and which has a back reference to CR

                ("CR_Child" is also a State-Full record type, just like the Parent record type "CR")

                But When I execute the below code, I get an error saying "Validation Hook Failed, with the specified error message". I have closed the associated ChildCR's though.

                 

                set session = GetSession
                set querydef = session.BuildQuery("CR_Child")
                querydef.buildfield("ChildCR_ID")
                querydef.buildfield("state")
                set operator = querydef.BuildFilterOperator(AD_BOOL_OP_AND)
                operator.BuildFilter "state", AD_COMP_OP_NEQ, "CLOSED"
                set resultset = session.BuildResultSet(querydef)
                resultset.execute
                if resultset.MoveNext = AD_SUCCESS then
                CR_Validation = "Close the child CR's"
                End If

                Please help!

                 

                Thanks,

                Raj

                 

                • raj_kgdr
                  raj_kgdr
                  62 Posts
                  ACCEPTED ANSWER

                  Re: Action Validation Hook

                  ‏2013-04-26T05:35:53Z  in response to raj_kgdr

                  following the above with the same back reference condition as explained, I still aint reaching my Goal.

                   

                  set sessionObj = GetSession
                  CRdbid = GetFieldValue("dbid").GetValue()
                  Set entity = sessionObj.GetEntity("CR", CRdbid)
                  a = entity.GetFieldValue("CR_ID").GetValue()
                  set querydef = sessionObj.BuildQuery("CR_Child")
                  querydef.buildfield ("dbid")
                  querydef.buildfield("ChildCR_ID")
                  querydef.buildfield("state")
                  set operator = querydef.BuildFilterOperator(AD_BOOL_OP_AND)
                  operator.BuildFilter "ChildCR_ID", AD_COMP_OP_LIKE, "a"
                  operator.BuildFilter "state", AD_COMP_OP_NEQ, "CLOSED"
                  set resultset = sessionObj.BuildResultSet(querydef)
                  resultset.execute
                  if resultset.MoveNext = AD_SUCCESS then
                  CR_Validation = "Close the child CR's"
                  End If

                   

                  Please please help.

                  • MartinThompson
                    MartinThompson
                    4 Posts
                    ACCEPTED ANSWER

                    Re: Action Validation Hook

                    ‏2013-04-26T18:40:31Z  in response to raj_kgdr

                    It looks like you are close.

                    We use something like this:

                    count = 0
                      ResultSet.EnableRecordCount
                      ResultSet.Execute
                      count = ResultSet.RecordCount
                      IF count > 0 THEN

                    • raj_kgdr
                      raj_kgdr
                      62 Posts
                      ACCEPTED ANSWER

                      Re: Action Validation Hook

                      ‏2013-04-29T11:01:51Z  in response to MartinThompson

                      Hi Martin/Pavel,

                      I am lost now. I don't know how do I fix this?

                      Please help. I am unable to figure it out.

                       

                      Thanks,

                      Raj

                       

                       

                      • MartinThompson
                        MartinThompson
                        4 Posts
                        ACCEPTED ANSWER

                        Re: Action Validation Hook

                        ‏2013-04-29T14:16:56Z  in response to raj_kgdr

                        Are you getting any errors, can you describe how it fails?

                        • raj_kgdr
                          raj_kgdr
                          62 Posts
                          ACCEPTED ANSWER

                          Re: Action Validation Hook

                          ‏2013-04-30T02:57:10Z  in response to MartinThompson

                          yeah I get an error pop up which reads like:

                          CRMUD0055E The entity's Validation Hook failed, the error message is:

                           Close the child UCR's

                          The Child UCR is already closed though.

                          But the interesting fact here is, for the new CR's the hook is working, but for some of the old ones it is not.

                          I am using a CQ version 8.0.0

                           

                          Thanks,

                          Raj

                           

                           

                          • DonaldN
                            DonaldN
                            214 Posts
                            ACCEPTED ANSWER

                            Re: Action Validation Hook

                            ‏2013-04-30T03:53:28Z  in response to raj_kgdr

                            Hi Raj,

                            Seeing so many errors and mistakes in your code, I would suggest you do a couple of things.
                            1. Read the API reference of every single function that you intend to use. Pay attention to what it does, what parameters it accepts, and what value it returns. You can do a search in the IBM InfoCenter. There are also many sample codes there for your reference.
                            http://pic.dhe.ibm.com/infocenter/cqhelp/v8r0m0/topic/com.ibm.rational.clearquest.apiref.doc/topics/default_topic.htm
                            2. Construct the query first in ClearQuest client. Only when you get the result that you want, you implement the same logic in the code.

                            Once you learn all the basics, you feel more confident to do the coding and less likely get lost.

                            Now here are the things in your code that you can consider.
                            1. You don't need to get the entity first before getting the field "CR_ID". You are within the entity so it works the same way as "dbid", i.e. GetFieldValue("CR_ID").GetValue() is sufficient.
                            2. You are querying the "CR" record type/entity, so BuildQuery("CR"). When Pavel asked about the recor type name, you need to think about why he asked.
                            3. "ChildCR_ID.state" should be tested, not "state". You are trying to filter the child record's state, not the record's own state. So you should use BuildFilter "ChildCR_ID.state" ...
                            4. You missed the condition to limit the query result to the current record, which Pavel already pointed out. In other words, you should have a filter "CR_ID equals a".

                            If you ask why you need to write the query this way, build and run the query in ClearQuest client and see for yourself.

                            Hope that helps.

                            • raj_kgdr
                              raj_kgdr
                              62 Posts
                              ACCEPTED ANSWER

                              Re: Action Validation Hook

                              ‏2013-04-30T04:58:46Z  in response to DonaldN

                              Hi All who helped me to understand the basics in Hooks as I am newbie :-)

                              Mr Donald I today learnt a lesson from you that whenever you want to write a hook in terms of queries, then 1st run a query in the GUI mode. I did that today and myself see the difference.

                              Pavel thanks to you as you helped me to get a Go.

                              Martin thanks to you too sir.

                              The final bit of code is here: (I am not sure if I am good here still, but this worked for me and I am happy for it)

                               

                              Set sessionObj = GetSession
                              CRdbid = GetFieldValue("dbid").GetValue()
                              a = GetFieldValue("CR_ID").GetValue()
                              set querydef = sessionObj.BuildQuery("CR")
                              querydef.buildfield ("dbid")
                              querydef.buildfield("CR_ID")
                              querydef.buildfield("CR_Child.State")
                              set operator = querydef.BuildFilterOperator(AD_BOOL_OP_AND)
                              operator.BuildFilter "CR_ID", AD_COMP_OP_LIKE, a
                              operator.BuildFilter "CR_Child.State", AD_COMP_OP_NEQ, "Closed"
                              set resultset = sessionObj.BuildResultSet(querydef)
                              resultset.execute
                              If resultset.MoveNext = AD_SUCCESS then
                              CR_Validation = "Close the child CR's"
                              End If
                               
                              count = 0
                                ResultSet.EnableRecordCount
                                ResultSet.Execute
                                count = ResultSet.RecordCount
                                If count > 0 THEN
                              CR_Validation = "Close the child CR's"
                              End If
                              Updated on 2013-04-30T07:48:25Z at 2013-04-30T07:48:25Z by raj_kgdr
                              • raj_kgdr
                                raj_kgdr
                                62 Posts
                                ACCEPTED ANSWER

                                Re: Action Validation Hook

                                ‏2013-05-14T09:51:23Z  in response to raj_kgdr

                                Actually I see one more problem here.

                                The Parent CR is not getting closed if the child CR's are not in closed state. This objective is met with the above code. However the validation hook also gets applied for a stand alone Parent CR, meaning if there are no child CR's associated with a parent CR, the error message still pop's up with the validation message.

                                Is there a way to overcome this?

                                 

                                Thanks

                                • DonaldN
                                  DonaldN
                                  214 Posts
                                  ACCEPTED ANSWER

                                  Re: Action Validation Hook

                                  ‏2013-05-14T23:56:55Z  in response to raj_kgdr

                                  Hi Raj,

                                  If you still use the codes that you posted as above, then you may want to first block of testing the result set, and leave the second one (at the bottom) as is. You actually run the query twice and use two methods to test whether the query returns at least one record. If the problem still exists, you need to enable tracing and see what's going on.

                                  • raj_kgdr
                                    raj_kgdr
                                    62 Posts
                                    ACCEPTED ANSWER

                                    Re: Action Validation Hook

                                    ‏2013-05-15T09:53:37Z  in response to DonaldN

                                    HI Donald,

                                    I did a round of check.

                                     

                                    With the code that I have posted above, I see only a partial success or you can say I still failed.
                                    Meaning the query runs, but runs on the entire database to search for Child records which are not in closed state.

                                    So when there is even a single child record still in Open or On_Hold state, when you try to close the stand alone Parent record (without any child records), I get a Validation Hook message.

                                    Is there a way I can limit the query to run only on the current Parent record.? i.e., to search for the associated child records only for the current parent record which is open.
                                     
                                    Thanks,
                                    Raj
                                    • DonaldN
                                      DonaldN
                                      214 Posts
                                      ACCEPTED ANSWER

                                      Re: Action Validation Hook

                                      ‏2013-05-16T00:30:14Z  in response to raj_kgdr

                                      Hi Raj,

                                      It is quite difficult to say why the query failed without knowing the details of your ClearQuest schema. Have you tested the query in the ClearQuest client first? Check the result set and you should be able to figure out what filter to add/change in order to filter out the unwanted records there.

                                      For your posted codes, I would suggest making the change

                                      operator.BuildFilter "CR_ID", AD_COMP_OP_LIKE, a

                                      to

                                      operator.BuildFilter "dbid", AD_COMP_OP_EQ, CRdbid

                                      This should limit the query scope to the current record only.

                                      Hope it helps.

                                      • raj_kgdr
                                        raj_kgdr
                                        62 Posts
                                        ACCEPTED ANSWER

                                        Re: Action Validation Hook

                                        ‏2013-05-16T08:59:17Z  in response to DonaldN

                                        Hi Donald,

                                        I guess a trick and playing around the code worked for me.

                                        1st off the validation hook has to edited in the parent record(we were right here) but the build query should be done on the child record unlike the parent itself as we did in the past.

                                        Something like:

                                        set querydef = sessionObj.BuildQuery("CR_Child")

                                        With this I am able to achieve my desired objective, I still haven't tested it to the full though, but I strongly believe this should be it.

                                        And again if you find anything just any small thing which you think is wrong, please do not hesitate to point it out.

                                        After all I have started my learning here.

                                        So my final bit of code is as below:

                                         

                                        Set sessionObj = GetSession
                                         
                                        CRdbid = GetFieldValue("dbid").GetValue()
                                        Set entity = sessionObj.GetEntity("CR", CRdbid)
                                        a = entity.GetFieldValue("CR_ID").GetValue()
                                        set querydef = sessionObj.BuildQuery("CR_Child")
                                        querydef.buildfield ("dbid")
                                        querydef.buildfield("ChildCR_ID")
                                        querydef.buildfield("State")
                                        set operator = querydef.BuildFilterOperator(AD_BOOL_OP_AND)
                                        operator.BuildFilter "ChildCR_ID", AD_COMP_OP_LIKE, a
                                        operator.BuildFilter "State", AD_COMP_OP_NEQ, "Closed"
                                        set resultset = sessionObj.BuildResultSet(querydef)
                                        resultset.execute
                                        If resultset.MoveNext = AD_SUCCESS then
                                        CR_Validation = "Close the child CR's"
                                        End If

                                         

                                         

                                        • DonaldN
                                          DonaldN
                                          214 Posts
                                          ACCEPTED ANSWER

                                          Re: Action Validation Hook

                                          ‏2013-05-20T05:56:08Z  in response to raj_kgdr

                                          Hi Raj,

                                          The query does not look correct to me. If the parent record and child record are of different record types, it's better you list the record type names and which fields are used to reference/link to the other record before further discussion.

                                          To start with, you should have a query that can consistently display all the child records, and only these records, of a particular parent record. Then you can a more filter on the "state" field to filter out some of the child records.