I'm tryting to remove emails from a support case. ...
# suitescript
s
I'm tryting to remove emails from a support case. I tried to use record.detach, but I'm getting this: "Detaching of record type message from supportcase is not supported." Is there a different way of doing this? I'm making copy of the cases, so deleting the emails isn't an option...
j
I made a copyCase() function, here’s our approach
Copy code
var new_enquiry = record.copy({
    type: record.Type.SUPPORT_CASE,
    id: enquiry.id,
    isDynamic: true
});

// Find and remove all emails attached to our copy, since NetSuite makes a broken email.
var copied_emails = search.create({
	type: 'message',
	filters: [['case.internalid', 'anyof', new_enquiry_id]],
	columns: [search.createColumn({name: 'internalid', label: 'internalid'})]
});
		
copied_emails.run().each(function(result) {

	record.delete({type: record.Type.MESSAGE, id: result.getValue(copied_emails.columns[0])});
						
	// There's probably just one but let's continue all the same.
	return true;
			
});

// Now, add all the messages in.
var original_emails = search.create({
	type: 'message',
	filters: [['case.internalid', 'anyof', enquiry.id]],
	columns: [search.createColumn({name: 'internalid', label: 'internalid'})]
});
		
original_emails.run().each(function(result) {

	try {
				
		// Copy it.
		var msg = record.copy({type: record.Type.MESSAGE, id: result.getValue(original_emails.columns[0])});
			
		// Set the correct Enquiry.
		msg.setValue({fieldId: 'activity', value: new_enquiry_id});
			
		msg.save();
							
	} catch(error) {	
			
		// Handle failure...
		  				  			
	}
									
	// Keep going to get the rest of 'em.
	return true;
			
});
👀 1
s
hey @jen sorry for the late reply, do you infer that after record.copy, the emails on the record are actually new "message records"?
j
Hey @Sciuridae54696d, to be honest I don’t actually remember all the details as I set this up a long time ago. Yes you do get a copy of each message record attached to your new Case, but there was something weird/broken about them so I found I had to manually add them back in (can’t remember what the issue was though). One problem with my approach however is that very occasionally, one of the emails is missing a Subject somehow, so it fails on saving the copied email as Subject cannot be blank. Hence the try-catch in there.