Hi everyone! I have a question i hope you can help...
# suitescript
b
Hi everyone! I have a question i hope you can help me out with. Is it possible to attach a file to a custom entity field inside a customer record? Currently the document is being saved/attached to the customer record under the communication tab / files section. I need it to be saved to a custom entity field that in this case it is located under the financial tab / documents and forms section.
m
As long as the entity field is of the type Document. You would use the
N/file
module to save to the file cabinet and it will return the internal id of the file and you will set that to the field value. Or if you just need to get an existing file then you can just place it's id within the field.
b
Hi Marvin! 🙂 Thank you for your response. And yes, i am currently saving the file using the file.save() function, and it is working since the file is attached to the customer. record.attach({ record:{ type: 'file', id: fileId }, to: { type: 'customer', id: recId } });
The file is being saved under the communication tab and then files section. What i need is to save the file in a custom field cust_test_attch which is of type 'Document'.
m
Then you would set like a normal field.
Copy code
customerRecord.setValue('cust_test_attch', fileId);
b
Ahh I see. 🙂 So to do so i need to create a new record by loading the current customer id?
and then setting the value of my custom field?
m
What kind of script are you using?
b
*@NApiVersion 2.x *@NScriptType Suitelet
Is that what you were referring to?
m
Yeah. If you haven't loaded the record with
record.load
then you need to do that and then after you set the value
record.save()
. Something like this.
Copy code
const customerRecord = record.load({
  type: record.Type.CUSTOMER,
  id: customer_id
});
customerRecord.setValue('cust_test_attch', fileId);
customerRecord.save();
b
I did this: var objRecord = record.load({ type: record.Type.CUSTOMER, id: recId }); objRecord.setValue('cust_test_attch', fileId);
m
just add
objRecord.save()
b
Ahh ok sorry i am not sure why it did not show me your full message.
got you! Ok trying it now 🙂
🙏
At least i got an error regarding an invalid field value for the custom field.
Thank you so much Marvin! I appreciate your help!
âś… 1
Hi Marvin, sorry to bug you again but i am getting an error related to the document saving to the custom field. "type":"error.SuiteScriptError","name":"INVALID_FLD_VALUE","message":"You have entered an Invalid Field Value 18112172 for the following field: cust_test_attch" I am loading the customer record, logged it and i see it loading and showing all the fields that have data. But when i try to save the fileId into the custom field, i get that error.
m
Did you check that 18112172 is the proper file id?
b
Yes i did.
I tried with another type of custom entity field and it saves properly.
m
Might want to add your response about the error back into main. I think I have only saved a file to a field once before using a script, usually I am fetching. So I could be wrong in what needs to be set.
b
Ok, thank you so much!!! I appreciate your time and help!
m
Should be able to do it like this.