Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

How to add connection to subgrid

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

  • Verified answer
    Inogic Profile Picture
    Inogic 24,031 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans