Question

Macro Multiple Filtration

Hi Guys,

I am trying to filter product in opportunity based on location and sum the total for that location, then display it in printable template. Here is my code but its giving me three rows instead of just one. Please help

namespace Terrasoft.Configuration
{
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.ServiceModel.Activation;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using Terrasoft.Common;
    using Terrasoft.Core;
    using Terrasoft.Core.DB;
    using Terrasoft.Core.Entities;
    using Terrasoft.Core.Packages;
    using Terrasoft.Core.Factories;
    
    [ExpressionConverterAttribute("AtsGetLocationProductsSum")]
    class OpportunityProductSumByLocation : IExpressionConverter
    {
        private UserConnection _userConnection;
        
        public string Evaluate(object value, string arguments = "")
        {
            try
            {
                _userConnection = (UserConnection)HttpContext.Current.Session["UserConnection"];
                Guid locationId = new Guid(value.ToString());
                return getLocationProductsSum(locationId);
            }
            catch (Exception err)
            {
                return err.Message;
            }
        }
        private string getLocationProductsSum(Guid locationId)
        {
            try
            {
                EntitySchemaQuery esq = new EntitySchemaQuery(_userConnection.EntitySchemaManager, "OpportunityProductInterest");
                var AtsSumProduct = esq.AddColumn(esq.CreateAggregationFunction(AggregationTypeStrict.Sum, "AtsLineTotal"));
                var filter = esq.CreateFilterWithParameters(FilterComparisonType.Equal, "AtsLocation.Id", locationId);
                esq.Filters.Add(filter);
                EntityCollection entities = esq.GetEntityCollection(_userConnection);
                if (entities.Count > 0)
                {
                    return entities[0].GetTypedColumnValue(AtsSumProduct.Name).ToString();
                }
                return "0";
            }
            catch (Exception err)
            {
                throw err;
            }
        }
    }
}

File attachments

Like

3 comments

Dear Adebola,

Hi! It is impossible for listed C# code to return more than 1 result, at least because you explicitly specified ToString in getLocationProductsSum. You can ensure that only one row is returned using esq.RowCount = 1. Probably, the reason behind such an output is that your function Evaluate is called three times. You can debug your piece of code using VisualStudio: https://msdn.microsoft.com/en-us/library/sc65sadd.aspx/

Lisa

Hi Lisa, 

The code is suppose to group by locationId and sum the colums. Also, i didn't call the Evaluate function three times. Could it be because the location Id is in a table template. Please, could you explain how i can filter based on opportunity and location Id from the code i posted, maybe that will solve the problem. 

Thank you

Dear Adebola,

You can add additional filter using same syntax as with first one. If you want to create filter with multiple conditions using AND/OR, you can use EntitySchemaQueryFilterCollection - https://academy.bpmonline.com/api/SDKNETAPI/7.7.0/NetCoreAPI_Help.html#…

Best regards,

Lisa

Show all comments