0
public class PartialMatchClassPurchased {
    public static List<Order_Line_Item__c> Partialmatch(List<Id> ids) {
        List<Commission__c> comm = new List<Commission__c>();
        List<Order_Line_Item__c> Purchaselist =new List<Order_Line_Item__c>();
        List<Order_Line_Item__c> idfs = new List<Order_Line_Item__c>();
        idfs=[select Customer__r.id from Order_Line_Item__c where id=:ids];
        Commission_Validity_days__c Setting = Commission_Validity_days__c.getValues('Days');
        Integer days = Integer.valueOf(Setting.Validity_Period_days__c);
        List<string> Value= new List<string>();
        for(Order_Line_Item__c o : idfs ){
            Value.add(o.Customer__r.id);
        }
        for(Order_Line_Item__c oc:[SELECT Id,Refund__c,Status__c,New_Category__c,createddate,New_Group__c,New_Department__c,Product__c,Commission_Set__c,Who_Recommended__c,New_Division__c,Web_Code__c,TEMP_Type__c,Name,New_Product_Brand__c,New_Product_Colour__c,New_Product_Size__c,New_Product_Title__c,Order_Date__c,Order_Number__c,Product_Code__c,recordtypeid FROM Order_Line_Item__c where recordtypeid= 'XXXXXXXXXXXXXX' and Id =:ids])
        {
            Purchaselist.add(oc);
        }

        List<Order_Line_Item__c> RecommendationList =new List<Order_Line_Item__c>();
        for(Order_Line_Item__c od:[SELECT Id,Refund__c,Status__c,New_Category__c,createddate,New_Group__c,New_Department__c,Commission_Set__c,Who_Recommended__c,Product__c,Web_Code__c,New_Division__c,TEMP_Type__c,Name,New_Product_Brand__c,New_Product_Colour__c,New_Product_Size__c,New_Product_Title__c,Order_Date__c,Order_Number__c,Product_Code__c,recordtypeid FROM Order_Line_Item__c where recordtypeid= 'XXXXXXXXXXXXXX' and customer__r.id IN : Value ])
        {
            RecommendationList.add(od);  
        }
        try
        {
            for(integer i=0;i<Purchaselist.size();i++)
            {
                for(integer j=0;j<RecommendationList.size();j++)
                {
                    if(RecommendationList.size()>0)
                    {
                        Datetime PurchaseDate = Purchaselist[i].createddate;
                        Datetime Recommendate = RecommendationList[j].createddate;
                        Date Purchase = PurchaseDate.dateGmt();
                        Date Recommend = Recommendate.dateGmt();
                        Integer numberDaysDue = Recommend.daysBetween(Purchase);
                        //Integer numberDaysDue = Purchaselist[i].createddate.daysBetween(RecommendationList[j].createddate);

                        if(Purchaselist[i].Product__c == RecommendationList[j].Product__c || Purchaselist[i].New_Product_Brand__c == RecommendationList[j].New_Product_Brand__c
                           &&  Purchaselist[i].New_Product_Title__c == RecommendationList[j].New_Product_Title__c
                           &&  Purchaselist[i].New_Category__c == RecommendationList[j].New_Category__c
                           && Purchaselist[i].New_Division__c == RecommendationList[j].New_Division__c
                           && Purchaselist[i].New_Department__c == RecommendationList[j].New_Department__c
                           && Purchaselist[i].New_Group__c == RecommendationList[j].New_Group__c 
                           && numberDaysDue<=days)
                        {

                            Commission__c c1 = new Commission__c();

                            c1.Related_Product_Interaction__c=RecommendationList[j].id;
                            c1.Status__c = Purchaselist[i].Status__c;
                            c1.Purchased_Interaction__c = Purchaselist[i].id;
                            c1.Name__c ='Recommendation Purchased';
                            if(Purchaselist[i].Product__c == RecommendationList[j].Product__c)
                            {
                                c1.Match_Type__c ='Exact';
                            }
                            else
                            {
                                c1.Match_Type__c ='Partial';
                            }
                            comm.add(c1); 
                            for(Commission__c cs:[select ID,Related_Product_Interaction__c from Commission__c where
                                                  Related_Product_Interaction__c=: RecommendationList[j].id])
                            {
                                if(cs.Related_Product_Interaction__c == RecommendationList[j].id)
                                {
                                    comm.clear();
                                }
                                else {

                                    Commission__c c = new Commission__c();
                                    c.Related_Product_Interaction__c=RecommendationList[j].id;
                                    comm.add(c);  
                                } } } } }} }
        catch(exception e)
        {
            system.debug('Cause'+e.getCause());
            system.debug('e.getLineNumber()'+e.getLineNumber());
            system.debug('e.getMessage()'+e.getMessage());
            system.debug('e.getStackTraceString()'+e.getStackTraceString());
            system.debug('e.getTypeName()'+e.getTypeName());  
        }
        insert comm;
        return RecommendationList;
    }

}

It is creating commission records by comparing Purchased and Recommendation list,the problem i'm facing is it is creating the records even though the criteria is not met (which are not matching at all) like Purchased record a is not equal to Recommendation a -->creates the record **that shouldn't be happening with code and also I've created a custom setting for date the problem is it is updating records with old dates(Last years) as well but it should only be updating records which are created within 30 days recommendation created for this month Like Recommendation Product:Apple,Created Date=12/05/2018 should be valid for 30 days after that it shouldnt be creating any records.but with this code it is doing that.I'm using trigger to update the record.BeforeInsert,afterinsert,afterupdate.

Kindly help me!

1 Answer 1

1

I suggest you use a unit test case to do this development. In that you can use a small number of records that contain known data as the unit test will have inserted the data. That then makes using debug logs and being certain of what the code is doing easier.

My first thought in the code you have posted is that the two queries are always returning matching values. But the problem may be elsewhere.

Reducing the amount of unnecessary code might make it easier to identify the problem. For example:

        for(integer i=0;i<Purchaselist.size();i++)
        {
            for(integer j=0;j<RecommendationList.size();j++)
            {
                if(RecommendationList.size()>0)
                {
                    Datetime PurchaseDate = Purchaselist[i].createddate;
                    Datetime Recommendate = RecommendationList[j].createddate;

                    ..

                    if(Purchaselist[i].Product__c == RecommendationList[j].Product__c || Purchaselist[i].New_Product_Brand__c == RecommendationList[j].New_Product_Brand__c
                       &&  Purchaselist[i].New_Product_Title__c == RecommendationList[j].New_Product_Title__c
                       &&  Purchaselist[i].New_Category__c == RecommendationList[j].New_Category__c
                       && Purchaselist[i].New_Division__c == RecommendationList[j].New_Division__c
                       && Purchaselist[i].New_Department__c == RecommendationList[j].New_Department__c
                       && Purchaselist[i].New_Group__c == RecommendationList[j].New_Group__c 
                       && numberDaysDue<=days)
                    {

can be replaced by (and added braces to be sure about the || vs && precedence):

        for (Order_Line_Item__c  p : Purchaselist)
        {
            for(Order_Line_Item__c r : RecommendationList)
            {
                    Datetime PurchaseDate = p.createddate;
                    Datetime Recommendate = r.createddate;

                    ...

                    if((p.Product__c == r.Product__c || p.New_Product_Brand__c == r.New_Product_Brand__c)
                       && p.New_Product_Title__c == r.New_Product_Title__c
                       && p.New_Category__c == r.New_Category__c
                       && p.New_Division__c == r.New_Division__c
                       && p.New_Department__c == r.New_Department__c
                       && p.New_Group__c == r.New_Group__c 
                       && numberDaysDue<=days)
                    {

When you do get this working, you also need to consider whether the query of Commission__c in the middle of the loop will cause the the 100 queries governor limit to be exceeded ever and if so move that out of the loop.

Also note that doubly nested loops can run you into Apex CPU time limit exceeded errors sooner than you might expect. Techniques using maps can avoid that.

You can simplify this pattern:

    List<Order_Line_Item__c> Purchaselist =new List<Order_Line_Item__c>();

    ...

    for(Order_Line_Item__c oc:[SELECT ...])
    {
        Purchaselist.add(oc);
    }

to just this:

    List<Order_Line_Item__c> Purchaselist = [SELECT ...];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .