Skip to main content

Notifications

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
    Inogic 24,102 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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans