``` fields = context.fields; rolemap =...
# suitescript
n
Copy code
fields = context.fields;
            rolemap = {
                "Winner": 1,
                "Bidder": 2
            }

            if (context.recordType == "opportunity") {
                for (var i = 0; i < fields.length; i++) {
                    record.attach({
                        record: {
                            type: 'opportunity',
                            id: context["ns_id"]
                        },
                        to: {
                            type: 'customer',
                            id: fields[i]["contact_id"]
                        },
                        attributes: {
                            role: rolemap[fields[i].label]
                        }
                    });
                }
Attempting to attach customers to an opportunity record in suitescript, getting some error however
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "TypeError",
  "message": "execute on com.sun.proxy.$Proxy668.nlapiAttachRecord failed due to: Cannot convert '{versions: [{name: \"netsuiteid\", value: \"7499012\", timestamp: 1646288523546, source: \"API\", sourceVid: [], requestId: \"6dee4522-5322-4906...'(language: JavaScript, type: Object) to Java type 'java.lang.String': Invalid or lossy primitive coercion.",
  "stack": [
    "Error",
    " at Object.attachRecord (suitescript/resources/javascript/record/recordImpl.js:169:11)",
    " at <http://Object.post|Object.post> (/SuiteScripts/project.js:19:28)"
  ],
  "cause": {
    "message": "execute on com.sun.proxy.$Proxy668.nlapiAttachRecord failed due to: Cannot convert '{versions: [{name: \"netsuiteid\", value: \"7499012\", timestamp: 1646288523546, source: \"API\", sourceVid: [], requestId: \"6dee4522-5322-4906...'(language: JavaScript, type: Object) to Java type 'java.lang.String': Invalid or lossy primitive coercion.",
    "stack": "TypeError: execute on com.sun.proxy.$Proxy668.nlapiAttachRecord failed due to: Cannot convert '{versions: [{name: \"netsuiteid\", value: \"7499012\", timestamp: 1646288523546, source: \"API\", sourceVid: [], requestId: \"6dee4522-5322-4906...'(language: JavaScript, type: Object) to Java type 'java.lang.String': Invalid or lossy primitive coercion.\n at Object.attachRecord (suitescript/resources/javascript/record/recordImpl.js:169:11)\n at <http://Object.post|Object.post> (/SuiteScripts/project.js:19:28)"
  },
  "notifyOff": false,
  "userFacing": true
}
c
Check the IDs you are passing in. Is this even possible in the UI? I thought an opp just had a customer specified at the header level & didn't realise you could attach them like this
n
I think my primary issue was the flow of the script. I need to attach contact to opp, not opp to contact. I am still debugging some things currently
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "UNEXPECTED_ERROR",
  "message": "An unexpected SuiteScript error has occurred",
  "stack": [
    "Error\n at Object.attachRecord (suitescript/resources/javascript/record/recordImpl.js:169:11)\n at <http://Object.post|Object.post> (/SuiteScripts/project.js:20:28)"
  ],
  "cause": {
    "type": "internal error",
    "code": "UNEXPECTED_ERROR",
    "details": "An unexpected SuiteScript error has occurred",
    "userEvent": null,
    "stackTrace": [
      "Error\n at Object.attachRecord (suitescript/resources/javascript/record/recordImpl.js:169:11)\n at <http://Object.post|Object.post> (/SuiteScripts/project.js:20:28)"
    ],
    "notifyOff": false
  },
  "id": "",
  "notifyOff": false,
  "userFacing": true
}
This is the latest error, I did confirm that the IDs I am trying to attach can be attached,
also the code has been updating to be
Copy code
record.attach({
                        record: {
                            type: record.Type.CONTACT,
                            id: fields[i]["contact_id"]
                        },
                        to: {
                            type: record.Type.OPPORTUNITY,
                            id: context["ns_id"]
                        },
                        attributes: {
                            role: rolemap[fields[i].label]
                        }
                    });