Groovy 27 – Create Copy Button For Custom Object
Create a Copy Button for a Custom Object
This button will copy the entire record, including attachments.
Watch the demonstration video below:
Setup for the Video Demonstration
Custom Object name is: OSCSalesObj with the following properties:
- API Name: OSCSalesObj (OSCSalesObj_c)
- Description: OSCSalesObj
- Display Label: OSCSalesObj
- Plural Label: OSCSalesObj
- Record Name Label: OSCSalesObj Name
- Record Name Data Type: Text
Standard Fields:
Using SOAP Web Services
We will use two SOAP Web-services:
Register these Web Services in Sales Cloud
Named web-service references as Obj and attach.
Creating the Copy Button
Create a text field called Warning_c.
Next, create an Object Function with the following properties:
- Function: showWarning
- Returns: void
def rand = new Random() setAttribute('Warning_c', rand.nextInt())
Now go to the Validation tab, create a field validation on Warning_c, and set the error message to something like:
The record has been copied successfully
Code:
adf.error.warn(null)
Create the Copy Button
Go to Actions and Links and create a new button called Copy.
With these specifications:
- Function: Copy
- Returns: void
try { def Name = "Copy :" + RecordName + " " + now() def CustomText = CustomText_c def CustomNumber = CustomNumber_c def vAccount_c = Account_Id_c def vCountry_c = Country_c def vOwner = Owner_Id_c def Partner = Partner_Id_c boolean CustomCheckBox = false if (CustomCheckBox_c == 'Y') { CustomCheckBox = true } def objectName = 'OSCSalesObj_c' def object = [ RecordName: Name, CustomText_c: CustomText, CustomNumber_c: CustomNumber, CustomCheckBox_c: CustomCheckBox, Account_Id_c: vAccount_c, Country_c: vCountry_c, Owner_Id_c: vOwner, Partner_Id_c: Partner ] def myObject = adf.webServices.Obj.createEntity(object, objectName) String vID = Id def getAllAttachmentRows = [ [EntityName: 'OSCSalesObj_c', Pk1Value: vID] ] def attachIDs = adf.webServices.attach.getAttachmentsListWithoutContent(getAllAttachmentRows) if (attachIDs != null) { for (int i = 0; i < attachIDs.size(); i++) { Long pAttachmentId = Long.parseLong(attachIDs[i].AttachedDocumentId) def getThisAttachmentRows = [ EntityName: 'OSCSalesObj_c', Pk1Value: vID, AttachedDocumentId: pAttachmentId ] def attachment = adf.webServices.attach.getAttachmentContent(getThisAttachmentRows) String vTitle = attachment.Title String vFilename = attachment.FileName String vDatatypeCode = attachment.DatatypeCode String vCategory = attachment.CategoryName String vContentType = attachment.UploadedFileContentType String vContent = attachment.UploadedFile String vDownloadStatus = 'N' def createAttachmentRows = [ [ EntityName: 'OSCSalesObj_c', Pk1Value: myObject.Id, DatatypeCode: vDatatypeCode, FileName: vFilename, Title: vTitle, DownloadStatus: vDownloadStatus, CategoryName: vCategory, UploadedFileContentType: vContentType, UploadedFile: vContent ] ] def createAttachment = adf.webServices.attach.createAttachment(createAttachmentRows, 'commit') } } showWarning() } catch (e) { // Handle exception }