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