API: add journal entry to and existing incident

rful011
Kilo Explorer

I have a ruby script that I use to create and update calls.  I have not used the update function for a long time and now I find it does not work.  So far as I can tell there are no errors at the API level (I do check the results of each call 😉

i first search on the short description to see if there is a match:

<p>https://uoatest.service-now.com/incident.do?JSONv2&sysparm_action=getRecords&sysparm_query=active=true^short_description=xxxxx^state!=7^state!=6 </p>

and get back a response

<p>

{"records":[{"u_preferred_contact_method":"email",...,"sys_id":"deb0fb44db8641100acc3ee5f39619bd",...}]}

</p>

I then try and apply the update:

<p> Post https://uoatest.service-now.com/sys_journal_field.do?JSONv2&sysparm_action=insert </p>

with data 

<p>  {"element":"comments","element_id":"deb0fb44db8641100acc3ee5f39619bd","name":"incident","value": ...} </p>

gets

<p>

'{"records":[{"sys_id":"b56c0c901b4e41102b33c05d274bcba8","sys_created_on":"2022-03-01 01:21:19",...} </p>

which is the original record (as I can tell from the "sys_created_on" item.

When I look at the incident in the web browser all I can see is the original entry...

Any ideas what is going on or what I can do it find outt?

 

4 REPLIES 4

rful011
Kilo Explorer

OK so how am I supposed to insert code? OH, I post it into the popup window.  sigh...

Anil Lande
Kilo Patron

Hi,

Please check the below link and see the example of update.

https://docs.servicenow.com/bundle/sandiego-application-development/page/integrate/inbound-other-web...

 

You are using parameter sysparm_action=insert which will create new incident. Instead use the parameter like below:

<p> Post https://uoatest.service-now.com/sys_journal_field.do?JSONv2&sysparm_query=sys_id=deb0fb44db8641100acc3ee5f39619bd&sysparm_action=update</p>

with data 

<p>  {"comments":"Your comments here"} </p>

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

The plot thickens!

first something I should have included before: 

Thanks for your reply Anil!  It was not the correct answer but lead me to investigate some other areas.

First I tried using update with the query.  This behaved exactly the same as before.   Then I made an accidental discovery.  In the web interface I accidentally clicked on "Notes History" instead of "Notes" and there were all my updates!

I changed to program back to its original functionality (but kept all the recoding I had done to improve clarity 😉 and retested.

I think using "update" simply replaces the existing comment rather than adding a new one.

It works as expected modulo that you need to look in history to see the new comment/journal  entries.  This is not satisfactory as the person receiving the call won't know to look there.

When this code was originally written (order of 10 years ago) all the comments were stored in a journal table and you simply added comments by inserting new entries with the element_id of the sys_id of the original call.

At some point in the last couple of years we upgraded to a version that works a bit differently and I had to rework things but I never got the the journal stuff going.  

So I will take a step backwards and explain what I am trying to do:

This is a security monitoring application that logs tickets when it finds problems,  if there is another detection for the same machine I want to record that in the existing ticket by adding a new comment (or work note) rather than loggin a new call.

      call = @sn.find_incident( {:query => "short_description=#{subject}^state!=7^state!=6" })
      if call.records.size > 0 
	$logger.info "existing call #{call.records[0]['number'] } state #{call.records[0]['state']}  #{@global[:no_log] ? ' not logged' : ''} "
        @sn.journal('comments', 'insert', call.sys_id, m) unless @global[:no_log]
      else
        call = @sn.log_incident( {:short => subject, :group => call_group, :comments => m })  unless @global[:no_log]
        if err = call.records[0]['error']
          logger.error "failed to log Incident #{subject} to AskIT: #{err['reason']}"
        else
          $logger.info "new AskIT Incident #{subject}, callnumber: '#{call.records[0]['number']}'" #{@global[:no_log]? ' --suppressed' : call.records[0]['number']} "                                    
	end
      end

 

class ServiceNow::Journal < ServiceNow::Table

  def initialize(sn, field, op, target_id, data )
    @data = data
    post = nil
    query = nil
    case op
    when 'query'
      query = "element_id=#{target_id}"
    when 'insert', 'update'
      post = {element: field, name: 'incident', value: data }
      query = nil
      if op == 'insert'
        post[:element_id] = target_id
      else # update                                                                                                                                                                                      
        query = "element_id=#{target_id}" if op == 'update'
      end
    end
    @records = super(sn, 'sys_journal_field', op,  {post: post, query: query } )
  end
end

I won't include the Table class but it is pretty obvious what it does...

So what should I do to get the entries showing up in Notes, not just History?

 

 

somehow failed to add in the version info: current "Quebec to Rome Patch 4 next week"