+ Start a Discussion
kavya mareedukavya mareedu 

I would recommend you guys to help with triggers concept.

My question is: When ever lead is inserted, with leadsource as web set wilson as owner of record. If the leadsource is other than web then assign it to lifeQueue as owner.

Ans:

public class Lead_Example {
    
    User u= [Select id from User where FirstName LIKE 'wilson'];
    group g= [Select id from Group where type='Queue' and name='LifeQueue'];
    public static void callMe(List<Lead> leads) 
    {
        for(Lead l: leads)
        {
            if(l.LeadSource=='web')
            {
                l.OwnerId= u.id;
            }else{
                l.OwnerId=g.id;
            }
        }
    }
}

Trigger Callout:
trigger Lead_Example on Lead (before insert) {
    List<Lead> leads=Trigger.new;
    
    Lead_Example.callMe(Trigger.new);
    
}
kavya mareedukavya mareedu
The error is it says variable u does not exist.
 
Sagar PatilSagar Patil
Hi Kavya,

Kindly add soql for User & Group inside callMe method in order to resolve this issue as below.
 
public class Lead_Example {
    
   
    public static void callMe(List<Lead> leads) 
    {
         User u= [Select id from User where FirstName LIKE 'wilson'];
         Group g= [Select id from Group where type='Queue' and name='LifeQueue'];

        for(Lead l: leads)
        {
            if(l.LeadSource=='web')
            {
                l.OwnerId= u.id;
            }else{
                l.OwnerId=g.id;
            }
        }
    }
}

Mark this answer as best answer if it helps you.

Regards,
Sagar
kavya mareedukavya mareedu
Review all error messages below to correct your data.
Apex trigger LeadOwnerSetup caused an unexpected exception, contact your administrator: LeadOwnerSetup: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.LeadOwnerSetup: line 19, column 1   ----- THIS IS THE ERROR MESSAGE
Maharajan CMaharajan C
Hi Kavya,

Post your LeadOwnerSetup trigger code here.

Or try to change the event from After Insert to Before Insert.

In the After Insert the Trigger.New values will  be in Read only. You can't edit the value if you want edit the value then fetch the record from database in after insert then modify those records.

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C