1 : I'm trying to make a library for inserting signatures in emails for our crm system, but I have some Scope problem and I can't figure out why, I'm fairly new at jscript and still a little confused about the scope.
2 : I set the _templateId variable in the end of my callback function _retrievedQueueItemsCallBack, I then add a alert to see that the variable have been set, afterwards I'm trying to use the variable in my InsertSignature2 function but here it's undefined and I cannot figure out why.
hope someone can help me.
if (typeof(EmailLib) === "undefined"){
EmailLib = {};
}
 var EmailLib = {
_templateId: "undefined",
_retrievedQueueItemsCallBack: function(retrievedItems){
         if (retrievedItems === null || retrievedItems[0] === null) {
             alert("No Record Found");
         }
         else {
             alert("Found " + retrievedItems[0].QueueId.Name);
             var QueueLookupItem = new Array();
             var RawlookupItem = new Object();
             var strQueueName = retrievedItems[0].QueueId.Name;
             var strQueueId = retrievedItems[0].QueueId.Id;
             RawlookupItem.id = strQueueId;
             RawlookupItem.name = strQueueName;
             RawlookupItem.typename = "queue";
             QueueLookupItem[0] = RawlookupItem;
             this._queueLookUpObject = QueueLookupItem;
             var tmp = "test";
//setting the right template
             switch(strQueueName)
             {
                 case "queuename":
                 alert("Matched templateID for Queue");
                 this._templateId = "Some Unique Id";
                 break;
                 case "queuename2":
                 this._templateId = "undefined"; 
                 break;
                 default:
                 this._templateId = "some unique id";
                 break;
             }
         }
     },
     _completeRetriveQueueItemsCallBack: function() {
         alert("callback complete");
         alert(this._templateId);
         //this._templateId = tmp;
         //alert(this._templateId);
     },
     InsertSignature2: function(ConfirmBeforeInsert) {
         var formType = Xrm.Page.ui.getFormType();
         var emailStatus = Xrm.Page.getAttribute("statecode").getValue();
         var emailDirection = Xrm.Page.getAttribute("directioncode").getValue();
         if((formType == 2 && emailStatus == "Open")) {
             var previousEmailId = EmailLib.GetOriginatingEmailId();
             var type = "QueueItem";
             var Options = "$select=QueueId,QueueItemId&$filter=ObjectId/Id eq guid" + "'" + previousEmailId + "'";
             SDK.REST.retrieveMultipleRecords(type, Options, this._retrievedQueueItemsCallBack, function (error) { alert(error); }, this._completeRetriveQueueItemsCallBack);
             if(ConfirmBeforeInsert === true){
                 var objInputBox = confirm("Click OK for automatic signature");
                 if (objInputBox === false){return;}
             }
             var objRegardingItems = this._getRegardingObject();
             if (objRegardingItems !== null) {
                 if (objRegardingItems[0].id === null || objRegardingItems[0].id === "") {alert("regarding id = null or empty"); return; }
                 if (objRegardingItems[0].type === null || objRegardingItems[0].type === "") {alert("regarding type = null or empty"); return; }
//at this point when i try to use the variable _templateId it is undefined even though it was set earlier.
                 var templateId = this._templateId;
                 alert(templateId);
                 if(templateId === "undefined"){return;}
                 var strEmailTemplate = this.GetEmailTemplate(templateId, objRegardingItems[0].id, objRegardingItems[0].type);
                 if (strEmailTemplate != "undefined") {
                     var strEmailBody = this.CreateEmailBodyFromTemplate(strEmailTemplate);
                     Xrm.Page.getAttribute("description").setValue(strEmailBody.toString() + Xrm.Page.getAttribute("description").getValue());
                     Xrm.Page.getAttribute("description").setSubmitMode("always"); 
                 }
             } 
         }   
     }
};
 
    