Dynamics CRM - call javascript resource - javascript

We are in the process of migrating our data to CRM online.
I would like to run some javascript code that I have that creates the sharepoint folder for an entity when the page loads.
However as we are using an api to create the record from existing data, is there a way to call/trigger the javascript on record create?

There is no hook for JavaScript with Dynamics CRM to trigger an action when a record is created, however the following may work (although is a little messy!).
1) Create a new dummy field (hidden, but on every the form) called "SharePoint Created?", this is a two options field, default value of "No".
2) Create a new JavaScript function to execute on "Form Load", which checks the form type to ensure its in edit mode (not create), if so, checks the value for the "SharePoint Created?" field, if this field is "No", update it to "Yes", call your SharePoint creation function, then save the record.
The better option would be to the move the call to a Plugin, which can be executed on create of a record, but understand authentication is always a problem.

Related

How to preserve userscript modifications in Chrome after an asynchronous content update on an .aspx webpage

I'm trying to automate the workflow of a webpage for my company's inventory system. The page is generated by the server-side logic of an ASP.Net page, which I don't have access to. The page has several fields on it to allow you to enter a new container barcode, the item that should go in the container, etc. Each of these fields has an onchange event listener hooked up to it which calls the page's __doPostBack() function to verify the entered data. If the data is verified, the page code is re-served with the data entered so far, and focus is set to the next field on the form.
I want to automate this page with a userscript in Chrome. I started by using ViolentMonkey to inject a custom script, but I could only get the script to trigger on the initial load, not after each data entry. After this, I tried using Chrome Local Overrides to change __doPostBack() to try to capture the data I need to automate the page. That also only works once; after a field is filled and loses focus and new HTML is served, it overwrites Chrome's local copy.
I think that my problems are being caused by an asynchronous refresh of the entire page contents, which wipes out the injected userscript and Chrome's Local Override without triggering the normal page refresh listeners in Chrome Overrides or ViolentMonkey to re-inject the modified code. Does anyone have any thoughts on how I could modify the JavaScript in such a way that it would persist after the page content is replaced with new HTML?
P.S. I don't think the code itself is relevant to this particular problem, but if anyone thinks it would be helpful to share a limited section of the client-side code, let me know.
Edit 1: Here's a more in-depth view of what I'm trying to accomplish, and the progress I've made so far. For reference, the form looks like this:
My Original Plan
The user loads the page. ViolentMonkey injects a userscript which issues a series of prompts, collecting data on the range of new barcodes that the user would like entered into the system. (Specifically, the barcode prefix, the starting barcode number, and the ending barcode number.) This values are stored in localStorage.
After this data has been collected and validated by the user, the page loads normally. For reference, the form looks something like this:
The user fills out the fields as normal. After each field is filled out (with the exception of the Container Description field), the page pushes focus to the next field. (For example: <script language="javascript"> try { document.getElementById('txtContDesc').focus() } catch (e) { } </script>. The id of the field to focus is dynamically changed via the server logic.)
I need to collect the User Badge, Container Type, and Destination Barcode values so that I can refill them later when I automate the form. My original plan was to add a onfocus event listener to the Container Description field, since focus will be shifted to it once the Destination Barcode field has been verified. I will know at this point that the user has successfully entered a valid entry for each of the fields above the Container Description field, and I would then be able to collect these values and store them in localStorage.
Once I have all the data needed for the form, I would pilot the form using the userscript in ViolentMonkey and the data stored in localStorage, to persist data across page refreshes.
Other Alternatives:
The eventListener idea on an element doesn't work, because ASP.NET updates the page with fresh code every time a field is verified, wiping out the listener. It also doesn't trigger a refresh, so ViolentMonkey doesn't rerun my userscript.
My other thought was to modify doPostBack(). The doPostBack() function looks like this (as far as I can tell):
<script type="text/javascript">
var theForm = document.forms['formNewContainer'];
if (!theForm) {
theForm = document.formNewContainer;
}
function __doPostBack(eventTarget, eventArgument) {
console.log("Form submitted");
}
</script>
It is called on verified fields with the following onchange handler:
onchange="javascript:setTimeout('__doPostBack(\'ctl00$newContPage$txtBarcode\',\'\')', 0)"
My goal would be to modify doPostBack() to save the information I need to localStorage before executing the rest of doPostBack() without changing it.
(Note: doPostBack() here looks incredibly simplistic, so I think I'm missing some information about how ASP.NET works here. This is outside of the question though, unless it's relevant for what I'm trying to do.)
I was able to successfully modify doPostBack() in this way using Chrome Local Overrides to serve myself a local copy of the page on page load, instead of the server version. But this only works for the first doPostBack() request. After the first request, the server serves me new code. Like with ViolentMonkey, the lack of a refresh trigger prevents Chrome Local Overrides from re-serving my local copy, and I'm served code without the doPostBack() modification.
So that's where I'm at. I'll try adding a global listener like #wOxxOm suggested, and see where that gets me.
I ended up using a Chrome extension called "Run Javascript" (has an elephant for it's logo), which runs the JavaScript code even on AJAX requests.
Link: https://chrome.google.com/webstore/detail/run-javascript/lmilalhkkdhfieeienjbiicclobibjao/
I don't see how this is possible at all. You need to work with the people that created that web page.
Asp.net and the server side code will be EXTENSIVE .net code (c# or vb.net). Each of those events you trigger will set variables and server side session (or viewstate) values for the code behind to run.
That's how asp.net pages work. You post back, page travels up to server, THEN the .net code behind runs. That code will modify the page, modify controls, and modify the view state for that page. And after that code runs (say on a button click), then you client side will receive a whole new fresh page - that will blow out any JavaScript you try and inject. (you would have to re-inject each time). But, it gets worse, since quite of bit of that code behind also checks and often will NOT tolerate that the page settings have been messed with, and will be rejected.
About the only way to do this would be to write some desktop software, and that software would "house" or "host" a full "com" object copy of the web page, and you thus automate that given page. (and even then, you still fighting a losing battle).
Hint:
Web development, business logic, and a functional business applcation is NOT some simple markup and JavaScript (despite what that lame 2 week HTML course tells you).
This is a application, and asp.net applcation. Trying to think of this as just some markup and JavaScript is actually quite silly here. It not how you write, or build business solutions for a company.
If you can't write and modify the code and the web server side of things then find out if that site has some kind of web api or whatever.
But, really - this is silly, and unless this is some simple college project, or some hacked up html page and some JavaScript? Forget this approach - you dealing with FAR too much server side and code behind on the server.
In fact, asp.net as noted has quite a bit built in features that check if the page being posted back been messed with, and you never really be sure that you set values and that the proper amounts of code behind that runs to setup row values, database primary key values and a WHOLE boatload of state values that are probably 100% saved in server side session() based class objects - and objects that are never exposed server side.
Tring to supposed modify or assume you can create or modify such a system with only client side tools is not going to work - its just not.
code behind runs, it re-processes the page with .net code and then sends the whole page back down - all with new state values etc. This is not some lame html + JavaScript, but is a full server side code driven system written in c# .net code.

UserEvent script running on browser?

I have few questions related to NetSuite:
On the NetSuite help page, the User Event scripts are stated to execute on servers.
In that case if a record is updated in NetSuite indirectly*, it should still trigger the User Event script associated with it, right?
*By indirectly I mean a user does not navigate to the record in a browser and clicks on Edit and Save. An example usecase would be, when a customer payment is made against an Invoice, it updates the Amount due on the Invoice automatically. But it does not trigger the UserEvent Script deployed on the Invoice.
Please let me know if my understanding is correct. Also, could you please give me a way to execute a script when a record is updated both directly and indirectly.
Is there a way to execute a script (make a REST call) when a File is uploaded. I am not able to see it under script deployments for User Event scripts.
Thanks in advance!
The event type that you mentioned, it will not trigger a user event script. Just like the user event script deployed on sales order, it will not be executed when the sales order got fulfilled. Based on my experience changes on the status of a transaction caused by creating another transaction will not trigger UE script. You can deploy a UE script too on Customer Payment record that would process the invoice that is being paid.
File record is not yet supported in SuiteScript. When I say not supported, you cannot deploy a script to it.
1) This is correct yes. You can however select "Execute in commerce context" on your UE script, which means the script will be triggered if a Sales Order is created from outside of NetSuite, on a web store for instance - which is integrated with the NetSuite account.
2) Don't you want to send the file from NetSuite, rather than polling for it from a different system?
You will then need to first load the record as follows in your script(SuiteScript 2.0):
var curRec = scriptContext.newRecord;
curRec = record.load({
type: record.Type.PURCHASE_ORDER,
id: curRec.id,
isDynamic: false
});
curRec will contain all detail of the record.

Will using AJAX to update database before form submit allowing me to insert newly edited data into new mysql table

I currently have a web-page built in flask where I am selecting data from a mysql database.
I am going to use Javascript (probably inline-edit) to allow the user to edit some text on the web-page.
When the user leaves edit mode, I am going to use ajax to update the table in the database.
However this data will also be within a form, where there will be a submit button and when this is pressed it will run a query that inserts the data into a new table in the database.
My question is;
Will the AJAX inlin-edit function have updated the data (run before the form submit) in the current table before the form submit to the new table as I want the newly edited data in the new table.
I was originally concerned about the asynchronous part but read here that this is not to be worried about.
I am also unsure whether the new data will be available in the new table from the db.
The fact that the linked question says you functionally don't have to worry about asynchronous, doesn't mean you shouldn't technically worry about it.
If you fire of a normal AJAX request after editing, you send a command to the backend. Normally, in async-mode, you can just press 'submit'. If, for some reaon, the thread handling your first request is slower, you will NOT have your data in your database yet.
So yeah, you should 'worry' (read: take care) of that.
You could do any of the following
Call your 'AJAX' synchronously. this is a bit strange, but you could do that
Disable your 'submit' button until your ajax has returned
but in the end, you should just look at the flow: you are calling a function and then later another fucntion. If the first should be finished before the second is called, make sure it is. If you just fire off your second (submit), then it's not going to be sure it'll work.

Microsoft Dynamics CRM run script (or something similar) in background

I wrote a script that adds the values of field A and field B. It then writes the result in field C.
My problem now is that, as I'm using a script, it only runs if I the form is open. However, I need to run it whenever field A or field B changes, regardless whether the form is open or not. For example, if a workflow changes the value of field A in the background, I need the script to calculate the new value of field C in the background as well.
I know that scripts only run on forms. That's why I'm looking for an alternative for scripts. I am aware that I could normally solve this by using a workflow, but I can't access field A through the workflow (it's a calculated field).
Are there any other possibilities?
I find that for custom calculations the following approach is effective and doesn't require much effort to implement and mantain:
Create an ACTION (let's call it new_action) without any steps
Create a PLUGIN which does the math you want, register it to the new_action message
Identify all the Simple fields involved
Create a workflow for each entity where you found the fields, set it to run on Update of the Simple fields you identified in that entity
The workflows should all be the same and the only step would be EXECUTE ACTION -> new_action
The end result is this behavior:
Simple field involved in your math changes -> Workflow starts -> Action starts -> Plugin does the math
NOTE: I usually make the action Unbound and "hand-craft" the data received by both the action and the plugin, but I glossed over this aspect because the approach itself stays the same.
Use CRM plugin. It works on server side so it will run whenever fields values will change (user interface, workflow, system process, CRM API call, etc.).
Detailed information: https://msdn.microsoft.com/en-us/library/gg328263.aspx
1.Create an update message plugin with filtering attribute as A and B,
This will fire only when your form is updated and also only when attribute A and B has changed on that form.
2.write your logic in your plugin
3. choose async and sync depending on how frequent the changes are on A and B, ideally, I will use sync so that my value of C gets updated and be in sync all the time w.r.t A and B.

Getting a record's guid before save operation

I need to use the GUID of a record. If the record is old, i can access to the guid with
Xrm.Page.data.entity.getId();
The issue is that I need to use the ID in the same moment that a new record is saved.
I try to save the record, reload the page and then use the id with
parent.Xrm.Page.data.entity.save();
window.location.reload(true);
var currentCaseId = Xrm.Page.data.entity.getId();
but it does not work.
Edit: I change my code so now I can get the ID in the onLoad event but now my problem is that I can only get the GUID if the user select "Save" (cause "save" button refresh the page). If the user select "Save&Close" or "Save&New" the record doesn't refresh so the onLoad event doesn't happen and I can't get the GUID.
You get a guid for an instance first when you actually create it. So, you need to actually perform the save operation before the server returns to you with the guid.
If you need to do something to the entity just before it's being saved (or maybe after the save but before postback to the client) you might want to intercept the operation using a plugin.
The optimal approach will depend on the actual need and that's depending on the reason for you trying to get to the guid in the first place. What exactly do you intend to do with the "early guid"?
Does it have to be in JavaScript at all?
If you create a record in crm the primary key is created by crm and is returned to you. You can create a guid yourself and assigning it before saving. Now you know the Key on forehand.
If the record is in create mode I am using JavaScript and XrmServiceToolkit to create new record with all posible attributes. This gives me the needed GUID for another actions in the background. Then I prevent save of the original form and open the newly created record in the same window. It looks exactly the same as regular saving, but it does what you wanted.
I had a similar problem. This might not be the industry best practise, but it works: I do the autosave (Xrm.Page.data.entity.save();) before user has a chance to save(andclose or saveandnew). I do the autosave after user has entered all required fields. Guid is then generated and usable on the onload-event.

Categories

Resources