web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How to add connection to subgrid

(0) ShareShare
ReportReport
Posted on by 80

Hello dear fellas,

I need to write a script that would add the opportunity owner to sales team subgrid, i`ve been looking aroung and didn`t find anything on how to do it, please help.

Here is the code I`ve written:

function SetOpportunityOwnerAsSalesMember(executionContext) {

var formContext = executionContext.getFormContext();
if (formContext == null) {
formContext = Xrm.Page;
}
var userName = formContext.getAttribute("ownerid").getValue()[0].name;
var salesTeamGridContext = formContext.getControl("SalesTeam");


if (!IsUserExistsInSalesTeam(userName, salesTeamGridContext)) {
AddUserToSalesTeam(userName)
}


}

function IsUserExistsInSalesTeam(userName, gridContext) {
var result = false;
var gridRows = gridContext.getGrid().getRows();

gridRows.forEach(element => {

var salesTeamMemberName = element.getData().getEntity().getEntityReference().name

if (salesTeamMemberName.length > 0 && userName == salesTeamMemberName) {
result = true;
}
})

return result;
}

function AddUserToSalesTeam(user) {
//
console.log("added user to sales team")
}

Best regards,

Bohdan

I have the same question (0)
  • Suggested answer
    Inogic Profile Picture
    766 on at
    Hi Bohdan,

    We assume that you want to create a connection record of Opportunity Owner with the Opportunity record including the Sales Team role.

    We advise you to use Xrm.WebApi.createRecord(entityLogicalName, data).then(successCallback, errorCallback) method to create a connection record.

    Prerequisite for creating a connection record is to add record1 (opportunity) and record2 (user) lookup data including record2 role in an object which will be passed in create record method as shown in below code.

    var data = {}
    data["record1id_opportunity@odata.bind"] = "/opportunities(" + OpportunityGuid + ")";
    data["record2id_systemuser@odata.bind"] = "/systemusers(" + UserGuid + ")";
    data["record2roleid@odata.bind"] = "/connectionroles(" + SelectedRoleGuid + ")";
    Xrm.WebApi.createRecord(“connection”, data).then((result)=>{},(error)=>{});
     
    For reference please follow the link provided here.
     
    Hope this helps!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 143 Super User 2026 Season 1

#2
Hamza H Profile Picture

Hamza H 142 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 121

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans