How to call system property into a REST message

KajolM
Tera Contributor

We are working on ServiceNow integration with Clarizen application and have equirement to to call the system property (which contains APP Key) into Rest Message hearder.

I have added this code -

var r = new sn_ws.RESTMessageV2('Clarizen', 'Get User Subscription Data');
r.setStringParameterNoEscape('clarizen_apikey', gs.getProperty('Clarizen'));, however it is giving me below error:
KajolM_0-1729521669349.png

 

KajolM_1-1729521713612.png

 

{"errorCode":"LoginFailure","message":"You are not authorized to access this service.","referenceId":"1gL04tE1dK87xaLU5gmZMJ"}

Can anybody help me with this? Do I need to create a system property as well?

 

Regards, 

Kajol

2 ACCEPTED SOLUTIONS

ShreyasK0030
Kilo Guru

Hi @KajolM ,
Please check in the sys_properties table that a property with name "Clarizen" exists. If not then create one with the same name and add the API key in the value field. Now you should be able to call the system property in the Rest Message 

View solution in original post

Hello @KajolM,
What I can articulate from what you have mentioned is that, the API is bringing in entire data in the response body ie let's say clarizen already has 1000 records in it's database and those 1000 records were imported into servicenow from API call for the first time. Now suppose 30 new records have been created in Clarizen and you are making an API call again, so this time you need 30 records as those 1000 records have already been imported but Clarizen is sending the data of 1030 records (1000 old + 30 new). And as per your present code it is creating duplicate records for the old records that were already imported. 
 
Solution as changed code : 
We will check in the table if a record with Same user principal name and product exists, if yes then only update its last login time, if the user principal name and product doesn't exist then create a new record on servicenow with different field being populated
 

var
 r = new sn_ws.RESTMessageV2('Clarizen''Get User Subscription Data');
r.setStringParameterNoEscape('clarizen_apikey', gs.getProperty('Clarizen'));

 

 var response = r.execute();
 responseBody = response.getBody();
gs.info("Kajol before parse = " +responseBody);
 var httpStatus = response.getStatusCode();
var obj;
obj = JSON.parse(responseBody);
gs.info("Kajol parsed obj= " + obj);
gs.info("Kajol parsed length= " + obj.entities.length);
gs.info("kajol emails" + obj.entities[0].Email);
for(var i=0; i<obj.entities.length;i++)
{
        var subscriptionGR = new GlideRecord('samp_sw_subscription');
        subscriptionGR.addQuery('user_principal_name', obj.entities[i].Email);
        subscriptionGR.addQuery('product', obj.entities[i].product);
        subscriptionGR.query();
        if(subscriptionGR.next())
           {
              subscriptionGR.last_activity = obj.entities[i].LastLogin;
              subscriptionGR.update();
           }
        else
          {
             subscriptionGR.initialize();
             subscriptionGR.user_principal_name = obj.entities[i].Email;
             subscriptionGR.user = obj.entities[i].Email;
             subscriptionGR.publisher = 'a58d9fbf47181a18ddae7acc416d4324';
             subscriptionGR.product = 'e0fa8c3adb5c5410c41a10825b961931';
             subscriptionGR.software_model = '218d9fbf47181a18ddae7acc416d4325';
             subscriptionGR.last_activity = obj.entities[i].LastLogin;
             subscriptionGR.insert()
          }
}

View solution in original post

5 REPLIES 5

Hi @KajolM ,
can you lemme know if just data import is intended or data update as well ? for imports what fields are supposed to be populated and for updates what fields are supposed to be updated. Moreover can you paste a sample response body for better understanding ?