Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Javascript web resource works in browser, errors in Dynamics App for Outlook

Posted on by Microsoft Employee

I wrote code to format the text of opportunities, cases, and contact names into title case. It also takes into account some known acronyms that should be all caps. I have the web resource launching onchange, and it works perfectly in the web browser. It fails with an error message in Outlook. I have no idea why.  See sample code below:

function setTitleCase(executionContext) {
    var attribute = executionContext.getEventSource();    

    if (attribute != null) {
        
    var str = attribute.getValue();
    var lc, lowers, uc, uppers, surnames;
    var pieces = str.split(" ");

 lowers = ['a', 'an', 'the', 'and', 'but', 'or', 'for', 'nor', 'as', 'at', 
  'by', 'for', 'from', 'in', 'into', 'near', 'of', 'on', 'onto', 'to', 'with'];
  
 uppers = ['USA','US','U.S.','AL','AK','AZ',
	'AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS',
	'KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH',
	'NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN',
	'TX','UT','VT','VA','WA','WV','WI','WY','GU','PR','VI','MEX','CDN',
	'USDOJ','DOJ','DOC','AIC'];
 
surnames=['macd','mcd','mack','o\'b','mcke',
'o\'c','o\'d','o\'f','o\'g','o\'h','o\'j','o\'k',
'o\'l','o\'m','o\'n','o\'p','o\'q','o\'r','o\'s',
'o\'t','o\'u','o\'v','o\'w','o\'y','o\'z'];

    
    for (var i = 0; i < pieces.length; i++) {
		lc=pieces[i].toLowerCase();
		uc=pieces[i].toUpperCase();
        if(lowers.indexOf(lc)>=0) {
			pieces[i] = lc;  //if the word is in the lowers array, then make it lowercase
		} 
		else if(uppers.indexOf(uc)>=0) {
			pieces[i] = uc;  //If the word is in the uppers array, then make it uppercase
		}
		else if(pieces[i].charAt(0)=="(") {
			// ignore the things in ()
		}
		else if(pieces[i].charAt(pieces[i].length-1)==")") {
			// ignore the things in ()
		}
		else if(surnames.indexOf(lc.substring(0, 4))>=0) {
			//Check if it's a MacDonald, MacKenzie type of name
			var j = pieces[i].charAt(0).toUpperCase();			//M********
			j = j + pieces[i].charAt(1).toLowerCase();			//Ma*******
			j = j + pieces[i].charAt(2).toLowerCase();			//Mac******
			j = j + pieces[i].charAt(3).toUpperCase();			//MacD*****
			pieces[i] = j + pieces[i].substr(4).toLowerCase();	//MacDonald
		}
		else if(surnames.indexOf(lc.substring(0, 3))>=0) {
			//Check if it's an O'Brien type of name
			var j = pieces[i].charAt(0).toUpperCase();			//O********		M*******
			j = j + pieces[i].charAt(1).toLowerCase();			//O'			Mc******
			j = j + pieces[i].charAt(2).toUpperCase();			//O'C******		McD*****
			pieces[i] = j + pieces[i].substr(3).toLowerCase();      	//O'Connnor		McDonald
		}
		else {
			var j = pieces[i].charAt(0).toUpperCase();  //Make the first letter uppercase
			pieces[i] = j + pieces[i].substr(1).toLowerCase(); //Make the rest lowercase
		}
	}

    pieces[0] = pieces[0].replace(/^\w/, c => c.toUpperCase()); //Make the first letter uppercase
    attribute.setValue(pieces.join(' '));

}

}

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript web resource works in browser, errors in Dynamics App for Outlook

    I think I got it! 

    I replaced this:

        pieces[0] = pieces[0].replace(/^\w/, c => c.toUpperCase()); //Make the first letter uppercase

    With this:

        pieces[0] = pieces[0].charAt(0).toUpperCase() + pieces[0].substr(1); //Make the first letter uppercase
    


    That seems to have fixed the issue. 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript web resource works in browser, errors in Dynamics App for Outlook

    I followed your advice and tested this in IE11 (11.492.16299.0). I ran Internet Explorer as Admin, and I am logged in as a System Administrator. I'm not sure where to go from here, and I do appreciate the advice.

    I got to the step that works in Chrome without exception, and it threw an exception:

    One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: 'setTitleCase' is undefined at eval code (eval code:1:1)


    And this is the log file (slightly sanitized):

    ReferenceError: 'setTitleCase' is undefined
       at eval code (eval code:1:1)
       at RunHandlerInternal (https://****.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=-1705280916:159:1)
       at RunHandlers (https://*****.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=-1705280916:118:1)
       at ExecuteHandler (https://*****.crm.dynamics.com/form/ClientApiWrapper.aspx?ver=-1705280916:81:1)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.$Df_1 (https://*****.crm.dynamics.com/_static/form/formcontrols.js?ver=-1705280916:5093:17)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandler (https://*****.crm.dynamics.com/_static/form/formcontrols.js?ver=-1705280916:5031:13)
       at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandlerByDescriptor (https://*****.crm.dynamics.com/_static/form/formcontrols.js?ver=-1705280916:5062:13)
       at Anonymous function (https://*****.crm.dynamics.com/_static/form/formcontrols.js?ver=-1705280916:5073:13)
       at Anonymous function (https://*****.crm.dynamics.com/_common/global.ashx?ver=-1705280916:6780:193033)
       at Mscrm.TurboForm.Control.Data.DataAttributeBase.prototype.fireOnChange (https://*****.crm.dynamics.com/_static/form/formcontrols.js?ver=-1705280916:17689:13)

    I ran the same test in the Dynamics 365 App for Outlook, and got this message:

    (!) Script Error
    
    Web resource method does not exist: setTitleCase
    Session Id: f1db1448-d436-4636-a486-e57d34fd6e69
    Correlation Id: 49bb00d5-0dfd-4166-b70c-7b013f602c47
    Time: Tue Aug 07 2018 09:06:51 GMT-0500 (Central Daylight Time)


    And the log file:

    ReferenceError: Web resource method does not exist: setTitleCase
       at e.prototype.execute (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1644:50552)
       at e.prototype._executeIndividualEvent (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1644:42393)
       at e.prototype._executeEventHandler (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1644:41263)
       at execute (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1644:40870)
       at e.prototype._executeSyncAction (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1656:614)
       at e.prototype._executeSync (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1656:166)
       at e.prototype.executeAction (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1653:402)
       at t.dispatch (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1659:2055)
       at Anonymous function (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:2480:3285)
       at n.dispatch (https://*****.crm.dynamics.com/uclient/scripts/app.js?v=1.3.163-180712-011820:1659:1597)



    I have debugging turned on, and it is going through Visual Studio 2017. However, nothing displays at the time of the Script Error message.

  • Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Javascript web resource works in browser, errors in Dynamics App for Outlook

    The App for Outlook uses an embedded version of IE 11. Without looking at the code further I would test it in IE11 and see if the same error appears....

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans