0

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"); 
                 }


             } 
         }   

     }
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
InSane
  • 1
  • 1
  • [`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) is the *context* of a function call. *scope* is what local variables live in. – Bergi May 23 '14 at 08:54
  • Looks like you've also got an [asynchrony issue](http://stackoverflow.com/q/23667086/1048572) – Bergi May 23 '14 at 08:56
  • Thanks for the info, I've found a solution but in future javascript I will have asynchrony in mind when using callbacks. :D – InSane May 26 '14 at 09:21

1 Answers1

0

Found the Solution:

Apparently when working with callbacks , they are operation totally in there own scope, so when in an object using this.function wasn't gonna work in a callback, but if you use LibraryName.Function you will be able to get in to the scope of the object, and therefore there was no need for global variables in the library :D, I Could just call my function from within the callback and send the Query data to the used funkction.

InSane
  • 1
  • 1