Hi All! I Have been trying to update my Existing S...
# suitescript
g
Hi All! I Have been trying to update my Existing Sales Order by Changing Some fields in the Existing line Item but it is giving me some errors
Copy code
{"type":"error.SuiteScriptError","name":"TRANS_UNBALNCD","message":"Transaction was not in balance. Total = 700.0","id":"","stack":["anonymous(N/serverRecordService)","_put(/SuiteScripts/RestLet_Crude_Operation.js:211)"],"cause":{"type":"internal error","code":"TRANS_UNBALNCD","details":"Transaction was not in balance. Total = 700.0","userEvent":null,"stackTrace":["anonymous(N/serverRecordService)","_put(/SuiteScripts/RestLet_Crude_Operation.js:211)"],"notifyOff":false},"notifyOff":false,"userFacing":false}
Error: Error: {"type":"error.SuiteScriptError","name":"TRANS_UNBALNCD","message":"Transaction was not in balance. Total = 700.0","id":"","stack":["anonymous(N/serverRecordService)","_put(/SuiteScripts/RestLet_Crude_Operation.js:211)"],"cause":{"type":"internal error","code":"TRANS_UNBALNCD","details":"Transaction was not in balance. Total = 700.0","userEvent":null,"stackTrace":["anonymous(N/serverRecordService)","_put(/SuiteScripts/RestLet_Crude_Operation.js:211)"],"notifyOff":false},"notifyOff":false,"userFacing":false}
This is my JavaScript Code
Copy code
let data = {
      id: salesorder,
      recordtype: "salesorder",
      custbody_rr_startnewcontract: true,
      custbody_rr_contractpaymentmethod: "3",
      custbody_rr_billdaysinadvance: "15",
      opportunity: opportunity,
      cseg_property: property,
      custbodytransactionunit: unit,
      entity: entity,
      memo: memoforopp,
      custbody_agreement_tnumber: agreement,
      custbody_rr_contract_billing_day: contractBillingDay,
      salesrep: "8",
      department: "4",
      sublists: {
        item: {
          records: [
            {
              item: "29",
              line: 1,
              cseg_property: property,
              cseg_unit: unit,
              department: "4",
              description: memoforopp,
              custcol_rr_escpercent: "3",
              custcol_rr_escperiodmonths: "12",
              custcol_rr_contractendoftermaction: "4",
              custcolagreementline: agreement,
              custcol_rr_startdate: startDate,
              // custcol_rr_firstbillingdate: firstAutoBillDate,
              custcol_rr_nextescalationdate: nextEscalationDate,
              amount: license_fee,
              rate: license_fee,
            },
          ],
        },
      },
    };
And This is my suite Script Put request code
Copy code
function _put(context) {
    doValidation([context.recordtype, context.id], ["recordtype", "id"], "PUT");
    try {
      var rec = record.load({
        type: context.recordtype,
        id: context.id,
        isDynamic: true,
      });
      for (var fldName in context) {
        if (context.hasOwnProperty(fldName)) {
          if (
            fldName !== "recordtype" &&
            fldName !== "sublists" &&
            fldName !== "id"
          ) {
            var fieldObj = rec.getField({ fieldId: fldName });
            if (fieldObj && fieldObj.type === "date") {
              rec.setValue(fldName, new Date(context[fldName]));
            } else {
              rec.setValue(fldName, context[fldName]);
            }
          }
        }
      }
      if (context.sublists) {
        var sublists = context.sublists;
        for (var sublistsId in sublists) {
          var records = sublists[sublistsId].records;
          for (var i = 0; i < records.length; i++) {
            if (records[i].type && records[i].type === "REMOVE") {
              rec.removeLine({
                sublistId: sublistsId,
                line: records[i].line,
                ignoreRecalc: true,
              });
              continue;
            }
            rec.selectNewLine({ sublistId: sublistsId });
            for (var sublistsFieldName in records[i]) {
              var sublistFieldObj = rec.getSublistField({
                sublistId: sublistsId,
                fieldId: sublistsFieldName,
                line: i + 1,
              });
              if (sublistFieldObj && sublistFieldObj.type === "date") {
                rec.setCurrentSublistValue({
                  sublistId: sublistsId,
                  fieldId: sublistsFieldName,
                  value: new Date(records[i][sublistsFieldName]),
                });
              } else {
                rec.setCurrentSublistValue({
                  sublistId: sublistsId,
                  fieldId: sublistsFieldName,
                  value: records[i][sublistsFieldName],
                });
              }
            }
            rec.commitLine({ sublistId: sublistsId });
          }
        }
      }
      var recordId = rec.save();
      return JSON.stringify(recordId);
    } catch (error) {
      return JSON.stringify(error);
    }
  }
That last link mentions only setting Rate and not Rate and Amount. That could also be an issue
b
you probably need to cut down your request to only update the field you really want
setting the entity (and to a lesser degree the line) are not fields that you want to set
especially in dynamic mode
you also want to be much more careful about the order you set your fields
dynamic mode matches the sourcing done in the ui, for example, setting the rate after the amount overwrites the amount