How to remove and sort an account group ascending

New Contributor III


If anyone needs to sort a specific account group by ascending. This is the codes it worked.

Sub RemoveandSortAccounts(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DashboardExtenderArgs)
	Dim ParentGroup As String = args.NameValuePairs.XFGetValue("ParentGroup")
	Dim osAccountMember As Member = BRApi.Finance.Members.GetMember(si, dimtypeid.Account, ParentGroup)
	Dim actDimPk As DimPk = BRApi.Finance.Dim.GetDimPk(si, "ST_Accounts")
	Dim osBaseList As list(Of Member) = BRApi.Finance.Members.GetChildren(si, actDimPk, osAccountMember.MemberId)
	Dim osBaseList2 As list(Of Member) = BRApi.Finance.Members.GetChildren(si, actDimPk, osAccountMember.MemberId)
	osBaseList2.Sort(Function(x, y) x.Name.CompareTo(y.Name))
	If osBaseList.Count > 0 Then
		For Each baseMember As Member In osBaseList 'osBaseList
			'===Remove first====
			Dim PId As Integer = BRApi.Finance.Members.GetMemberId(si, dimTypeId.Account, osAccountMember.Name)
			Dim myId As Integer = BRApi.Finance.Members.GetMemberId(si, dimTypeId.Account, baseMember.Name)
			Dim relationshipPks As New List(Of RelationshipPk)
			Dim RelPk As New RelationshipPk(actDimPk.DimTypeId, PId, myId)
			BRApi.Finance.MemberAdmin.RemoveRelationships(si, actDimPk, relationshipPks, True)

		For Each baseMember As Member In osBaseList2 'osBaseList2
			'===Add back ====
			Dim parentMem As MemberInfo = BRApi.Finance.Metadata.GetMember(si, DimType.Account.Id, osAccountMember.Name)
			Dim myMember As MemberInfo = BRApi.Finance.Metadata.GetMember(si, DimType.Account.Id, baseMember.Name)
			'retrieve rel info for parent child relationship
			'Dim myRelInfo As RelationshipInfo = BRApi.Finance.Members.GetRelationshipInfo(si, dimTypeId.Account, myMember.Member.Name, parentmem.Member.Name)  
			Dim newProperties As New VaryingMemberProperties(, mymember.Member.MemberId, parentmem.Member.MemberId)
			Dim relPk As New RelationshipPk(actDimPk.DimTypeId, parentMem.Member.MemberId, mymember.Member.MemberId)
			Dim rel As New Relationship(relPk, parentMem.Member.DimId, RelationshipMovementType.InsertAsLastSibling, 1)
			Dim relPostionOpt As New RelationshipPositionOptions()
			BRApi.Finance.MemberAdmin.SaveRelationshipInfo(si, True, rel, False, newProperties, relPostionOpt)
	End If
End Sub



Honored Contributor

Hey! Thanks for contributing, it's really appreciated. Maybe have a look at this howto, next time you want to post rules - everyone loves well-formatted code 😊