Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Import Set API

supriya pratapa
Tera Guru
(function process(request, response) {
    var bodyText = request.body.dataString;
    if (!bodyText) {
        response.setStatus(400);
        return { status: "Error", message: "Missing request body" };
    }

    var body = {};
    try {
        body = JSON.parse(bodyText);
    } catch (e) {
        response.setStatus(400);
        return { status: "Error", message: "Invalid JSON" };
    }

    if (!body.data) {
        response.setStatus(400);
        return { status: "Error", message: "Missing body.data" };
    }

    var invoiceCaseNumber = (body.data.InvoiceCase + "").trim();
    if (!invoiceCaseNumber) {
        response.setStatus(400);
        return { status: "Error", message: "InvoiceCase is required" };
    }

    var fields = body.data.Fields || {};

    var invCase = new GlideRecord('sn_ap_cm_ap_case');
    invCase.addQuery('number', invoiceCaseNumber);
    invCase.query();
    if (!invCase.next()) {
        response.setStatus(404);
        return { status: "Error", message: "Invoice Case not found: " + invoiceCaseNumber };
    }

    var row = new GlideRecord('sn_ap_ic_ecolab_abbyy_invc');
    gs.error("STAGING DEBUG: " + JSON.stringify(row.getFields()));
    row.initialize();

    if (row.isValidField('u_invoice_case_number'))
        row.u_invoice_case_number = invoiceCaseNumber;

    if (fields["Invoice Number"]) row.u_supplier_invoice_number = fields["Invoice Number"] + "";
    if (fields["Invoice Date"])   row.u_invoice_date           = fields["Invoice Date"] + "";
    if (fields["Business Owner"]) row.u_business_owner         = fields["Business Owner"] + "";
    if (fields["Payment Terms"])  row.u_payment_terms          = fields["Payment Terms"] + "";
    if (fields["Purchase Order"]) row.u_purchase_order         = fields["Purchase Order"] + "";
    if (fields["Short Description"]) row.u_short_description   = fields["Short Description"] + "";
    if (fields["Supplier"])       row.u_supplier               = fields["Supplier"] + "";
    if (fields["Supplier Tax ID"])row.u_supplier_tax_id        = fields["Supplier Tax ID"] + "";
    if (fields["Type"])           row.u_type                   = fields["Type"] + "";

    if (fields["Subtotal"])       row.u_subtotal       = fields["Subtotal"] + "";
    if (fields["Supplier Tax"])   row.u_supplier_tax   = fields["Supplier Tax"] + "";
    if (fields["Shipping"])       row.u_shipping       = fields["Shipping"] + "";
    if (fields["Other Charges"])  row.u_other_charges  = fields["Other Charges"] + "";
    if (fields["Discount"])       row.u_discount       = fields["Discount"] + "";

    if (fields["Legal Entity"])   row.u_legal_entity   = fields["Legal Entity"] + "";

    if (fields["Bill to Street"])           row.u_bill_to_street          = fields["Bill to Street"] + "";
    if (fields["Bill to Country"])          row.u_bill_to_country         = fields["Bill to Country"] + "";
    if (fields["Bill to City"])             row.u_bill_to_city            = fields["Bill to City"] + "";
    if (fields["Bill to Zip/postal code"])  row.u_bill_to_zip_postal_code = fields["Bill to Zip/postal code"] + "";
    if (fields["Bill to state/province"])   row.u_bill_to_state_province  = fields["Bill to state/province"] + "";

    var stagingSysId = row.insert();
    if (!stagingSysId) {
        response.setStatus(500);
        return { status: "Error", message: "Failed to insert into staging table" };
    }

    return {
        status: "Success",
        invoice_case: invoiceCaseNumber,
        staging_row_sys_id: stagingSysId,
        message: "Staging row created successfully."
    };
})(request, response);
 
 
 
when i try the same in postman, I get this error:
 
{
    "result": {
        "status": "Error",
        "message": "Failed to insert into staging table"
    }
}
2 REPLIES 2

Kieran Anson
Kilo Patron

Hi,

When posting on the community, please use the </> option on the toolbar to format your code for readability. Are you aware that there is an Import Set API OOTB? Reader • Docs | ServiceNow It can also be configured to use either label or field name if you have a limitation of managing the payload format being sent by the 3rd party.

 

In terms of your error in your script, you can use the following to get the reason why the insert failed

row.getLastErrorMessage()

Hi,

 

I have tried the OOTB API and I get the user is not authenticated. What roles does a user need to create records in import set table through this API?