Unable to Invoke Inserted Javascript Fragment - blueprism

I am attempting to insert a Javascript fragment into a webpage and then invoke it using blue prism. The purpose of this is to analyse what elements are returned from a search to determine where to go next in the overall process flow.
I have tested the Javascript code on the intended website using the IE 11 developer console and it works without issue. The code is below in case it is useful.
function includes(stringToCheck, CharacterToSearchFor)
{
var found = new Boolean();
var splitString = stringToCheck.split("");
for (var index = 0; index < splitString.length; index++)
{
if(splitString[index] == CharacterToSearchFor)
{
return true;
}
}
return false;
}
function getPartners() //declare a function which can be called from BP. once called all code within the enclosing {} will be run
{
var searchResults = document.getElementsByClassName("findASolicitorListItem"); //search the web page for all elements with a specific tag and store them in a variable called searchResults.
if(searchResults.length == 0) // If the number
{
alert( "No Solicitors were found.");
}else if(searchResults.length == 1)
{
var innerSearchResults = searchResults[0].getElementsByTagName("span");
for(i = 0; i < innerSearchResults.length; i++)
{
var spanText = innerSearchResults[i].innerText.toString();
if((spanText != ""))
{
if(!includes(spanText, "|"))
{
alert("One Solicitor found. " + spanText);
}
}
}
}else if (searchResults.length > 1)
{
alert( "More than one solicitor was found. Manual Checking required.");
}
}
This is stored in a data item and is passed into the Navigate stage (Insert Javascript Fragment) parameter.
PrintScreen of Insert Javascript Fragment stage
When this stage is ran it successfully injects the Javascript functions into the webpage.
I then try and invoke this inserted javascript fragment
Printscreen of Invoke Javascript Function stage
When this stage runs I get the following error message raised by Blue Prism.
Internal : Failed to perform step 1 in Navigate Stage 'Analyse Result' on page 'Analyse Search Results' - Failed while invoking javascript method:Exception from HRESULT: 0x80020101-> at mshtml.HTMLWindow2Class.IHTMLWindow2_execScript(String code, String language)
at BluePrism.ApplicationManager.HTML.clsHTMLDocument.InvokeJavascriptMethod(String methodname, String jsonargs, Object& retval, String& sErr)
I have searched for this error code and found this answer which indicates there is a problem with the code however I can manually run this code just fine.
Does anyone have any experience with using these methods in BluePrism or have seen this error message before who can help me resolve?

I was actually never able to get Invoke Function working reliably with parameters, I always use Insert Fragment for everything, invoking included.
If you insert this function as a fragment...
function sayHello(name)
{
alert("Hello " + name + "!");
}
...to invoke it you just insert this as another fragment:
sayHello("World");
Tadaa!
As a side note, I am not sure which element in Application Modeler you are using for fragment insertion, but it seems like you are using the root (application) node. I had better experience with inserting the fragment to a dedicated HTML BODY element, for some reason the performance is much greater.

To invoke function by action "Invoke Javascript function", in Arguments field you should put arguments in JSON syntax. If there is no argument you put "[{}]".
On the above Marek's example the function should look:
function sayHello(name)
{
alert("Hello " + name.name + "!");
}
and arguments: "[{'name':'world'}]".

Related

How do I transfer rich text attachments from one document to another

I have 2 documents which are supposed work in such a way to be able to transfer files between them. However, it doesn't work. The exception I get is Exception occurred calling method NotesDocument.save() Notes error: One or more of the source document's attachment are missing. Run Fixup to delete the document in the source database. This happens after I try to call save() function on the document to which I've just transfered files from the first.
The functions are below:
function transferFiles(docToGetFrom, docToTransferTo, fileFieldFromFirstName, fileFieldFromSecondName)
{
var rit1:NotesRichTextItem = getFirstNotesRichTextItem(docToGetFrom, fileFieldFromFirstName);
docToTransferTo.copyItem(rit1, fileFieldFromSecondName);
deleteAllFilesFromDocument(docToGetFrom, fileFieldFromFirstName);
docToTransferTo.save();
}
function getFirstNotesRichTextItem(documentToGetFrom, fileFieldName)
{
if (documentToGetFrom == null)
{
return(null);
}
if (!documentToGetFrom.hasItem(fileFieldName))
{
return(null);
}
var rit1:NotesRichTextItem = documentToGetFrom.getFirstItem(fileFieldName);
return rit1;
}
function deleteAllFilesFromDocument(documentToDeleteFrom, fileFieldName)
{
var arr = getAllEmbeddedObjects(documentToDeleteFrom, fileFieldName);
for(var i = 0; i < arr.length; i++)
{
arr[i].remove();
}
documentToDeleteFrom.save();
}
function getAllEmbeddedObjects(documentToGetFrom, fileFieldName)
{
var rit1:NotesRichTextItem = getFirstNotesRichTextItem(documentToGetFrom, fileFieldName);
if (rit1 == null)
{
return(null);
}
try
{
var arr=rit1.getEmbeddedObjects();
return arr;
}
catch(e)
{
return(null);
}
}
According to plain logic, I need to do the following in order to make it work:
Get attachments from the document A
Copy them to the document B
Remove attachments from the document A
Call save() on A
Call save() on B
I did exactly the same, but nevertheless get this nasty exception. Also, I've tried to solve the problem by setting OLEDisableFX to 1, but with no luck. I assume that something must be wrong with the method copyItem() (I guess it can only work properly with simple datatypes). What's the problem? Thanks in advance.
You'll probably need to detach the attachment from the source document and attach it to the target document. See the NotesEmbeddedObject class for examples.
Use the CopyItemToDocument method of the NotesItem class. The following is some code I used in a LotusScript agent but the CopyItemToDocument method is also available in Java and SSJS.
If doc.Hasitem("RTF1") Then
Set item = Nothing
Set item = doc.getFirstItem("RTF1")
Call item.Copyitemtodocument(targetdoc, "targetRTF")
Call item.Remove()
End If

Value in xp:confirm is not being updated after partial refresh. Why?

On an xpage I calculate the message for an xp:confirm control:
var arr = viewScope.get("attachmentsAll");
if(arr.length>0){
return "";
}else{
return arr.length + " Are you sure want to upload the file?";
}
the viewScope is being updated after the event has been executed. I check this via a xp:text and I notice that this assumption is true.
<xp:text escape="true" id="computedField1"><xp:this.value><![CDATA[#{javascript:var arr = viewScope.get("attachmentsAll")
return arr.length + " number?"}]]></xp:this.value></xp:text>
The xp:confirm and xp:text reside in the same panel that is being partially updated after the event.
Can anyone explain me why the value for the viewScope variable is updated in the xp:text control and not in the xp:confirm control?
The main idea of my answer to your previous question was to put hidden input with computed value. What if you try to use <xp:this.script> instead of xp:confirm, and get the confirmation message from that hidden input the same way?
Update
The reason and the alternative solution that does not require to make any changes to existing xpage
It came out that the back-end instance of xp:confirm evaluates the new message correctly. The new value is even being sent to the browser with the response to ajax request. But one of the functions of XSP client module is built so that it won't update the querySubmit listener function if there already exists one with the same name. So, we are stuck with the old confirmation function that contains the old message. There is a way to override this behavior without breaking any other features. I have tried this and it works for me.
Create a new JavaScript library (client-side). Add the code:
if (!XSP._pushListenerTuned) {
XSP.__pushListener = XSP._pushListener;
XSP._pushListener = function x__pl(listeners, formId, clientId, scriptId, listener) {
if (scriptId && scriptId.endsWith("_confirm")) {
for (var i = 0; i < listeners.length; i++) {
if (scriptId == listeners[i].scriptId) {
listeners.splice(i, 1);
}
}
listeners.push(new this._SubmitListener(formId, listener, clientId, scriptId));
} else {
XSP.__pushListener(listeners, formId, clientId, scriptId, listener);
}
}
XSP._pushListenerTuned = true;
}
Attach your new library as resource globally via theme or as page resource on required page. I guess placing the above code as scriptBlock on a required page should also work. Now, any xp:confirm component on any page (if you used theme resource), or on particular page and everywhere after visiting this page (if you used page resource or scriptBlock), will work as naturally expected.

How to prevent global event handlers from firing caused by an API call

I have a custom module that uses Kentico API (DocumentHelper) to update certain fields of my document and then publish but I do not want it to trigger the event handlers that are linked to my document page type. I tried adding comments to .Publish("admin_edit") hoping that I can catch it from the WorkflowEventargs parameter but the VersionComment property always return null. Is there a way to accomplish this in Kentico?
update field:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn();
document.Publish("admin_edit");
}
event handler:
public override void Init()
{
WorkflowEvents.Publish.After += Publish_After;
}
private void Publish_After(object sender, WorkflowEventArgs e)
{
if (!string.IsNullOrEmpty(e.VersionComment) &&
e.VersionComment.Contains("admin_edit"))
return;
}
You always get null for Version information, because that is related to the 'Page versioning' events, specially for 'SaveVersion'. You can find more about that on this link. If you expand 'Properties' you will see which properties are populated for the specific event. In your case, you can try something like this, to add your message for last version and then check for that comment on 'Publish_After' event, see code bellow:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn(versionComment: "admin_edit");
document.Publish();
}
and then, in event handler, take last version and check for comment like this:
if (e.PublishedDocument?.VersionHistory?.Count > 0)
{
var lastVersion = e.PublishedDocument.VersionHistory[0] as VersionHistoryInfo;
if (lastVersion.VersionComment.Equals("admin_edit"))
{
return;
}
}
NOTE: In case that you have a lot of concurrent content editors, there is a chance that your last version is not version from API (someone changed content and saved it right after your API call made change). There is a low chance for that, but still is possible. If this is something that you will use often, you must take it in consideration. This code is tested for Kentico 11.

Set visible Tab or section in dynamics crm 2011

I'm a bit confused how to use that function. I'd like to hide/show tab and section by using this logic.
function setVisibleTabSection(tabname, sectionname, show) {
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null) {
if (sectionname == null)
tab.setVisible(show);
else {
var section = tab.sections.get(sectionname);
if (section != null) {
section.setVisible(show);
if (show)
tab.setVisible(show);
}
}
}
}
I didn't get how to invoke the function properly.
I created this function, also, a tab ("tab_8"), the section name is ("tab_8_section_1"), where I put the text field ("new_conf_report").
after that, I call this function (at the same place where a code was written) as setVisibleTabSection("tab_8", "tab_8_section_1", false);
moreover, I call this function "onLoad" event
however I got the next errors:
" var tab = Xrm.Page.ui.tabs.get(tabname); "'Xrm.Page.ui.tabs' - IS null or it isn't object
"tab.setVisible(show); " Object doesn't support this method
thanks in advance
You can debug your code. Just put the word debugger in JS code. When you launch the form press F12 in tab Script press "begin script" and you are able to step by step understand how the code is working.
If you are passing strings you have to call them like this:
setVisibleTabSection("tab", "Sectname", false);

Remove parent div if duplicate words

I'm using a service called Embedly to style my RSS feeds from Google Feedburner. I have an example code over her: JsFiddle
If you look closely you will see the source (CNN) at the end of every title. This is called .provider I would like to get rid of the whole div (.embed) IF the the word CNN is located elsewere (meaning duplicate) in the div, either .description or a
I tried many things, this is one of them really straight forward code:
$('.embed').each(function() {
if($('.embed a:first **could also be .description**', this).text() == $('.provider', this).text())
$(this).remove();
});
I cant figure out why its not working. I also used it with on and live click with no luck.
I just realized the 'embeds' are not there on document tready. I added a button with click event which you can click after the embedly has loaded in: http://jsfiddle.net/2VBSX/37/
You can use success event to filter provider class from the data like this :
EDITED
$('div.newscontainer').embedly({
key: ':3eccf441bf0f43acbb076da9817af27d',
success: function(oembed, dict) {
output = $(oembed['code']);
description = $(oembed['code']).find(".description").text();
var regex =new RegExp(output.find('.provider').text(),"i");
if(regex.exec(description) == null ) {
$(dict["node"]).parent().html(output);
}
output.find("a:eq(0)").text(); // First
output.find("a:eq(1)").text(); // Provider
}
});
Checkout this jsfiddle demo
Is it that what you want?
var regex = /CNN/;
$('.embed').each(function(index, element) {
if (regex.exec($('.embed a:first').text()) != null
&& regex.exec($('.provider').text()) != null) {
element.remove();
}
});

Resources