function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Venkat PolisettVenkat Polisett 

Attachment creation hangs from S-Control

I have written an s-control that clones the attachments of one opportunity to another.  When it reaches the statement that creates the attachement, it hangs indefinetly.
Here is the code:
 
<script language="javascript" src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script> <script> function initPage() { sforceClient.registerInitCallback(Setup); sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}", true); } function Setup() { debugger; var origOppId = "00660000008IkwJ"; var clonedOppId = "00660000008IkXC"; // clone attachements var attachmentFieldList = new Sforce.Dynabean("Attachment").getDefinition().fieldList; var queryResult = sforceClient.query("Select " + attachmentFieldList + " from attachment where parentId = '" + origOppId + "'"); if (queryResult.className == "Fault") { var output = "There was an error retrieving Attachments: " + queryResult.toString(); alert(output) SetReturnURL(); return; } var oppAttachments = queryResult.records; for ( var i in oppAttachments) { var att = new Sforce.Dynabean("Attachment"); att.set("Name", oppAttachments[i].get("Name")); att.set("Body", oppAttachments[i].get("Body")); att.set("OwnerId", oppAttachments[i].get("OwnerId")); att.set("id", null); att.set("parentId", clonedOppId); var saveResult = sforceClient.create([att]); if (saveResult[0].success == false) { alert("Rats! There was a problem creating Attachment and I think the problem is: " + saveResult[0].errors[0].message); } } /* // clone Notes var noteFieldList = new Sforce.Dynabean("Note").getDefinition().fieldList; var queryResult = sforceClient.query("Select " + NoteFieldList + " from Note where parentId = '{!Opportunity_ID}'"); if (queryResult.className == "Fault") { var output = "There was an error retrieving Notes: " + queryResult.toString(); alert(output) SetReturnURL(); return; } var oppNotes = queryResult.records; for ( var i in oppNotes) { var note = oppNotes[i]; note.set("id", null); note.set("parentId", clonedOppId); var saveResult = sforceClient.create([note]); if (saveResult[0].success == false) { alert("Rats! There was a problem creating Note and I think the problem is: " + saveResult[0].errors[0].message); } } */ SetReturnURL(); return; } function SetReturnURL() { var url = "{!API_Enterprise_Server_URL_70}"; var returnURL = url.replace(/services\/.*/, "{!Opportunity_ID}"); top.location = returnURL; } </script>
gsickalgsickal

You can't retrieve multiple attachment body fields using the Query call (see the api docs).  Instead you should query for a list of attachment IDs and then use Retrieve in a loop to get each attachment that you can then save as a new attachment to the cloned object.  Keep in mind this does not scale well if your attachments are large files (in this case, you'll have to use a method such as that described in an earlier post http://community.salesforce.com/sforce/board/message?board.id=ajax_toolkit&message.id=1009)

Message Edited by gsickal on 11-10-2006 07:37 AM

Venkat PolisettVenkat Polisett
Thanks.
 
I will look into the API Docs.