GTM custom javascript is not working, what to do? - javascript

I've created custom js function. It worked on console, however I tried to transfer it to GTM using custom js variable function, it returned undefined. The function transfers a string to integer and removes whitespaces.
function(){
a= parseInt(google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element").querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
return a}
it returns undefined((

No, the function does a lot more than you indicated. All the dom scraping The reason for it "not firing" in GTM may be due to timing. However, you're not supposed to use this kind of global scope code in GTM.
GTM kindly provides you with a {{Clicked Element}} which you can definitely use in your GTM custom JS code. So your code would become something like:
return {{Clicked Element}}.querySelectorAll("div>div>div>div")[27].querySelector("div>input").getAttribute("value").replace(/\s/g, ""));
Still, you can debug it. You can console.log your google_tag_manager["GTM-000000000"].dataLayer.get("gtm.element") from GTM and then debug it further in your console. The console nowadays allows you to copy a consol logged object, put it in a var and query against it.

Related

How to access flow variables from javascript in power automate

I'm working on a power automate flow and I'm having some trouble. I copied some text to my clipboard in the flow and i asigned it to a variable in my flow. I'm trying to parse the variable using the javascript scripting feature but i'm getting a syntax error whenever I try to use the %variable% syntax in javascript. I used the variable button in the text editor they give you to add the variable to my script but even something simple as var res = %SimResults%; returns an error C:\Users\$me\AppData\Local\Temp\Robin\nkwrgcdoxrp.tmp(1, 11) Microsoft JScript compilation error: Syntax error
It seems that even though I Should be able to access my flow variables from Javascript it throws a syntax error when I try
You'll need to put quotes around it ...
var res = "%SimResults%"
... it treats the variables as more of a find and replace within your Javascript code, therefore, you need to do the work to ensure the correct encapsulation, etc. exists.
This may not solve your problem though depending on the complexity of what is contained within the variable.
If you have additional quotes, etc. you may need to escape them prior to putting them in the Javascript task IF they exist within the string ... just be mindful of that.

Custom JavaScript log function

I am having some issues in my production code and I want to be able to send a flag to enable/disable JS logging, so I want to write my own logging function.
I want to use something like function log(){...}. When I looked up reserved words in JS I didn't see
log listed, but I do see it listed in the w3schools docs as a math function.
Is it okay to use log() as a function name in production code for IE 7+, Chrome, and FF?
Yes, it is absolutely fine. The maths log function is a function on the Math object, so will not collide with your implementation.
If you are confused by this sort of thing, look into JavaScript 'namespaces'
How do I declare a namespace in JavaScript?
Yes, it is ok. The math log function is available as Math.log, not log so you won't break anything...
That should be fine. The mathematical log function is called via Math.log(x).
Yes it is ok. The math log function always needs to be called as Math.log(). So you're fine.
I would suggest using an existing object or namespace rather than making your log function global.
See http://www.yuiblog.com/blog/2006/06/01/global-domination/

How to access controls from a console

I tried to execute the following command from the console.
var subject = Xrm.Page.ui.controls.get("subject");
That's the exact syntax I'm using in the web resource that I'm plugging in to CRM. However, I only got an error message saying that "unable to get property 'controls' of undefined or null reference".
I do understand the message. What I want to know is two-fold.
What syntax will work from the console (F12) to refer to the stuff on the screen?
Why doesn't it work the way I did? Where doesn ui come from?
I've checked that I can refer to both Xrm and Crm.Page but apparently ui is null (it's listed when I print out the contents of Page but sett to null).
I know this is a kinda old thread, but if you still getting that 'object doesn't support property..' error when executing the command from console, IE F12; try calling it from the frame i.e
frames[0].Xrm.Page.getAttribute("controlId").getValue();
In CRM 2013 it is a little different
frames[1].Xrm.Page
It's kind of tough to detect the frames across different browsers, so this little javascript can help you out:
for(var i=0;i<5;i++) //loop through 0 to 4
if(frames[i].Xrm.Page.ui != undefined) //check if undefined
{
Xrm = frames[i].Xrm; //assign Xrm
console.info("~: Xrm updated with frame " + i + " :~"); //show info
break; //breakout the loop
}
What it does ?
What it's basically doing is to loop through 0-5 to find frame where
Xrm.Page.ui is not undefined, once it gets it it assigns it to the Xrm and breaks the loop.
How to use ?
To use it just copy/paste and run in the browser console once per
session then after you can run/test all your Xrm codes form the browser console.
This works for me Xrm.Page.getControl("controlId"). It's just a shortcut for what you have already though...cant-disable-set-to-read-only-protect-gray-out-etc-a-field
In addition to what #Daryl said, I can add that I use different syntax. For some reason, I don't get his to work either. Might have to do with different browser version or something. Instead try to execute this, if you still can't get it to work (although I must admit that his is shorter = better).
Xrm.Page.getAttribute("lastname").getValue();
The lastname parts is tested a minute ago on creation of an instance of entity Contact. I just put in a breakpoint inside a script that is executed onchange and while broken-pointed, I entered the command above to the console.
If neither approach works for you, you've got some weird problem with your CRM or browser.
A reason some people need this information is to access their own code. If you need to access your own methods from the console, in 2011, any global methods (or namespaces) in your javascript were also in forms[0]. Obviously, this is a bad idea, just from a naming standpoint. In forms v6+ any global objects or functions are in an object called customScriptsFrame inside frames[0] (or presumably whichever frame the Xrm is found).
frames[0].customScriptsFrame.myFunctionName();

Calling a function in a JavaScript file with Selenium IDE

So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.
However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.
Is it even possible for Selenium IDE to do this sort of thing?
If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.
Thanks!
If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:
Note that, by default, the snippet will run in the context of the
"selenium" object itself, so this will refer to the Selenium object.
Use window to refer to the window of your application, e.g.
window.document.getElementById('foo')
So if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().
This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.
If you use runScript, that should already run in the window's context.
This works for me.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("myJavascriptGoesHere");
Make sure your javascript works first before using it here!
Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()
See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )
https://stackoverflow.com/a/54887281/2143734

Accessing properties of window with Selenium

The page I am trying to test sets the value of "global variables". From the Firebug, I can access those as properties of the window object (e.g. window.foo).
From Selenium however, typeof selenium.browserbot.getCurrentWindow().foo always return "undefined", say when used in the condition of a waitForCondition. Any idea of what I could be doing wrong?
I think it cant be done, it could be done in greasemonkey using unsafeWindow, however that doesnt work in selenium. i tried to do it using
addLocationStrategy
zzz
return prompt(inWindow.a);
and than
click
zzz=xxx
but i cant access it. if you have any ideas how to do it let me know.
if its your page maybe you could keep that variable in some html control. its a hack, but .....

Categories

Resources