Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Annoying and bemusing Jscript error

  • 1.  Annoying and bemusing Jscript error

    Posted 15 days ago
    Hi,

    I'm getting the JScript error "TypeError: Cannot read property 'getFormContext' of undefined" for this code:

    var Sdk = window.Sdk || {};
    (function () {
    
        // Code to run in the form OnSave event 
        this.formOnSave = function (executionContext) {
        	var formContext = executionContext.getFormContext();
            var selectedOptionText = formContext.getAttribute("new_bookingservicetasks").getText();
    	        
    	        var textfield = formContext.getAttribute("new_servicetasknotes");
    		var arrayLength = selectedOptionText.length;
    		var concatenatedText = "";
    		for (var i = 0; i < arrayLength; i++) {
    			concatenatedText += selectedOptionText[i] + "; ";
    		}
    		
    	textfield.setValue(concatenatedText);
        }
      
    }).call(Sdk);


    I've passed the execution context as the first parameter:


    I've published all customizations lots of times and, tried a different browser.  The most bemusing thing is that the script works, I just get a popup script error!

    Any thoughts?

    Thanks,

    Stewart

    ------------------------------
    -----------------------------
    Stewart Tranter
    Business Systems Manager
    TCL Group
    Derbyshire, UK
    ------------------------------


  • 2.  RE: Annoying and bemusing Jscript error

    GOLD CONTRIBUTOR
    Posted 15 days ago
    Try passing the context as parameter to the main function.

    ------------------------------
    Sunil Raheja
    Aha Apps
    Glen Allen VA
    https://www.ahaapps.com/microsoft-dynamics-crm/
    ------------------------------



  • 3.  RE: Annoying and bemusing Jscript error

    Posted 14 days ago
    Hi Sunil,

    Thanks for your reply.  I'm not quite sure how this would look.  I've tried:

    var Sdk = window.Sdk || {};
    (function (executionContext) {
        var formContext = executionContext.getFormContext();
    
        // Code to run in the form OnSave event 
        this.formOnSave = function (formContext) {
        	
            var selectedOptionText = formContext.getAttribute("new_bookingservicetasks").getText();
    	        
    	        var textfield = formContext.getAttribute("new_servicetasknotes");
    		var arrayLength = selectedOptionText.length;
    		var concatenatedText = "";
    		for (var i = 0; i < arrayLength; i++) {
    			concatenatedText += selectedOptionText[i] + "; ";
    		}
    		
    	textfield.setValue(concatenatedText);
        }
      
    }).call(Sdk);​​
    and now it doesn't work at all.

    Stewart

    ------------------------------
    -----------------------------
    Stewart Tranter
    Business Systems Manager
    TCL Group
    Derbyshire, UK
    ------------------------------



  • 4.  RE: Annoying and bemusing Jscript error

    Posted 13 days ago
    Well, I've fixed it.  Not quite sure how, but I am now using the onFormChange event to call this script:

    var Sdk = window.Sdk || {};
    (function () {
        // Define some global variables
        
        // Code to run in the attribute OnChange event 
        this.attributeOnChange = function (executionContext) {
                	var formContext = executionContext.getFormContext();
                	var selectedOptionText = formContext.getAttribute("new_bookingservicetasks").getText();
    	        
                	var textfield = formContext.getAttribute("new_bookingservicetasktextfield");
    		var arrayLength = selectedOptionText.length;
    		var concatenatedText = "";
    		for (var i = 0; i < arrayLength; i++) {
    			concatenatedText += selectedOptionText[i] + ";";
    		}
    		
    		textfield.setValue(concatenatedText);
        }    
    }).call(Sdk);​

    It works beautifully.  Every time a value is added or removed, from the multi-select option set, the text field updates with the chosen values.

    Stewart

    ------------------------------
    Stewart Tranter
    Business Systems Manager
    TCL Group
    Derbyshire, UK
    ------------------------------



  • 5.  RE: Annoying and bemusing Jscript error

    GOLD CONTRIBUTOR
    Posted 13 days ago
    Apologies for not looking at this earlier Stewart.
    And am glad it worked finally. I did not pay attention that you were not calling it on Attribute's on change event, which is how it should have been. Great work!!

    ------------------------------
    Sunil Raheja
    Aha Apps
    Glen Allen VA
    https://www.ahaapps.com/microsoft-dynamics-crm/
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role