Reputation: 71
I have a sub-grid dynamics 365 crm named competitors I have to get data from that sub-grid and print it into alert. I have searched in the net but every solution I get from there when I used is throwing error as deprecated method. How can I get the sub-grid data in dynamics 365 application using JavaScript. Can anyone provide sample code for that. Thanks in advance
Upvotes: 0
Views: 3848
Reputation: 71
I found my answer already - I have added an onload event to subgrid via JavaScript and retrieved data using Xrm.Page.getControl(“entityname”).getGrid()
.
Upvotes: 0
Reputation: 11
As AnkUser has eluded to you need to retrieve the associated records from your subgrid adding in the <attribute name=''>
tags as needed for the columns you need.
Remember a sub-grid is simply a view of data, usually, associated with the given record.
What specifically are you trying to retrieve from the subgrid?
Upvotes: 0
Reputation: 5531
Here is below code I tried in one of my Instance. Considering you want alert on Opportunity entity and that too onload of a form.
Use Debugger and you will get the data.
function onLoad(executionContext){
debugger;
var formContext = executionContext.getFormContext();
var formType= formContext.ui.getFormType();
if(formType && formType!==1 && formContext.data.entity.getId()){
retreiveOppCompRecords(formContext.data.entity.getId().replace(/[{}]/g, "")).then(
function success(result) {
if(result.entities.length > 0){
for(i=0;i<result.entities.length;i++){
}
}
},
function (error) {
var alertStrings = {
confirmButtonLabel: "OK",
text: 'Failed at Function: enableDisableExpiryDate with error: ' + error.message
};
var alertOptions = {
height: 250,
width: 450
};
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}
);
}
}
function retreiveOppCompRecords(recordId){
var fetchxml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">',
'<entity name="opportunity" >',
'<attribute name="name" />',
'<filter type="and" >',
'<condition attribute="opportunityid" operator="eq" value="' + recordId + '" />',
'</filter>',
'<link-entity name="opportunitycompetitors" from="opportunityid" to="opportunityid" alias="OppCompetitor" intersect="true" >',
'<link-entity name="competitor" from="competitorid" to="competitorid" link-type="inner" alias="Competitor" >',
'<attribute name="websiteurl" alias="CompetitorWebsite" />',
'<attribute name="name" alias="CompetitoName" />',
'</link-entity>',
'</link-entity>',
'</entity>',
'</fetch>'].join("");
fetchxml = "?fetchXml=" + encodeURIComponent(fetchxml);
return Xrm.WebApi.retrieveMultipleRecords("opportunity", fetchxml);
}
Upvotes: 2