Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Inogic Profile Picture
    599 on at
    RE: How to add connection to subgrid
    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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Holly Huffman Profile Picture

Holly Huffman 103

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 96 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 51 Most Valuable Professional

Product updates

Dynamics 365 release plans