Write a fix script to map fields and also populate it

Priyanka_77
Tera Contributor

So, there are 2 tables Basedata Cost center and Cost Center.
There is an after business rule which sets the fields which we receive in the message from ICU into Cost Center table.
Now, I want to write a fix script which will update the 1 yr old records.
So, when we receive a message with the xml which has data in it i need to map that into cost center table.

how can i do this?

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@Priyanka_77 I recommend you to take help from any AI tool like ChatGPT, Gemini to get the desired script. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka_77 

we don't know the complete script and complete business requirement so can't help.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

maheshkhatal
Mega Sage

@Priyanka_77 As I guessed earlier if you are having something in xml you need to parse it, get the respected data values from the xml notes and simple update the target table where filter is set for date is upto 1 year.

If your xml is like <xml><parent><abc>123123243werwer</abc><child>abc_child</child></parent><parent><abc>9234234234234jhk2h4</abc><child>pqr_child</child></parent></xml>. Now you can see this xml has two data points in the parent node. So you need to parse these nodes using javascript api.

If you dig up internet you should able to find. If not let me help you with this.

If my response helped you get some clarity mark this reply as helpful.

Thank you,

Mahesh.

maheshkhatal
Mega Sage

@Priyanka_77 I am assuming certain things here since you have not provided all the required details,

your xml could be like:

 

<CostCenter>
    <CostCenterID>12345</CostCenterID>
    <Name>Marketing</Name>
    <Manager>John Doe</Manager>
    <Budget>500000</Budget>
    <LastUpdated>2023-01-01</LastUpdated>
</CostCenter>

 

 There should be a fix script that processes the XML message and updates the Cost Center table. Take a look athe given reference function you will need to adjust/accomodate as per your requirements:

 

 // Example XML message (this would typically come from an external source)
    var xmlMessage = '<CostCenter>' +
                        '<CostCenterID>12345</CostCenterID>' +
                        '<Name>Marketing</Name>' +
                        '<Manager>John Doe</Manager>' +
                        '<Budget>500000</Budget>' +
                        '<LastUpdated>2023-01-01</LastUpdated>' +
                      '</CostCenter>';

    // Parse the XML message
    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(xmlMessage);

    // Extract data from the XML
    var costCenterID = xmlDoc.getNodeText('//CostCenterID');
    var name = xmlDoc.getNodeText('//Name');
    var manager = xmlDoc.getNodeText('//Manager');
    var budget = xmlDoc.getNodeText('//Budget');
    var lastUpdated = xmlDoc.getNodeText('//LastUpdated');

    // Convert lastUpdated to a GlideDateTime object
    var glideLastUpdated = new GlideDateTime();
    glideLastUpdated.setValue(lastUpdated);

    // Calculate the date 1 year ago from today
    var oneYearAgo = new GlideDateTime();
    oneYearAgo.addYears(-1);

    // Query the Cost Center table for records that are 1 year old
    var costCenterGr = new GlideRecord('cost_center');
    costCenterGr.addQuery('last_updated', '<=', oneYearAgo);
    costCenterGr.addQuery('cost_center_id', costCenterID);
    costCenterGr.query();

    // Update the records
    while (costCenterGr.next()) {
        costCenterGr.name = name;
        costCenterGr.manager = manager;
        costCenterGr.budget = budget;
        costCenterGr.last_updated = glideLastUpdated;
        costCenterGr.update();
    }

    gs.print('Fix Script executed successfully. Records updated.');

 

Mark my response as helpful if it helped resolve your query.

Thank you,

Mahesh.