<apex:page standardController="Asset">
|
<apex:includeScript value="{!URLFOR($Resource.qrcode)}"/>
|
<apex:includeScript value="{!URLFOR($Resource.connection20)}"/>
|
<apex:includeScript value="{!URLFOR($Resource.apex20)}"/>
|
|
<div id="qrcode"></div>
|
<script type="text/javascript">
|
function createAttachment() {
|
var div = document.getElementById("qrcode");
|
var img = div.getElementsByTagName("img");
|
var src = img[0].src;
|
src = src.substring(22, src.length);
|
sforce.connection.sessionId = '{!GETSESSIONID()}';
|
// var record = sforce.connection.query("select id from Attachment where ParentId = \'{!Asset.Id}\' and name = \'QRCode\'");
|
var record = sforce.connection.query("SELECT ContentDocumentId from ContentDocumentLink where LinkedEntityId =\'{!Asset.Id}\'");
|
var records;
|
if(record.size > 0){
|
records = record.getArray("records");
|
var idList = records.map(obj => "'" + obj.ContentDocumentId + "'").join(",");
|
record = sforce.connection.query("SELECT Id,Title, OwnerId, ContentModifiedDate FROM ContentDocument WHERE Id IN (" + idList + ") AND Title = 'QRCode'");
|
}
|
// 既にあれば無視
|
if (record.size == 0) {
|
// var atta = new sforce.SObject("Attachment");
|
// atta.Name = "QRCode";
|
// atta.ParentId = "{!Asset.Id}";
|
// atta.Body = src;
|
// var result = sforce.connection.create([atta]);
|
var version = new sforce.SObject("ContentVersion");
|
version.VersionData = src;
|
version.Title = "QRCode";
|
version.ContentLocation= 's';
|
version.PathOnClient="QRCode.jpg";
|
var versionResult = sforce.connection.create([version]);
|
record = sforce.connection.query("SELECT ContentDocumentId FROM ContentVersion WHERE Id =\'" + versionResult[0].id + "\'");
|
records = record.getArray("records");
|
var link = new sforce.SObject("ContentDocumentLink");
|
link.ContentDocumentId = records[0].ContentDocumentId;
|
link.LinkedEntityId = "{!Asset.Id}";
|
link.ShareType = 'I';
|
link.Visibility = 'AllUsers';
|
var result = sforce.connection.create([link]);
|
|
|
var es = new sforce.SObject("Asset");
|
es.Id = "{!Asset.Id}";
|
es.QRId__c = versionResult[0].id;
|
es.Fixture_QRCode_Text__c = "{!Asset.Fixture_QRCode__c}";
|
result = sforce.connection.update([es]);
|
} else if ("{!Asset.QRId__c}" == null || "{!Asset.QRId__c}" == '' || "{!Asset.Fixture_QRCode__c}" != "{!Asset.Fixture_QRCode_Text__c}"){
|
// var records = record.getArray("records");
|
// var version =
|
// var atta = new sforce.SObject("Attachment");
|
// atta.Name = "QRCode";
|
// atta.Body = src;
|
// atta.Id = records[0].Id;
|
// var result = sforce.connection.update([atta]);
|
|
|
var result = sforce.connection.query("SELECT Id from ContentDocumentLink where ContentDocumentId = \'" + records[0].ContentDocumentId + "\'");
|
var version = sforce.connection.query("SELECT Id from ContentVersion where ContentDocumentId = \'" + records[0].ContentDocumentId + "\'");
|
var versions = version.getArray("records");
|
var atta = new sforce.SObject("ContentVersion");
|
atta.VersionData = src;
|
atta.Id = versions[0].Id;
|
version = sforce.connection.update([atta]);
|
records = result.getArray("records");
|
var es = new sforce.SObject("Asset");
|
es.Id = "{!Asset.Id}";
|
es.QRId__c = atta.Id;
|
es.Fixture_QRCode_Text__c = "{!Asset.Fixture_QRCode__c}";
|
result = sforce.connection.update([es]);
|
// TODO Name変わる可能性あり、upsertにします
|
}
|
}
|
|
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
text: "{!Asset.Fixture_QRCode__c}",
|
width: 128,
|
height: 128,
|
colorDark : "#000000",
|
colorLight : "#ffffff",
|
correctLevel : QRCode.CorrectLevel.H
|
});
|
|
// qrcode生成後、Attachmentを作成
|
setTimeout("createAttachment()", 1000);
|
</script>
|
</apex:page>
|