Skip to main content
edited title
Link
J.W.
  • 21
  • 1
  • 1
  • 6

Soap AJAX attachment upload, bad field ParentId

Added JS code, and edited the description to better reflect the situation and background
Source Link
J.W.
  • 21
  • 1
  • 1
  • 6

I have a project to upload some files for both licensed users and portal users. Got it working 98%... except for portal users.. mama mia...

I created a site off a domain, logged in a user and inserted a content versionContentVersion by API, in soap and javascriptalso an Attachment, both in JavaScript, and the attached fileContentVersion appears in salesforce classic chatter, and the attachment in an account related list. However, ideally, I want to insert an attachment, so it shows up under a related list in accountjust Attachments, but when setting Parent Id in JavaScript, an exception is thrown using the API for portal users: attachmentAttachment: Bad Field names on insert/update call: ParentId. 

I read recently that soapAPI calls are now restricted for attachments, and we cannot insert attachments for portal users, as witnessed by the exception. Is there some way I can let a soapportal user upload an attachment so that the parent recorda ParentId can be set? In(In my case, set to an account record.) Need I try sending attachments as an email and make a trigger on emails received to insert an attachment?

Function UploadFile(parentId) is called from an onclick commandlink. variables {!sessionID} {!username} {!password} and {!parentAccountName} are set in prior pages. The !sessionID is a session ID generated by logging in on a prior page using SOAP, but when I get to here and try to insert, error says session invalid, so I've logged in on this page too, but using the API to get a session... If a local user is using my forms, no problems, meaning not required to login to API AND I can insert an attachment like I like instead of inserting a VersionContent.

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="/soap/ajax/32.0/connection.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
     
<script type="text/javascript">
            var
            localUsersession = '{!$Api.Session_ID}', externalUsersession = '{!sessionID}';
                var filesToUpload = [];  
                var uploadedFile = 0, __sfdcSessionId='', runninglog='';

            if ( (localUsersession=='') || (localUsersession=='NULL_SESSION_ID')) { 
                    sforce.connection.login('{!username}','{!password}');  // username and password come from the controller, set in the login page
                 __sfdcSessionId=sforce.connection.sessionId;
                     }
            else {
                    __sfdcSessionId=localUsersession;
                    sforce.connection.sessionId = __sfdcSessionId;
            }

        function uploadFile(parentId)
            {   
                // parentId='001o000000Pc7HtAAJ'; // for debugging
                var  
                    filesToUpload=[];
                    filesToUpload.push($(this)[0].files[0]); 
                 
                
                 for(var i = 0, f; f = filesToUpload[i]; i++)
                {
                   
                    var reader = new FileReader();
                        
                   
                    // Keep a reference to the File in the FileReader so it can be accessed in callbacks
                    reader.file = f;

                    reader.onload = function(e)
                    {
                       /* this block is for attachment insertion

                        var att = new sforce.SObject("Attachment");
                        att.Name = this.file.name;
                        att.ContentType = this.file.type;
                        att.ParentId = parentId;
                        
                        var binary = "", bytes = new Uint8Array(e.target.result);
                        var length = bytes.byteLength;
                        
                        for (var j = 0; j < length; j++)
                            { binary += String.fromCharCode(bytes[j]); }
                        
                        att.Body = (new sforce.Base64Binary(binary)).toString();
            
          */
                        
                           var fi = new sforce.SObject('FeedItem'); 
                               fa = new sforce.SObject('FeedAttachment');
                               cv = new sforce.SObject('ContentVersion');
                            
                            fi.Title = this.file.name;
                            fi.Body = 'Document from '+'{!nickname}';
                            fi.ParentId = parentId;
                            sforce.connection.create([fi]);
                            
                            cv.Title=this.file.name+' for: '+{!parentAccountName};
                            cv.VersionData = (new sforce.Base64Binary(binary)).toString();
                            cv.PathOnClient= this.file.name;
                           
            sforce.connection.create([cv],
                            {
                            onSuccess : function(result, source)
                            {
                                if (result[0].getBoolean("success"))
                                { 
                                  fa.FeedEntityId = fi.Id;
                                  fa.RecordId = cv.Id;
                                  fa.Title='Document from {!nickname}';
                                  fa.Type='CONTENT';
                                  sforce.connection.create([fa]);
                                    
                                  var  html = "Finished uploading "+att.Name; 
                                      if (filesToUpload.length>1)  
                                        html+='... Now uploading the next one...';
                                  document.getElementById('statusid').innerHTML = html;
                                  html='';
                                
                                    
                                } // success
                                else
                                {   var html = "Failed to create attachment!<br/>"+result[0];
                                    document.getElementById('statusid').innerHTML = html;
                               
                                }
                            },
                            onFailure : function(error, source)
                            {
                                var html = "An Error has occurred<br/>"+error;
                                document.getElementById('statusid').innerHTML = html;
                               
                            }
                            
                        }); // sforce.create feeditem 
                        } // externalUsersession
                        else
                        sforce.connection.create([att],
                        { etc... repeat the onsuccess and onfailure blocks

            }

        }  // onload
        } // for loop
    } // function uploadfiles

I have created a site off a domain, logged in a user and inserted a content version, in soap and javascript, and the attached file appears in salesforce classic chatter. However, ideally, I want to insert an attachment, so it shows up under a related list in account, but when setting Parent Id in JavaScript, an exception is thrown: attachment: Bad Field names on insert/update call: ParentId. I read recently that soap calls are now restricted and we cannot insert attachments, as witnessed by the exception. Is there some way I can let a soap user upload an attachment so that the parent record can be set? In my case, set to an account record. Need I try sending attachments as an email and make a trigger on emails received to insert an attachment?

I have a project to upload some files for both licensed users and portal users. Got it working 98%... except for portal users.. mama mia...

I created a site off a domain, logged in a user and inserted a ContentVersion by API, and also an Attachment, both in JavaScript, and the ContentVersion appears in salesforce classic chatter, and the attachment in an account related list. However, ideally, I want to insert just Attachments, but when setting Parent Id in JavaScript, an exception is thrown using the API for portal users: Attachment: Bad Field names on insert/update call: ParentId. 

I read recently that API calls are now restricted for attachments, and we cannot insert attachments for portal users, as witnessed by the exception. Is there some way I can let a portal user upload an attachment so a ParentId can be set? (In my case, set to an account record.) Need I try sending attachments as an email and make a trigger on emails received to insert an attachment?

Function UploadFile(parentId) is called from an onclick commandlink. variables {!sessionID} {!username} {!password} and {!parentAccountName} are set in prior pages. The !sessionID is a session ID generated by logging in on a prior page using SOAP, but when I get to here and try to insert, error says session invalid, so I've logged in on this page too, but using the API to get a session... If a local user is using my forms, no problems, meaning not required to login to API AND I can insert an attachment like I like instead of inserting a VersionContent.

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="/soap/ajax/32.0/connection.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
     
<script type="text/javascript">
            var
            localUsersession = '{!$Api.Session_ID}', externalUsersession = '{!sessionID}';
                var filesToUpload = [];  
                var uploadedFile = 0, __sfdcSessionId='', runninglog='';

            if ( (localUsersession=='') || (localUsersession=='NULL_SESSION_ID')) { 
                    sforce.connection.login('{!username}','{!password}');  // username and password come from the controller, set in the login page
                 __sfdcSessionId=sforce.connection.sessionId;
                     }
            else {
                    __sfdcSessionId=localUsersession;
                    sforce.connection.sessionId = __sfdcSessionId;
            }

        function uploadFile(parentId)
            {   
                // parentId='001o000000Pc7HtAAJ'; // for debugging
                var  
                    filesToUpload=[];
                    filesToUpload.push($(this)[0].files[0]); 
                 
                
                 for(var i = 0, f; f = filesToUpload[i]; i++)
                {
                   
                    var reader = new FileReader();
                        
                   
                    // Keep a reference to the File in the FileReader so it can be accessed in callbacks
                    reader.file = f;

                    reader.onload = function(e)
                    {
                       /* this block is for attachment insertion

                        var att = new sforce.SObject("Attachment");
                        att.Name = this.file.name;
                        att.ContentType = this.file.type;
                        att.ParentId = parentId;
                        
                        var binary = "", bytes = new Uint8Array(e.target.result);
                        var length = bytes.byteLength;
                        
                        for (var j = 0; j < length; j++)
                            { binary += String.fromCharCode(bytes[j]); }
                        
                        att.Body = (new sforce.Base64Binary(binary)).toString();
            
          */
                        
                           var fi = new sforce.SObject('FeedItem'); 
                               fa = new sforce.SObject('FeedAttachment');
                               cv = new sforce.SObject('ContentVersion');
                            
                            fi.Title = this.file.name;
                            fi.Body = 'Document from '+'{!nickname}';
                            fi.ParentId = parentId;
                            sforce.connection.create([fi]);
                            
                            cv.Title=this.file.name+' for: '+{!parentAccountName};
                            cv.VersionData = (new sforce.Base64Binary(binary)).toString();
                            cv.PathOnClient= this.file.name;
                           
            sforce.connection.create([cv],
                            {
                            onSuccess : function(result, source)
                            {
                                if (result[0].getBoolean("success"))
                                { 
                                  fa.FeedEntityId = fi.Id;
                                  fa.RecordId = cv.Id;
                                  fa.Title='Document from {!nickname}';
                                  fa.Type='CONTENT';
                                  sforce.connection.create([fa]);
                                    
                                  var  html = "Finished uploading "+att.Name; 
                                      if (filesToUpload.length>1)  
                                        html+='... Now uploading the next one...';
                                  document.getElementById('statusid').innerHTML = html;
                                  html='';
                                
                                    
                                } // success
                                else
                                {   var html = "Failed to create attachment!<br/>"+result[0];
                                    document.getElementById('statusid').innerHTML = html;
                               
                                }
                            },
                            onFailure : function(error, source)
                            {
                                var html = "An Error has occurred<br/>"+error;
                                document.getElementById('statusid').innerHTML = html;
                               
                            }
                            
                        }); // sforce.create feeditem 
                        } // externalUsersession
                        else
                        sforce.connection.create([att],
                        { etc... repeat the onsuccess and onfailure blocks

            }

        }  // onload
        } // for loop
    } // function uploadfiles
Source Link
J.W.
  • 21
  • 1
  • 1
  • 6

Soap attachment upload, bad field ParentId

I have created a site off a domain, logged in a user and inserted a content version, in soap and javascript, and the attached file appears in salesforce classic chatter. However, ideally, I want to insert an attachment, so it shows up under a related list in account, but when setting Parent Id in JavaScript, an exception is thrown: attachment: Bad Field names on insert/update call: ParentId. I read recently that soap calls are now restricted and we cannot insert attachments, as witnessed by the exception. Is there some way I can let a soap user upload an attachment so that the parent record can be set? In my case, set to an account record. Need I try sending attachments as an email and make a trigger on emails received to insert an attachment?