Check if the Parameter has been created inside USD Data Parameters - javascript

I am trying to built a logic where I am running an action call with uii action CreateEntity. The action call creates a record in CRM Custom Entity. Now, the action call is being fired multiple times and that is because it is attached in the BrowserDocumentComplete Event which is being fired multiple times.
Now since that action call is being fired multiple times therefore multiple records are being created inside CRM. I want to make it stop after it creates the first record and what happens is if it creates a first record a parameter is created inside USD DataParameters.
So, I want to check through Scriptlet preferably that if the parameter lets name it RecordCreated has been created inside USD then make the action call stop.
Something like this if (RecordCreated Exists) then stop else run

Ideally BrowserDocumentComplete event won't get triggered multiple times in USD. Is it the PageLoadComplete event normally gets triggered twice. That is something which you need to check once before thinking of another solution.
Coming to your issue, if the browser document complete is not working as expected in your case, another option is to do a check whether the record is created before running the action call the second time. For that what you can do is, whenever an action call is executed you will get an object $Result in the USD data parameters. Look for the object and get the guid of the CRM record being created. You should be able to access that something like this,
$Result.<<Name of your action call goes here>>
In your same action call, check whether the output of the above code and see whether that is empty of a GUID. If its not empty that means your action call was executed previously, otherwise execute the action call to create the record.
Hope that helps.

I believe that BrowserDocumentComplete fires each time either a page or an iframe finishes loading. Instead, use an event that generally only fires once, like DataReady or PageReady. This assumes that DataReady and PageReady are available in your version of USD.
DataReady and PageReady can still fire multiple times during reload / refresh scenarios, so you can still have the same problem. To mitigate this, check for the existence of the data parameter that is created when the record is created. In the condition check, allow the replacement parameter to be replaced by an empty string when it doesn't exist, using the '+' modifier. If your Action Call is named "Create Custom Entity Record", then your condition expression will probably look something like this:
[[$Result.Create Custom Entity Record]+]===""
Now, even if the Action Call is attempted multiple times, it should only fire once and be prevented from firing subsequently. Subsequent attempts should be shaded yellow in Debugger, indicating "ConditionFailed".

Related

Rails implementation of input tag firing event on value change

I was wondering if there is some cleaner way to implement event firing on input tag value change (i want it to fire every time character is entered/deleted) and make these values visible in controller other than using ajax?
So the way I know now is implementing JavaScript snippet which would attach addEventListener to input element (like here) and would make ajax call to initial rails controller to pass the new input tag value. However ajax feels like too much as opposed to if there would be something native in rails, I just can't seem to find it.
If you need anything from browser going out to server (written in any language) without refreshing the page, Ajax is the way to go. Now-a-days, fetch would be another alternative, which works in similar manner.
If you need every key-stroke to be sent to server (controller), you should add a keyup event listener. In the event listener function, make an Ajax/fetch call to the endpoint, and return relevant information from your controller.
Do remember to include a timestamp in the requests, and send them back in response though. In most such scenarios, you'd want to be differentiate between "what is new" vs "what is stale", in case user types too fast. Due to network delays, responses typically do not come back in same order as the requests were made in.

Subscription in Tracker.autorun causes publish callback to fire multiple times

I'm working in a ReactJS and Meteor project and I found a strange behavior I'm gonna describe here:
There is a Tracker.autorun block with a Meteor.subscribe call inside. So far, so good. In the server side, there is a matching Meteor.publish which declares a callback.
As far as I understand, the Meteor.publish callback should fire once for each subscription received, but somehow this callback is firing 3~4 times for a single subscription.
In my last test the Tracker.autorun block executed 4 times, the subscribe only executed 1 single time and the callback fired 4 times.
The Meteor.subscribe only runs once, even the tracker runs several times. How could it cause the callback to fire more the once?
Does it make sense?
Do you know what could explain such behavior?
If you need any other information, just let me know.
Thanks in advance
Meteor.publish('current-user', function currentUser(credentials) {
return Users.find();
});
Tracker.autorun((c) => {
if (!currentUserHandler) {
currentUserHandler = Meteor.subscribe('current-user', this.credentials);
}
});
You should expect that the autorun will fire twice as a normal condition, once without data, and the second with some data.
That is to allow you to show a "loading" state before the data arrives.
You are subscribing to the users collection, which is a special collection. Meteor uses it for authentication, and also to record session activity. You are doing a Users.find(), which is an unfiltered query on the whole users collection, so any modification to any user will cause it to fire. You also won't be able to see all of the users records (for security reasons).
It's probable that you are storing additional data on the users record, hence the need for you to subscribe to it. I would recommend that you consider storing this data in another collection, such as 'members', 'visitors', 'profiles' or whatever name suits you. Things are likely to work better that way.

React setState: are multiple callbacks synchronized?

Say there is such a case:
In the state, there is a list, which corresponding to multiple rows in a table on the UI. There are multiple api calls, one for each item (row), which will retrieve the latest status, and update one item in the list.
In such case, I can understand that callback method will be better than direct call of setState. However, I still don't know whether multiple calls of the callback will be synchronized.
For example, whether the following situation will happen?
callback 1 reads list
callback 2 reads list
callback 1 updates list(0)
callback 2 updates list(1)
callback 1 writes back
callback 2 writes back
In such case, the update from 1 might be lost, which is typical for read-modify-write.
I still do not clearly understand your problem, perhaps if you could provide some code it would be better.
But if you meant to call a fetch function multiple times and setting the state once it's arrived, then you should be safe to assume that a setState should not override another, whether were your calls done asynchronously or synchronously.
But in any case, you shouldn't as a front-end send this much requests at once, you should request all the data from the server

Using ajax to load paired html and js files and keeping order

I have a page that uses Ajax to load in sections of a page.
Each section consists of a html files and a JavaScript file that defines events for just that bit of html.
What I am trying to do is figure a method of managing the file loading that can bind keep the two files bound together.
Here is pseudo-code of how I am attempting to make it work now but I don't know if there is a more organized way.
Pseudo-code: (using jQuery)
Cycle through list of html/js file pairs that need loaded.
Add an object to the an array that uniquely identifies the pair of files. This object will eventually hold the container for the html and the js object
Start loading of html. When html returns, append content to the page and record the id in the array object
This is easy because I use the content for the jQuery get callback directly.
Start loading the js. When the file loads, the js executes and updates the object in the array with a reference to the files return value.
*This is the hard part. jQuery.getScript() automatically executes the script when it completes, so I cant use the return value because its already created. Since I cant use the ajax response I have to have the js file already know the object it will be adding itself to
So, I was hoping there was some js lib already available that does some data-binding between pairs of html and js.
Also, I wasn't sure how to structure the object manager.
Each object in the js files are going to have the same events bound that get called when you move to that section.
Sorry this is kind of a loaded question.
I think my AJAX library might help:
http://depressedpress.com/javascript-extensions/dp_ajax/
One of the most useful features is the ability to define a single "request" (which fires a single handler) with multiple "calls" (individual HTTP calls). You can thus define a single request to contain both the call for the script and a second call for the HTML. When both calls are complete the defined handler will be called and get the results passed to it in the correct order.
The request pool used to process the requests can be instantiated with multiple background objects (allowing multi-threading) so that while the requests get all your data before calling your handlers they're not forced to waste time by single threading (although you can do that as well if you like).
Here's a simple example of how it might be used:
// The handler function
function AddUp(Nums) { alert(Nums[1] + Nums[2] + Nums[3]) };
// Create the pool
myPool = DP_AJAX.createPool();
// Create the request
myRequest = DP_AJAX.createRequest(AddUp);
// Add the calls to the request
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [5,10]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [4,6]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [7,13]);
// Add the request to the pool
myPool.addRequest(myRequest);
"myRequest" defines the handler. The addCall() methods add multiple calls. Once all three are results will be sent to the handler as an array of three responses (in the same order that the calls were added).
You could calls your files in pairs or even call them all bundled into a single request - up to you.
Hope this helps!

pause current ajax call , carry out a new one , after carrying out , continue the default ajax call

i'm not sure this is possible.
I have two ajax calls executed during a button click event.
first ajax call is to add data to database using jquery ajax post .
the second one is to add another set of data to database too via jquery ajax post too
The first one will execute first then the second one. i have set a timer to the second call(windowstimeout) to create a time interval between both execution to test if i can pause the first ajax call.
You will be asking me why i don't want to combine both calls. i have the reasons to do, and i want to know is it really possible to pause an ajax call. I have search around the net , all i found is the ajaxstop(jQuery) but it don't really pause it , it stops the ajax call.
SO anyone have any idea to do so? Thanks.
Why do you want to pause it? You can't, but why would you want to? Just let them both run, or don't trigger the first call till the second is done.
No, you cannot pause Ajax calls - they are just instances of the normal HTTP request/response cycle and behave as such. If the server-side operations resulting from the second call depend on the results of the first one, then why don't you just chain the calls and trigger the second one in the first's success callback handler?
You cannot do that.
You should really ask yourself if the design of your data exchange protocol is correct.
Pausing a request (whatever the communication protocol is, AJAX or any other) should not work since you cannot know if there are other requests and what they do.
So ask yourself the questions: what is my exact need ? what are the other ways to do that ?

Categories