Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebResource in Dynamics 365 #7

Closed
MSCRMGuru opened this issue Jul 10, 2017 · 15 comments
Closed

WebResource in Dynamics 365 #7

MSCRMGuru opened this issue Jul 10, 2017 · 15 comments
Assignees

Comments

@MSCRMGuru
Copy link

Hi,

I'm trying to use the file as a WebResource in Dynamics 365. I keep getting an error saying DynamicsWebApi is not defined when trying to create one (var x = new DynamicsWebApi();). It's been added to the form as a library.

Tried to use require.js and head,js to load it but got the same result...

I wasn't sure which file in the dist folder is the correct one so I tried with all of them with the same result.

This is with Dynamics 365 trial.

Thanks!

@AleksandrRogov
Copy link
Owner

Hi @MSCRMGuru ,
First of all, you need to look at dist folder in the repository.

Secondly, you need to choose if you wish to use Promise or Callbacks based library: dynamics-web-api.js is a Promise based library and dynamics-web-api-callbacks.js is a Callbacks based library. If you choose Promise based library I would recommend adding ES6-Promise or Yaku polyfill libraries to the form. if you choose Callbacks - then no other libraries needed.

You do not need either require.js or head.js for the library to work.

Let me know if this resolves your issue.

P.S. you can also use minified versions in CRM.

@MSCRMGuru
Copy link
Author

Hi,

Thanks for the response.

I used the callback file from the dist folder as suggested. Added it to the account form as well as a custom script with a trigger on Load with just "debugger;" so I can enter debug mode.

I validated the dynamics-web-api has loaded, but when I try to instnasite, I get an error:
image

Let me know if you need any more details.

Thanks!

@AleksandrRogov
Copy link
Owner

AleksandrRogov commented Jul 11, 2017

Hi @MSCRMGuru ,
It is not accessible through a browser console. You should add another web resource to the form where you add your code that uses DynamicsWebApi object. I'd also suggest initializing it during onload event of the form.
Update: Sorry, I forgot to switch the context :-). And it's working fine for me.

2017-07-10_22-37-17

What is the error on top of the line var x = new DynamicsWebApi(); on your screenshot? Maybe there was internal CRM error and the script has not been loaded??
It seems like you did everything correctly.

@MSCRMGuru
Copy link
Author

MSCRMGuru commented Jul 11, 2017

Just to clarify, I do have my own separate JavaScript webresource with an event hooked to Form Load. In there is where I try to create a new DynamicsWebApi.

this is a trial of instance of Dynamics 365 I thought maybe they did something to how the form scripts are loaded again. I just tried connecting it to an OnChange event on the form and I get the same result.

This is the row it's failing on:
image

After stepping into it, I get:
image

The log file has these details:

ReferenceError: DynamicsWebApi is not defined
    at Test (https://crmsolpolyflor.crm6.dynamics.com/%7B636353401830000271%7D/WebResources/crmsol_/account/FormLogic.js?ver=-1960232510:10:11)
    at eval (eval at RunHandlerInternal (https://crmsolpolyflor.crm6.dynamics.com/form/ClientApiWrapper.aspx?ver=-1960232510:153:1), <anonymous>:1:1)
    at RunHandlerInternal (https://crmsolpolyflor.crm6.dynamics.com/form/ClientApiWrapper.aspx?ver=-1960232510:157:1)
    at RunHandlers (https://crmsolpolyflor.crm6.dynamics.com/form/ClientApiWrapper.aspx?ver=-1960232510:116:1)
    at ExecuteHandler (https://crmsolpolyflor.crm6.dynamics.com/form/ClientApiWrapper.aspx?ver=-1960232510:79:1)
    at Mscrm.TurboForm.Control.CustomScriptsManager.$CU_1 (https://crmcdn.azureedge.net/8-2-1-271/retail/_static/form/formcontrols.js?ver=-1960232510:3304:100)
    at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (https://crmcdn.azureedge.net/8-2-1-271/retail/_static/form/formcontrols.js?ver=-1960232510:3250:18)
    at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (https://crmcdn.azureedge.net/8-2-1-271/retail/_static/form/formcontrols.js?ver=-1960232510:3281:18)
    at https://crmcdn.azureedge.net/8-2-1-271/retail/_static/form/formcontrols.js?ver=-1960232510:3289:19
    at https://crmsolpolyflor.crm6.dynamics.com/_common/global.ashx?ver=-1960232510:4020:149265

I've tried different browsers and get the same result...

Not sure what i'm doing wrong...

Thanks!

@AleksandrRogov
Copy link
Owner

So, it does not work when you add that line to OnLoad event or to OnChange event?
Do you load the library before that scripts, right?

I cannot get it why it's not working on your side. Feels like the web resource is just not loading...
Did you publish all customizations?

@AleksandrRogov
Copy link
Owner

AleksandrRogov commented Jul 11, 2017

can you try to empty dynamicsWebApi web resource and put there these simple lines?

var DynamicsWebApi = function(){
    this.test = function(){
        alert("it's working");
    }
}

And just to try to create an object, will that work? If not, then something wrong with web resource loading.

@MSCRMGuru
Copy link
Author

Hey,

I made the change as you suggested and it seems to be working:

image

image

and with adding x.test() to my Test function I also get this:
image

I then tried updating it back to the callbacks file (dynamics-web-api-callbacks.js) and i'm back at the same issue.... It means that the file is being loaded, but for some reason not exporting the DynamicsWebApi object.

I will try with a couple of different organisations later today to see if I come up with different results.

Let me know if you have any other ideas on things to try.

Thanks for all your help!

@AleksandrRogov
Copy link
Owner

AleksandrRogov commented Jul 12, 2017

HI @MSCRMGuru ,
Hm... that's weird.

How do you upload web resources? Are you using Visual Studio or CRM UI?
If CRM UI, try to use "Upload file" option when a JavaScript web resource type chosen. It's recommended to use this option instead of a copy-paste directly in the editor.
2017-07-12_14-02-49

Other than that, I suggest using Visual Studio to upload and publish web resources (unless you already do that, if so... I've no idea what's wrong, except the DynamicsWebApi code you upload is corrupted).

@MSCRMGuru
Copy link
Author

Hey,

i'm using the XrmToolbox WebResource Manager to upload the script, which i'm copying directly from the GitHub "View Raw" page for the callbacks javascript...

I'll give it a try today with other CRM organisations\versions and see what I come up with.

I'll provide an update later on.

Thanks again for your help!

@MSCRMGuru
Copy link
Author

I don't know why or how, but it suddenly started working for me. I thought id give it one more test before trying a different framework, and it started working... Same org, same browser, same everything. Only thing that was done this mornign was an update of sitemap and republish of all customistions. This was also done yesterday when this wasn't working....

Not sure if its Dynamics 365 being slow, or something was cached, but it seems to b e working fine now.

A bit concerning as if it breaks I won't really know how to fix it, but I guess i'll let future me worry about that :)

Thanks again for all your help!

@AleksandrRogov
Copy link
Owner

I'm glad it's working!

@MSCRMGuru
Copy link
Author

I found the issue!!

It started happening again a few minutes after I started including my code and it nearly drove me nuts...

I was using Edge to test (since I had other Office 365 session in all my other browsers), and it actually provided a helpful message: Mismatched Anonymous Define()....

I had a look and found this stackoverflow question about this.

Dynamics 365 with the latest Project Service Automation (PSA) will have some custom scripts loaded on various forms, and it uses require.js as well as head.js:

image

Removing both require and head from the Account form resolved the issue. It worked previously becuase the DynamicsWebApi library was loaded before require.js - if it was loaded after it wouldn't work...

Not sure if you can fix this or how, but if require.js is included in the same form as your library things will randomly break.... For the moment I removed it and disabled the onload handlers that require it, but it's not a great solution....

Any thoughts??

@AleksandrRogov
Copy link
Owner

Interesting, I will check that behaviour. Why not load DynamicsWebApi first for now?

Thanks

@AleksandrRogov
Copy link
Owner

Hi @MSCRMGuru ,
An issue was in a webpack configuration file that bundles Node.js library into one file to support a browser. I needed to set a parameter that names an AMD module instead of using an undefined name. Hopefully, it's going to work fine together with require.js now.

Thank you to your investigation and help!

@MSCRMGuru
Copy link
Author

Hey Alex,

Thanks for your incredible responsiveness in helping look into the issue and resolving it.

I haven't tested this yet and hope to get it tested in the next few days. I will let you know if I come across any other issues!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants