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
マスターキートン!マスターキートン! 

javascriptからremote action実行した際において、直前でinsertした値が取得できない

javascript remote actionを呼び出して、ファイルアップロード(attachementオブジェクトに登録)を実装しているところで、
1M程度くらいから、input too longでアップロードができなくなります。(remote actionの仕様のため)

1M以上のファイルをあげる為に、以下を参考に、
1MのファイルをバイナリからStringにして何分割かにしてそれを分割した数リクエストして、
DB保存時に前回の値をDBからひっぱってきて、バイナリにして保存という処理を書いたのですが、

https://forceadventure.wordpress.com/2013/07/19/uploading-attachments-to-salesforce-using-javascript-remoting/


1回目のリクエストでinsert後のattachment.id を
2回目に指定して、getAttachment しようとしたところ、
データが取得できません。

なぜ、1回目でinsertした値が読み取れないのでしょうか。。

◯VF(js)
function uploadAttachment(fileId) {
                    var attachmentBody = '';
                    if (fileSize <= positionIndex + chunkSize) {
                        attachmentBody = attachment.substring(positionIndex);
                        doneUploading = true;
                    } else {
                        attachmentBody = attachment.substring(positionIndex, positionIndex + chunkSize);
                    }

                    var apexWrapper = {
                        id: fileId,
                        parentId: "{!parent.Id}",
                        fileName: fileName,
                        fileSize: fileSize,
                        uploadFile: attachmentBody
                    };
                    doSaveByApex(apexWrapper); // →call remote action
                }


◯cls
// 添付ファイルアップサート
            Attachment attachment = JSRemotingFileUploadHelper.getAttachment(wrapper.id);
System.debug('attachment = ' + attachment);
            String newBody = '';
            if (attachment.Body != null) {
                newBody = EncodingUtil.base64Encode(attachment.Body);
            }
            newBody += wrapper.uploadFile;
            attachment.Body = EncodingUtil.base64Decode(newBody);

            if (String.isBlank(wrapper.id)) {
                attachment.Name = wrapper.fileName;
                attachment.ParentId = wrapper.parentId;
                attachment.ContentType = helper.getContentType(wrapper.fileName);
            }
            upsert attachment;
 
global static Attachment getAttachment(String id) {
        List<Attachment> attachments = [
                SELECT
                        Id,
                        Body
                FROM
                        Attachment
                WHERE
                        Id = :id
        ];

        if (attachments.isEmpty()) {
            Attachment a = new Attachment();
            return a;
        } else {
            return attachments[0];
        }
    }






 
Taiki YoshikawaTaiki Yoshikawa
RemoteAction処理はクラス変数に値を保持して使いまわすことができません。(処理実行時に毎回引数として渡す必要があります。)そのため対象のIDをreturnで戻しJS側の変数にセットする必要があります。そのあたりでうまく値を引き継ぎできていないのではないでしょうか。
マスターキートン!マスターキートン!
ご回答ありがとうございます。
処理実行時に毎回引数として渡しており、debugにより、確実に渡されていることはわかっています。
しかし、soqlの取得で抽出できない状態となっております。

ただ、よくよくみると、以下では、IEがサポートされていないことがわかり、それではダメなので、
soqlも予期したとおりにならないので、jsはやめて、普通のapex actionにしようと思います。

https://forceadventure.wordpress.com/2013/07/19/uploading-attachments-to-salesforce-using-javascript-remoting/


または、
http://tyoshikawa1106.hatenablog.com/entry/2016/02/09/015328
にありました、以下を参考に実装するかどうか
https://www.sundoginteractive.com/blog/salesforce-mvp-brings-multi-file-uploading-to-salesforce