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
I am working on import set API:
(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);
 
below is the error I get when I try in postman
 
{
    "result": {
        "status": "Error",
        "message": "Failed to insert into staging table"
    }
}
 
1 REPLY 1

Kieran Anson
Kilo Patron