/* Note: Modify the ‘id’ and ‘name’ variables according to your scenario, this is for the case if you are opening a custom web resource from an entity */
// Get Id and entity Name from the parent opener
var id = Xrm.Page.data.entity.getId();
var nam = Xrm.Page.data.entity.getEntityName();
function _arrayBufferToBase64(buffer)
{ // Convert Array Buffer to Base 64 string
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++)
{
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
// function for Creating note record using SDK REST
function createNote(title, description, docBody, fName, mType)
{
var entity = {};
if (docBody != null & fName != null & mType != null) {
entity.DocumentBody = docBody;
entity.FileName = fName;
entity.MimeType = mType;
}
entity.Subject = title;
entity.NoteText = description;
entity.ObjectId = {
Id: id,
LogicalName: nam
};
SDK.REST.createRecord(
entity,
"Annotation",
function (result) {
var newEntityId = result.AnnotationId;
opener.RetrieveAnnotations(null);
window.close();
},
function (error) {
Xrm.Utility.alertDialog(error.message, null);
}
);
}
// function behind the onclick of Save button
function save()
{
var file = document.getElementById("file_Attach").files[0];
var subject = null; //document.getElementById("title").value;
var desc = null; //document.getElementById("text").value;
if (file) {
var reader = new FileReader();
reader.onload = function (evt)
{
var str = _arrayBufferToBase64(reader.result);
createNote(subject, desc, str, file.name, file.type);
}
reader.readAsArrayBuffer(file);
}
else {
createNote(subject, desc, null, null, null);
}
}
No comments:
Post a Comment