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
kd T 2kd T 2 

Hi everyone, I have urgent requirement regarding Trigger. Please help me on this.

There is one object called Compliance__c, on Compliance__c there is one field called ‘order__c' that contains the case 7 digit sales order no. We’ve to pull Account from Compliance__c by matching sales order 7 digit with order__c on case Account field(Account Id).
It should run only for QA record type.
Ajay K DubediAjay K Dubedi
Hi,
Try the following trigger and its handler it may be helpful for you:
Trigger:
trigger ComplianceTrigger on Compliance__c (before insert) {
   
    if(trigger.isInsert && trigger.isBefore){
        ComplianceHandler.findAccount(trigger.new);
    }
}

Trigger-Handler:
public class ComplianceHandler {
    public static void findAccount(List<Compliance__c> complList){
        if(complList.size()>0){
            try{
                Set<Decimal> orderNoSet=new Set<Decimal>();
                List<Account> accList=new List<Account>();
                for(Compliance__c comp:complList){
                    if(comp.order__c!=null){
                        orderNoSet.add(comp.order__c);
                    }
                }
                if(orderNoSet.size()>0){
                    accList=[SELECT Id,Name,order__c FROM Account WHERE order__c IN: orderNoSet LIMIT 10000];
                }
                if(accList.size()>0){
                    SYSTEM.debug('Account-List->'+accList);
                }
                else{
                    system.debug('No such Account found.');
                }
            }
            catch(Exception ex){
                system.debug('LineNo->'+ex.getLineNumber()+'msg->'+ex.getMessage());
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
kd T 2kd T 2
 Hi Ajay,
I have a doubt on above code
This order__c is not available on Account. So why you've taken order__c  on Account.
Ajay K DubediAjay K Dubedi
Hi,
Try the following code it may be helpful for you:
Trigger-Handler:
public class ComplianceHandler {
    public static void findAccount(List<Compliance__c> complList){
        if(complList.size()>0){
            try{
                Set<Decimal> orderNoSet=new Set<Decimal>();
                List<Account> accList=new List<Account>();
                Set<Id> acIdSet=new Set<Id>();
                List<Case> caseList=new List<Case>();
                for(Compliance__c comp:complList){
                    if(comp.order__c!=null){
                        orderNoSet.add(comp.order__c);
                    }
                }
                if(orderNoSet.size()>0){
                    caseList=[SELECT Id,order__c,AccountId FROM Case WHERE order__c IN: orderNoSet LIMIT 10000];
                }
                if(caseList.size()>0){
                    for(Case cse:caseList){
                        if(cse.AccountId!=null){
                            acIdSet.add(cse.AccountId);
                        }
                    }
                }
                if(acIdSet.size()>0){
                    accList=[SELECT Id,Name FROM Account WHERE Id IN: acIdSet LIMIT 10000];
                }
                if(accList.size()>0){
                    system.debug('Account-List->'+accList);
                }
            }
            catch(Exception ex){
                system.debug('LineNo->'+ex.getLineNumber()+'msg->'+ex.getMessage());
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
kd T 2kd T 2
No It doesn't work and I think trigger should be on case instead of compliance