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
Ashish Kumar YadavAshish Kumar Yadav 

how to check while insert the record when checkbox is true and when checkbox is false in apex class ?

Requirement:-
1.My Requirement is while insert the record .check if Scheme_Applicable_at_PO_Creation__c  checkbox=true and Active__c=true and startDate and endDate between if any record is not assigned then only insert the record. 
2.second Requirement is while insert the record .check if Scheme_Applicable_at_PO_Creation__c  checkbox=false and Active__c=false and startDate and endDate between if any record is not assigned then only insert the record. 

Note:only two record is insert one is when checkbox is true and second one when checkbox is false.
User-added image
I have created query but not able to write complete logic.

//main logic
List <Scheme_Master__c> allschemeList = [SELECT Id, Name, Start_Date__c, End_Date__c, Scheme_Type__c, Scheme_Applicable_For__c, Active__c,Scheme_Applicable_at_PO_Creation__c FROM Scheme_Master__c where id =: sam.Scheme_Master__c AND Active__c=false AND Scheme_Applicable_at_PO_Creation__c=false AND createdDate >= : startDate AND createdDate <: endDate];

​​​​​​​
    List <Scheme_Master__c> allschemeList = [SELECT Id, Name, Start_Date__c, End_Date__c, Scheme_Type__c, Scheme_Applicable_For__c, Active__c,Scheme_Applicable_at_PO_Creation__c FROM Scheme_Master__c where id =: sam.Scheme_Master__c AND Active__c=true AND Scheme_Applicable_at_PO_Creation__c=true AND createdDate >= : startDate AND createdDate <: endDate];
    
      for (Scheme_Master__c sm : allschemeList) {
            
            //Iterate through each record within the allAccList using traditional for loop
            //This is needed so we can compare accList records to allAccList records
            for (Integer i = 0; i < allschemeList.size(); i++) {
                
                //If both records' name match and their IDs don't match then they are duplicates
                if ((sm.Name == allschemeList[i].Name) && (sm.id != allschemeList[i].id)) {
                
                //Checks the duplicate name checkbox within that record in allAccList    
                
                }
            }
      }


// this is my complete method where i am inserting the record it is working fine need to set above logic.
 public PageReference AssignIndividual(){
        try{
            if(sam.Scheme_Master__c == NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
                return null; 
            }
            else if(sam.Account__c== NULL && sam.Retailer_Account__c== NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Check Distributor/Retailer Name'));
            }
            else {
                List<Scheme_main_Product_Detail__c> smpdlist = [SELECT Id, Name, Scheme_Master__c, Product__c, Scheme_Slabs_Details__c, Product_SubCategory__c, Product_Category__c, Merchandise_Category__c, article_code__c FROM Scheme_Main_Product_Detail__c where Scheme_Master__c=:sam.Scheme_Master__c];
                System.debug('smpdlist@@@@'+smpdlist);
                dateInfo = new Scheme_Master__c();
                Set<Id> activeSchemeIds = new Set<Id>();
                if(dateInfo.Start_Date__c != null & dateInfo.End_Date__c != null){
                    Date startDate=dateInfo.Start_Date__c;
                    Date endDate=dateInfo.End_Date__c ;
                    List<Scheme_Master__c> smstlist=[SELECT Id, Name, Start_Date__c, End_Date__c, Scheme_Type__c, Scheme_Applicable_For__c, Active__c,Scheme_Applicable_at_PO_Creation__c FROM Scheme_Master__c where id =: sam.Scheme_Master__c AND Active__c=true AND Scheme_Applicable_at_PO_Creation__c=true AND Start_Date__c =:startDate AND End_Date__c=:endDate];
                    System.debug('smstlist@@@@'+smstlist);
                }
                
                
                System.debug('---> assign individual');
                List<Id> ret_accid=new List<Id>();
                List<Id> dist_accid=new List<Id>();
                if(sam.Retailer_Account__c != null){
                    ret_accid.add(sam.Retailer_Account__c);
                }
                if(sam.Account__c != null){
                    dist_accid.add(sam.Account__c);
                }
                
                List<Scheme_Assign_Master__c> membs = new List<Scheme_Assign_Master__c>();
                if(sam.Scheme_Applicable_For__c =='Distributor'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Account__c in : dist_accid and Account__c != null];
                    System.debug('membs---->'+membs.size());
                }
                else if(sam.Scheme_Applicable_For__c =='Retailer'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Retailer_Account__c in : ret_accid and Retailer_Account__c != null];
                    System.debug('membs---->'+membs.size());
                }
                else if(sam.Scheme_Applicable_For__c =='Both'){
                    membs =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Account__c in : dist_accid and Account__c != null];
                    list<Scheme_Assign_Master__c> membs1 = new list<Scheme_Assign_Master__c>();
                    membs1 =[SELECT Id,Account__c,Retailer_Account__c, Scheme_Master__c,Scheme_Applicable_For__c from Scheme_Assign_Master__c where Scheme_Master__c =: sam.Scheme_Master__c and Retailer_Account__c in : ret_accid and Retailer_Account__c != null];
                    if(membs1.size() > 0){
                        membs.addAll(membs1);
                        System.debug('membs1---->'+membs1.size());
                    }
                    System.debug('membs---->'+membs.size());
                }
                
                Set<Id> accIds = new Set<Id>();
                
                for(Scheme_Assign_Master__c obj : membs){
                    System.debug('-----------SAM ID ->'+obj.Id);
                    if(obj.Retailer_Account__c != null){
                        accIds.add(obj.Retailer_Account__c);
                    }
                    if(obj.Account__c != null){
                        accIds.add(obj.Account__c);
                    }
                }
                
                lstAssignIndividual = new List<Scheme_Assign_Master__c>();
                scheme_masterobj=new Scheme_Master__c();
                
                System.debug('Retailer_Account__c--->'+sam.Retailer_Account__c);
                System.debug('Scheme_Master__c--->'+sam.Scheme_Master__c);
                System.debug('Account__c--->'+sam.Account__c);
                
                if(sam.Scheme_Master__c != null){
                    if(sam.Retailer_Account__c != null){
                        if(!accIds.contains(sam.Retailer_Account__c)){
                            Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                            objSAM.Retailer_Account__c = sam.Retailer_Account__c;
                            objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                            lstAssignIndividual.add(objSAM);
                            accIds.add(sam.Retailer_Account__c);
                        }
                        else{
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Scheme master already assigned to this Retailer Account'));
                        }
                    }
                    
                    if(sam.Account__c != null){
                        if(!accIds.contains(sam.Account__c)){
                            Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                            objSAM.Account__c = sam.Account__c;
                            objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                            lstAssignIndividual.add(objSAM);
                            accIds.add(sam.Account__c);
                        }
                        else{
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Scheme master already assigned to this Distributer Account'));
                        }
                    }
                }
                
                
                if(lstAssignIndividual.size() > 0){
                    insert lstAssignIndividual;
                    
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,' Assign Individual Scheme to Distributor / Retailer Successfully.'));
                    
                    List<User> userList = new List<User>();
                    userList = [SELECT Id,Email,IsActive FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = True ];
                    for(User u : userList){
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[] {u.Email};
                            mail.setToAddresses(toAddresses);
                        mail.setSubject('Scheme Assign to Distributor/Retailer');
                        mail.setPlainTextBody('Scheme Assigned to Distributor/Retailer successfully');
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
                }
            }
        }
        catch(Exception e){
            System.debug('Exception at '+e.getLineNumber() +','+ e.getMessage());
        }
        return null;
    }