0

I have a trigger where I'm calling a method from handler class and I'm passing the trigger.New to that method like this:

if(Trigger.isBefore && Trigger.isInsert){
        List<Lead> lstLead = Trigger.New;
        LeadHandler.assignLeadsToQueue(lstLead);        
    }

But Now I have a requirement where I want to pass another dynamic string value to that method in the apex class like this:

public static void assignLeadsToQueue(List<Lead> lstLead, String queueName){
List<Group> lstQueue = [SELECT Id FROM Group WHERE type='Queue' AND Name LIKE : queueName];

List<GroupMember> grpList = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId IN : lstQueue];
     
List<AggregateResult> userIdWithLeadCountList = [SELECT OwnerId, Status, COUNT(Id) leadCount FROM Lead WHERE ((OwnerId IN (SELECT UserOrGroupId FROM GroupMember WHERE GroupId IN : lstQueue)) AND (Status = 'Open - Not Contacted' OR Status = 'Working - Contacted')) GROUP BY OwnerId, Status ORDER BY COUNT(Id)];

   **Another Logic with List<Lead> lstLead, List<GroupMember> grpList and List<AggregateResult> userIdWithLeadCountList**
}

The String queueName is coming from the LWC.

Now, I'm facing issue in passing the value because the trigger shown the error if I do not add String parameter to the method. (We cannot pass the parameter directly to trigger via LWC)

How can I do that

5
  • If the trigger and the LWC are doing different things, you should have separate methods. If there's some common code, put that into a third, shared method called from the trigger and LWC specific methods.
    – Phil W
    Jan 31 at 10:52
  • Hi Phil W, I have made the changes can you pls check again Jan 31 at 10:55
  • you can also use method overloading. You could define two method with same name but with different parameter Jan 31 at 10:56
  • Hi Parag, the scenario is, I have apex class method where I'm passing trigger.New and in the same method I need to pass dynamic value through LWC Jan 31 at 11:00
  • I stand by more comment; using the same method to do two different things is bad practice. Keep methods having focused roles and responsibilities. Split the common code into a separate method and call this from the two places its needed.
    – Phil W
    Jan 31 at 11:03

1 Answer 1

0

You would just add the queue name to the trigger:

LeadHandler.assignLeadsToQueue(lstLead, 'Queue to assign to');

You can use literal parameters to pass in to a method.

2
  • Hi, the queue name is not static, it can change from the picklist value selected in LWC component where different queue names are present Feb 1 at 4:37
  • @AdityaDeshmukh I guess your question is unclear, then. Why do you have this trigger at all? The purpose of a trigger is to do some logic in all contexts, but it sounds like you only want to do the assignment from the LWC? If that's the case, just write an AuraEnabled method to call so you can do the assignment there.
    – sfdcfox
    Feb 1 at 13:05

You must log in to answer this question.

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