how to pass softcoded element to crm web resource script - dynamics-crm

I'm having trouble passing in the name of an element into a Dyanamics CRM web Resource javacript.
This code works:
function OnFormLoad()
{
var subGrid = window.parent.document.getElementById("Claims")
// do work
}
This code doesn't:
function OnFormLoad(GridName)
{
var subGrid = window.parent.document.getElementById(GridName)
// do work
}
How do I pass in the name of the element I want to work with?

Please refrain from using document.getElementById in Dynamics as it is not supported.
I believe you are trying to get GridContext and get Data from that Grid.
For Example on Account entity we have Contacts as Grid and then you wish to get data from that Grid.
I replicated the same on Account Entity (OnLoad) and get tried to get data from Contacts Grid.
When adding OnLoad event I have passed Grid name as Parameter as below.
I have added below onLoad Js on Account entity and was able to retrieve data from grid.
Note: I have added timeout because directly firing onload was not able to load complete page and then grid Name was not available.
function onLoad(executionContext,gridName){
setTimeout(function(){ getGridDatat(executionContext,gridName); }, 3000);
}
function getGridDatat(executionContext,gridName){
debugger
var formContext = executionContext.getFormContext();
var gridContext = formContext.getControl("Contacts"); // get the grid context
var myRows = gridContext.getGrid().getRows();
/*var myRow = myRows.get(arg);
var gridRowData = myRow.getData();*/
var firstRow =myRows.get(0).getData();
var firstRowAllAttributes = firstrow.entity.attributes.getAll()
var firstRowfirstAttributeValue = firstrow.entity.attributes.get(0).getValue()
}
If you want to perform some operation on change of data formGird then there is one more way to achieve this. Make your grid as Editable and then you can find Events for that gird as below and could perform your operations.

Related

Get all business recommended fields in subgrid

How do I check if a particular field or column of a subgrid is business recommend or not? I want to do this using a web resource. Also due to some requirements, I will have to use the execution context of the form where the subgrid is present and not of the subgrid itself.
It is a little tricky because at the time that the form loads, the subgrid does not have data. So you have to use the Form's load event to attach a load event to the subgrid.
This is described in this MS Docs Page. You can do this like
function attachGridEvent(executionContext)
{
var formContext = executionContext.getFormContext();
var gridContext = formContext.getControl("gridCategories");
// We have the grid, now add a "load" event handler
gridContext.addOnLoad(MyGridLoadedEvent);
}
Now you've got a 'load' event for your grid so you can iterate through its rows and examine its data
I haven't been able to get this to work for subgrids that don't contain data
I get the first row in the sub grid. Once I have that we can loop through each of the row's attributes. Each attribute has the following methods:
getName Returns the logical name of the attribute of a selected grid
row.
getRequiredLevel Returns a string value indicating whether a
value for the attribute is required or recommended.
setRequiredLevel Sets whether data is required or recommended for the
attribute of a selected grid row before the record can be saved.
getValue Retrieves the data value for an attribute.
setValue Sets the
data value for an attribute.
MS Docs Page
I'm using some modern browser features (map, =>) but this code should work for you
function MyGridLoadedEvent(evt)
{
var gridContext = evt.getEventSource();
var rows = gridContext.getGrid().getRows();
if (rows.getLength() > 0)
{
let rowAttributes = rows.getByIndex(0).getAttribute();
let mappedResults = rowAttributes.map(x => x.getName() + " : " + x.getRequiredLevel());
alert(mappedResults);
}
}

Google Map not Updating By Onchange Event In Dynamics 365

In Dynamics 365(version:8.2), I am loading google map using web resource and then trying to initiate google map by onchange event from CRM input field but it is showing error alert message:
ReferenceError: google is not defined at initMap
I got the solution by finding iframe and then call google map.like this:
var description = Xrm.Page.getAttribute('new_street').getValue();
// Get the HTML iFrame object.
var iFrame = Xrm.Page.ui.controls.get('WebResource_Map').getObject();
// Get the element from the iFrame.
var element = iFrame.contentWindow.document.getElementById('addr_line1');
var ifr = iFrame.contentWindow;
// Set the element's value.
element.value = description+' ';
I need help on this.
Here's how I would suggest you do it:
From your script that loads the google maps (Web Resource), get the form attribute values (address fields).
Whenever you need to refresh the map in case of any field change, just reload the web resource that's embedded on your form and it should automatically call the google maps again resulting in your new location being set.
You can call the reloadMapOnFieldChange() on change of the fields for which you want the map to be refreshed
function refreshWebResource(name) {
var _webResourceControl = Xrm.Page.getControl(name);
if(_webResourceControl!=null && _webResourceControl!="" && _webResourceControl!=undefined)
{
var _src = _webResourceControl.getSrc();
_webResourceControl.setSrc(null);
_webResourceControl.setSrc(_src);
}
}
function reloadMapOnFieldChange(){
refreshWebResource(<Name of your Map Web Resource>);
}

Firefox addon: how to grab data from webpage?

Purpose
I am trying to make a common answer database for a website form. The form is a description of computer hardware specifications, and I am trying to make it so that based on a model field, other fields are filled in automatically. The extension with have two buttons, "populate" which will based on the model field, check the database for a matching entry, and then populate data into the forms based on that. The second button being "save", which will take data in the fields and marry it to the model field and place it into the database.
Question
So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?
So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?
You can do this with a Firefox Add-on SDK Page-mod, click here for the documentation
Here is an example for getting data:
Example Page-Mod
/lib/main.js:
var tag = "p";
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.mozilla.org",
contentScriptFile: data.url("element-getter.js"),
onAttach: function(worker) {
worker.port.emit("getElements", tag);
worker.port.on("gotElement", function(elementContent) {
console.log(elementContent);
});
}
});
/data/element-getter.js:
self.port.on("getElements", function(tag) {
var elements = document.getElementsByTagName(tag);
for (var i = 0; i < elements.length; i++) {
self.port.emit("gotElement", elements[i].innerHTML);
}
});

Add or trigger event after inner content to page

I have links on a table to edit or delete elements, that elements can be filtered. I filtered and get the result using ajax and get functions. After that I added (display) the result on the table using inner.html, the issue here is that after filtering the links on the elements not work, cause a have the dojo function like this
dojo.ready(function(){
dojo.query(".delete-link").onclick(function(el){
var rowToDelete = dojo.attr(this,"name");
if(confirm("Really delete?")){
.......
}
});
I need to trigger the event after filtering, any idea?
(I'm assuming that you're using Dojo <= 1.5 here.)
The quick answer is that you need to extract the code in your dojo.ready into a separate function, and call that function at the end of your Ajax call's load() callback. For example, make a function like this:
var attachDeleteEvents = function()
dojo.query(".delete-link").onclick(function(el){
var rowToDelete = dojo.attr(this,"name");
if(confirm("Really delete?")){
.......
}
});
};
Then you call this function both in dojo.ready and when your Ajax call completes:
dojo.ready(function() { attachDeleteEvents(); });
....
var filter = function(someFilter) {
dojo.xhrGet({
url: "some/url.html?filter=someFilter",
handleAs: "text",
load: function(newRows) {
getTableBody().innerHTML = newRows;
attachDeleteEvents();
}
});
};
That was the quick answer. Another thing that you may want to look into is event delegation. What happens in the code above is that every row gets an onclick event handler. You could just as well have a single event handler on the table itself. That would mean there would be no need to reattach event handlers to the new rows when you filter the table.
In recent versions of Dojo, you could get some help from dojo/on - something along the lines of:
require(["dojo/on"], function(on) {
on(document.getElementById("theTableBody"), "a:click", function(evt) {...});
This would be a single event handler on the whole table body, but your event listener would only be called for clicks on the <a> element.
Because (I'm assuming) you're using 1.5 or below, you'll have to do it a bit differently. We'll still only get one event listener for the whole table body, but we have to make sure we only act on clicks on the <a> (or a child element) ourselves.
dojo.connect(tableBody, "click", function(evt) {
var a = null, name = null;
// Bubble up the DOM to find the actual link element (which
// has the data attribute), because the evt.target may be a
// child element (e.g. the span). We also guard against
// bubbling beyond the table body itself.
for(a = evt.target;
a != tableBody && a.nodeName !== "A";
a = a.parentNode);
name = dojo.attr(a, "data-yourapp-name");
if(name && confirm("Really delete " + name + "?")) {
alert("Will delete " + name);
}
});
Example: http://fiddle.jshell.net/qCZhs/1/

FBJS...OnClick not being passed. DHTML/setInnerXHtml problem

thanks so much in advance...here is the problem...
I am trying to add dynamic HTML element (EX. [delete]),everytime on a event call using FBJS.I am able to append the element using following code.
var oldFriendHtml = document.getElementById('friend_container');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").getValue()-1)+2;
numi.value = num;
var newElementId = "new"+num;
var newFriendHTML = document.createElement('div');
newFriendHTML.setId(newElementId);
newFriendHTML.setInnerXTML("HTML TO BE ADDED");
oldFriendHtml.appendChild(newFriendHTML);
The problem I am facing is that FBJS parses out the onClick part (event call ) out from the original HTML added .This stops me in the further activity on the added element .It also removes the styling added in the HTML ..
Regarding onclick being parsed out of setInnerXHTML, you can add the event later by using addEventListener.
Here is some sample:
var my_obj = document.getElementById('test');
// add a new event listener for each type of event you needed to capture
my_obj.addEventListener('click',my_click_func); // in your case is onclick
function my_click_func(evnt){
// some action
}
more details see http://developers.facebook.com/docs/fbjs#events

Resources