The following post is to show you how to create a Copy Button for a custom object.
This button will have the functionality of copying the entire record including the attachments.
Demonstration Video below:
The set up for the video demonstration is as below:
Custom Object name is: OSCSalesObj with the below properties:
Name (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
Standard Fields
First in creating the Copy Button we will be using 2 SOAP Web-services:
Sales Custom Object WS: LINK
Opportunity WS: LINK
Register this Web-Services in Sales Cloud
I have named my web-service references as Obj and attach.
Next go to fields and make a text field, call it Warning_c
Now go to Object Functions on the custom object and create anew Object function with this proprieties
Function: showWarning
Returns: void
Now go to Validation tab server scripts and create a field Validation on Warning_c .
Error message should be something like: The record has been copied successfully
Code:
With this specifications:
Function: Copy
Returns: void



def rand = new Random()
setAttribute(‘Warning_c’,rand.nextInt())
adf.error.warn(null)
Now move to the Actions and Link.
Create a new button in the Actions and Link of the Custom object
Call it Copy.

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){
}
