Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
1
Indexable
Never
 RetailerInvestment.OnChangeOfRetailer = function (executionContext) {
            var formContext = executionContext.getFormContext();
            var fundtype = InTouchCRM.Framework.GetValue(formContext, "bmc_fundtype");
            var retailerInvestmentStatus = InTouchCRM.Framework.GetValue(formContext, 'bmc_retailerinvestmentstatus');
            var listofBilltoFields = ["bmc_marketcoordinator", "bmc_moraccount", "bmc_tr", "bmc_businessunit", "bmc_asm", "bmc_rsd", "bmc_vp", "bmc_storephone", "bmc_account", "bmc_address1street1", "bmc_address1street2", "bmc_address1street3", "bmc_city", "bmc_stateprovince", "bmc_zippostalcode", "bmc_country", "bmc_retailerbusinessconsultant"];
            var retailerId = InTouchCRM.Framework.GetValue(formContext, 'bmc_retailer');
            if (retailerId != null && retailerId != undefined) {
                var id = retailerId[0].id;
                let loginUserId = InTouchCRM.Framework.FormatID(Xrm.Utility.getGlobalContext().userSettings.userId);
                let expandQuery = "&$expand=parentaccountid($select=accountnumber),owninguser($select=fullname),owningteam($select=_bmc_pointofcontact_value)";
                let marketingSaleRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.MSC);
                let territoryRepresentativeRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.TR);
                let dealerBusinessConsultantRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.DBC);
                let areaSalesManagerRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.ASM);
                let NationalAccountsRepRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.NAR);
                let systemAdministratorRoleExist = InTouchCRM.Framework.HasCurrentUserRole(UserRole.SA);
                let error = "";

                if (!systemAdministratorRoleExist) {
                    if (marketingSaleRoleExist) {
                        expandQuery = "&$expand=parentaccountid($select=accountnumber),owninguser($select=fullname;$expand=businessunitid($select=_bmc_marketcoordinatorid_value;$filter=_bmc_marketcoordinatorid_value eq '" + loginUserId + "')),owningteam($select=name,_bmc_pointofcontact_value;$expand=businessunitid($select=_bmc_marketcoordinatorid_value;$filter=_bmc_marketcoordinatorid_value eq '" + loginUserId + "'))";
                        error = "This account does not fall in your business unit. Therefore, you are unable to create a Retailer Investment Record for this retailer.";
                    } else if (territoryRepresentativeRoleExist || dealerBusinessConsultantRoleExist) {
                        expandQuery = "&$expand=parentaccountid($select=accountnumber),owninguser($select=fullname;$filter=systemuserid eq '" + loginUserId + "'),owningteam($select=name,_bmc_pointofcontact_value;$filter=_bmc_pointofcontact_value eq '" + loginUserId + "')";
                        error = "This account does not fall in your business unit. Therefore, you are unable to create a Retailer Investment Record for this retailer.";
                    } else if (areaSalesManagerRoleExist) {
                        if (fundtype == FundType.FPFFranchisePainter) {
                            expandQuery = "&$expand=parentaccountid($select=accountnumber),bmc_nationalaccountrep($select=fullname,_parentsystemuserid_value;$filter=_parentsystemuserid_value eq '" + loginUserId + "')";

                        }else
                            expandQuery = "&$expand=parentaccountid($select=accountnumber),owninguser($select=fullname,_parentsystemuserid_value;$filter=_parentsystemuserid_value eq '" + loginUserId + "'),owningteam($select=name,_bmc_pointofcontact_value,_bmc_manager_value;$filter=_bmc_manager_value eq '" + loginUserId + "')";
                        error = "This account does not fall under your team. Therefore, you are unable to create a Retailer Investment Record for this retailer.";
                    } else if (NationalAccountsRepRoleExist) {
                        expandQuery = ",_bmc_nationalaccountrep_value&$expand=parentaccountid($select=accountnumber),owninguser($select=fullname),owningteam($select=_bmc_pointofcontact_value),bmc_nationalaccountrep($select=systemuserid;$filter=systemuserid eq '" + loginUserId + "')";
                        error = "Since you are not set up as the designated \"National Account Rep\" for this account, you wont be able to create a Retailer Investment Record. Please get in touch with your Market Sales Coordinator who will be able to assist you in creating the record.";
                    }
                }

                Xrm.WebApi.online.retrieveRecord('account', id, '?$select=parentaccountid,_ownerid_value,telephone1,accountnumber,address1_line1,address1_line2,address1_line3,address1_city,address1_stateorprovince,address1_postalcode,address1_country' + expandQuery)
                    .then(
                        function success(result) {
                            var number = result.accountnumber;

                            if (!systemAdministratorRoleExist) {
                                let reatilerAttr = formContext.getAttribute("bmc_retailer");
                                if (marketingSaleRoleExist && !(result?.owninguser?.businessunitid?._bmc_marketcoordinatorid_value || result?.owningteam?.businessunitid?._bmc_marketcoordinatorid_value)) {
                                    Xrm.Navigation.openAlertDialog(error);
                                    reatilerAttr.setValue(null);
                                    reatilerAttr.fireOnChange();
                                    return;
                                } else if ((territoryRepresentativeRoleExist || dealerBusinessConsultantRoleExist) &&
                                    !(result?.owninguser?.systemuserid || result?.owningteam?._bmc_pointofcontact_value)) {
                                    Xrm.Navigation.openAlertDialog(error);
                                    reatilerAttr.setValue(null);
                                    reatilerAttr.fireOnChange(); return;

                                } else if (areaSalesManagerRoleExist && !(result?.owningteam?._bmc_manager_value || result?.owninguser?._parentsystemuserid_value || result?.bmc_nationalaccountrep?._parentsystemuserid_value)) {
                                    Xrm.Navigation.openAlertDialog(error);
                                    reatilerAttr.setValue(null);
                                    reatilerAttr.fireOnChange(); return;

                                } else if (NationalAccountsRepRoleExist && !result?.bmc_nationalaccountrep?.systemuserid) {
                                    Xrm.Navigation.openAlertDialog(error);
                                    reatilerAttr.setValue(null);
                                    reatilerAttr.fireOnChange(); return;
                                }
                            }
                            InTouchCRM.Framework.SetValue(formContext, "bmc_account", number);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_address1street1", result.address1_line1);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_address1street2", result.address1_line2);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_address1street3", result.address1_line3);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_city", result.address1_city);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_stateprovince", result.address1_stateorprovince);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_zippostalcode", result.address1_postalcode);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_country", result.address1_country);
                            InTouchCRM.Framework.SetValue(formContext, "bmc_storephone", result.telephone1);

                            var eligibleStatusForBOP = [RetailerInvestmentStatus.PendingSubmission, RetailerInvestmentStatus.PendingASMReview, RetailerInvestmentStatus.PendingCreditReviewApproval, RetailerInvestmentStatus.PendingRSDReview, RetailerInvestmentStatus.PendingFPAReviewApproval];

                            if (fundtype == FundType.MDFMarketDataFund || fundtype == FundType.HDWHardware || (fundtype == FundType.BOPBranchingOutProgram && !eligibleStatusForBOP.includes(retailerInvestmentStatus))) {
                                if (result.parentaccountid != null) {
                                    var parentaccountnumber = result.parentaccountid.accountnumber;

                                    if (parentaccountnumber != null && parentaccountnumber != undefined && fundtype != FundType.FPFFranchisePainter) {
                                        InTouchCRM.Framework.SetValue(formContext, "bmc_moraccount", result.parentaccountid.accountnumber);
                                        InTouchCRM.Framework.SetVisible(formContext, "bmc_moraccount", true);
                                    }
                                }
                                else if (result.parentaccountid == null) {
                                    InTouchCRM.Framework.SetValue(formContext, "bmc_moraccount", null);
                                    InTouchCRM.Framework.SetVisible(formContext, "bmc_moraccount", false);

                                }
                            }
                            // Set TR
                            if (NationalAccountsRepRoleExist && result._bmc_nationalaccountrep_value && result?.bmc_nationalaccountrep?.systemuserid) {
                                InTouchCRM.Framework.SetLookupValue(formContext, 'bmc_tr', result._bmc_nationalaccountrep_value,
                                    result["_bmc_nationalaccountrep_value@OData.Community.Display.V1.FormattedValue"],
                                    result["_bmc_nationalaccountrep_value@Microsoft.Dynamics.CRM.lookuplogicalname"]);

                            }
                            else if (result?.owninguser?.systemuserid) {
                                InTouchCRM.Framework.SetLookupValue(formContext, 'bmc_tr', result?.owninguser?.systemuserid, result?.owninguser?.fullname, result["_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]);
                            }
                            else if (result?.owningteam?._bmc_pointofcontact_value) {
                                InTouchCRM.Framework.SetLookupValue(formContext, 'bmc_tr', result?.owningteam?._bmc_pointofcontact_value, result?.owningteam["_bmc_pointofcontact_value@OData.Community.Display.V1.FormattedValue"], result?.owningteam["_bmc_pointofcontact_value@Microsoft.Dynamics.CRM.lookuplogicalname"]);
                            }

                            RetailerInvestment.OnChangeOfTr(executionContext);
                        },
                        function (error) {
                            Xrm.Utility.alertDialog(error.message);
                        });
            }
            else {
                for (var i = 0; i < listofBilltoFields.length; i++) {
                    InTouchCRM.Framework.SetValue(formContext, listofBilltoFields[i], null);
                }
            }
        },