help needed in scripting

tanz
Tera Expert

Hi, I have an table A where i have few records

 

itemtype   Qty    number     date of purchase  RITM            Total value

 n1                 3          12             08 july                RITM123          300

n2                   2          13            08 july                RITM123          600

n3                    4           14           08 july                RITM234          200

 

So I have another table , i want to transfer data in such a way once scheduled job is run so it can copy the data. This will be asset wise. it should be in table b

 

its should divide by qty and display in 3 rows for first, 2 rows for second

 

So I want to generate this data with the segregated tax value for same RITM, there can be two line items and the segregated value is total divided by qty. But while querying with with RITM it is always divding by first qty records.

But for same RITM , there are 5 records that should poplate and all have the quantity different.

var m = new GlideRecord('tableA')

m.addQuery('RITM', ritmnumber);

m.query()

while(m.next()){

 

}

 

can you help with the code. 

1 REPLY 1

Sid_Takali
Kilo Patron
Kilo Patron

Hi @tanz Try this code and modify accordingly 

var ritmnumber = 'RITM123'; 
var m = new GlideRecord('tableA');
m.addQuery('RITM', ritmnumber);
m.query();
while (m.next()) {
    var itemtype = m.getValue('itemtype');
    var qty = parseInt(m.getValue('Qty'), 10);
    var totalValue = parseFloat(m.getValue('Total value'));
    var unitValue = totalValue / qty;
    
    for (var i = 0; i < qty; i++) {
        var b = new GlideRecord('tableB');
        b.initialize();
        b.setValue('itemtype', itemtype);
        b.setValue('number', m.getValue('number'));
        b.setValue('date_of_purchase', m.getValue('date_of_purchase'));
        b.setValue('RITM', m.getValue('RITM'));
        b.setValue('unit_value', unitValue); 
        b.insert();
    }
}