function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Find previous child records and update checkbox on latest child record

Hi Team,

My requirement is like:

Suppose there are multilpe quotes for an Account. Now I want to have a invoice for a particular quote and we want to apply special discount for that quote. The discount will be applicable based on a date field and percentage field. So based on the date field, I need to check the previous quotes i.e if the date is 17th March 2021 for the current quote, then i need to check the if the quotes of Janaury or December or November for that particular account had percent field as 20%. 

So whenever there is invoicing for a particular quote, I need to exclude the recent previous quote and check back the other previous 3 quotes and make the checkbox checked for the quote which we are invoicing. 

I tried to write a trigger but it is throwing error: 'Logical operator can only be applied to Boolean'

TRIGGER:

trigger Eligibleforspenddisc on SBQQ__Quote__c (before insert,before update) {
    for(SBQQ__Quote__c sb : Trigger.new){
        if(sb.SBQQ__Account__c != null){
     List<SBQQ__Quote__c>  sbList = [Select id, SBQQ__Account__c, Activity_Date_PB__c, PA_Coverage__c from SBQQ__Quote__c
                                where SBQQ__Account__c =:sb.SBQQ__Account__c and PA_Coverage__c=20];
            if(((sb.Activity_Date_PB__c.addMonths(-2)) || (sb.Activity_Date_PB__c.addMonths(-3)) || (sb.Activity_Date_PB__c.addMonths(-4))) && 
               sb.PA_Coverage__c == 19.5){
                   sb.Eligible_for_Spend_Discount__c = true;
               }
        }
    }

}

Please help in solving this issue.

Thank You,
Mahesh
 
Best Answer chosen by Mahesh Babu 187
Mahesh Babu 187Mahesh Babu 187
I have fixed the error .
The updated code is below:

trigger Eligibleforspenddisc on SBQQ__Quote__c (before insert,before update) {
    for(SBQQ__Quote__c sb : Trigger.new){       
        if(sb.SBQQ__Account__c != null && sb.Invoice_Date_Formula__c!=null){            
            List<SBQQ__Quote__c>  sbList = [Select id, SBQQ__Account__c, Invoice_Date__c,Invoice_Date_PB__c,Invoice_Date_Formula__c, PA_Coverage__c from SBQQ__Quote__c
                                            where SBQQ__Account__c =:sb.SBQQ__Account__c and 
                                            (Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-2) or
                                             Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-3) or
                                             Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-4)) and  PA_Coverage__c >=:19.50];
            
            
            if(sbList.size()>0){
                sb.Eligible_for_Spend_Discount__c = true;          
            }
            else{
                sb.Eligible_for_Spend_Discount__c = false;
            } 
        }        
    }    

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Mahesh,

Greetings!

This error occurs whenever you use the operator incorrectly.I would suggest you to try by using && instead of || for the if condition.

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Mahesh Babu 187Mahesh Babu 187
Hi Shirisha,

I am still getting the same error.

Thanks, 
Mahesh
AnudeepAnudeep (Salesforce Developers) 
Hi Mahesh, 

I believe the error is occurring at the following line 

                   sb.Eligible_for_Spend_Discount__c = true;

Can you confirm if Eligible_for_Spend_Discount__c is a boolean field or can you try doing a SOQL and retrieve this field from SBQQ__Quote__c 

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Mahesh Babu 187Mahesh Babu 187
I have fixed the error .
The updated code is below:

trigger Eligibleforspenddisc on SBQQ__Quote__c (before insert,before update) {
    for(SBQQ__Quote__c sb : Trigger.new){       
        if(sb.SBQQ__Account__c != null && sb.Invoice_Date_Formula__c!=null){            
            List<SBQQ__Quote__c>  sbList = [Select id, SBQQ__Account__c, Invoice_Date__c,Invoice_Date_PB__c,Invoice_Date_Formula__c, PA_Coverage__c from SBQQ__Quote__c
                                            where SBQQ__Account__c =:sb.SBQQ__Account__c and 
                                            (Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-2) or
                                             Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-3) or
                                             Activity_Date_PB__c=:sb.Activity_Date_PB__c.addMonths(-4)) and  PA_Coverage__c >=:19.50];
            
            
            if(sbList.size()>0){
                sb.Eligible_for_Spend_Discount__c = true;          
            }
            else{
                sb.Eligible_for_Spend_Discount__c = false;
            } 
        }        
    }    
This was selected as the best answer