Need to fetch date from XML

SupriyaWaghmode
Kilo Sage

Hello Team ,

 

I have below type fetch request body on Rest integration.

 

<m:properties>
<d:personIdExternal>451091</d:personIdExternal>
<d:userId>451091</d:userId>
<d:hiringNotCompleted m:type="Edm.Boolean">false</d:hiringNotCompleted>
<d:isECRecord m:type="Edm.Boolean">true</d:isECRecord>
<d:lastModifiedDateTime m:type="Edm.DateTimeOffset">2020-09-07T16:38:05Z</d:lastModifiedDateTime>
<d:endDate m:null="true"></d:endDate>
<d:bonusPayExpirationDate m:null="true"></d:bonusPayExpirationDate>
<d:createdDateTime m:type="Edm.DateTimeOffset">2017-01-26T11:45:37Z</d:createdDateTime>
<d:employmentId m:type="Edm.Int64">22901</d:employmentId>
<d:regretTermination m:null="true"></d:regretTermination>
<d:createdOn m:type="Edm.DateTime">2017-01-26T11:45:37</d:createdOn>
<d:originalStartDate m:type="Edm.DateTime">2017-02-13T00:00:00</d:originalStartDate>
<d:customString17 m:null="true"></d:customString17>
<d:assignmentClass>ST</d:assignmentClass>
<d:customDate10 m:null="true"></d:customDate10>
<d:lastModifiedBy>452946</d:lastModifiedBy>
<d:okToRehire m:null="true"></d:okToRehire>
<d:assignmentIdExternal>451091</d:assignmentIdExternal>
<d:customString16 m:null="true"></d:customString16>
<d:lastModifiedOn m:type="Edm.DateTime">2020-09-07T16:38:05</d:lastModifiedOn>
<d:createdBy>430019</d:createdBy>
<d:seniorityDate m:type="Edm.DateTime">2017-02-13T00:00:00</d:seniorityDate>
<d:startDate m:type="Edm.DateTime">2017-02-13T00:00:00</d:startDate>
</m:properties>

 

I fetching the details using for loop:

 

 var employement_job = new GlideRecord("u_lr_successfactors_data");
        for (m = 0; m < jsonbodyLoc.feed.entry.length; m++) {

            employement_job.initialize();
            employement_job.u_employee_number = jsonbodyLoc.feed.entry[m].content['m:properties']['d:personIdExternal'];  >>>This  is working
            employement_job.u_employment_start_date = jsonbodyLoc.feed.entry[m].content['m:properties']['d:seniorityDate'];

            employement_job.u_employment_end_date = jsonbodyLoc.feed.entry[m].content['m:properties']['d:endDate'];
            employement_job.insert();
        }
Question: Only date type is retuning [object] [object] in service now stagging table.
 
3 REPLIES 3

Sonam_Tiwari
Kilo Sage

Can you try doing it this way

 

var seniorityDate = jsonbodyLoc.feed.entry[m].content['m:properties']['d:seniorityDate'];

 

employement_job.u_employment_start_date = seniorityDate ? seniorityDate.toString() : null;

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Maddysunil
Kilo Sage

@SupriyaWaghmode 

Could you pls try below script:

var employment_job = new GlideRecord("u_lr_successfactors_data");
for (var m = 0; m < jsonbodyLoc.feed.entry.length; m++) {
    employment_job.initialize();
    employment_job.u_employee_number = jsonbodyLoc.feed.entry[m].content['m:properties']['d:personIdExternal'];
    employment_job.u_employment_start_date = jsonbodyLoc.feed.entry[m].content['m:properties']['d:seniorityDate'].toISOString();
    employment_job.u_employment_end_date = jsonbodyLoc.feed.entry[m].content['m:properties']['d:endDate'] ? jsonbodyLoc.feed.entry[m].content['m:properties']['d:endDate'].toISOString() : null;
    employment_job.insert();
}

Rajdeep Ganguly
Mega Guru


The issue you're facing is due to the fact that the date values are being returned as objects, not as strings. You need to convert these date objects to strings before inserting them into the ServiceNow table. Here's how you can do it:

- Use the JavaScript toISOString() method to convert the date object to a string in the ISO format.

Here's the updated code:

javascript
var employement_job = new GlideRecord("u_lr_successfactors_data");
for (m = 0; m < jsonbodyLoc.feed.entry.length; m++) {
employement_job.initialize();
employement_job.u_employee_number = jsonbodyLoc.feed.entry[m].content['m:properties']['d:personIdExternal'];

// Convert date object to string
var seniorityDate = new Date(jsonbodyLoc.feed.entry[m].content['m:properties']['d:seniorityDate']);
employement_job.u_employment_start_date = seniorityDate.toISOString();

// Convert date object to string
var endDate = new Date(jsonbodyLoc.feed.entry[m].content['m:properties']['d:endDate']);
employement_job.u_employment_end_date = endDate ? endDate.toISOString() : null;

employement_job.insert();
}


Please note:

- The toISOString() method returns a string in the ISO format (YYYY-MM-DDTHH:mm:ss.sssZ). If you need a different format, you may need to use a different method or manually format the date.
- The new Date() constructor may not correctly parse all date string formats. If your dates are not in a format that new Date() can parse, you may need to use a library like moment.js to parse the dates.
- If the d:endDate can be null, you need to check for null before trying to convert it to a date.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER