You need to sign in to do that
Don't have an account?
levi6db
List has more than 1 row for assignment to SObject error, need help please.
Hi all,
I created a trigger that will update the Event owner to the owner of the Lead whenever the Lead owner is changed. We have a process where we assign a Lead to a queue with a scheduled appointment(Event) already assigned to an admin.
The trigger will only fire if the Lead owner does not equal a "Queue" owner.
I am getting an error when:
- There is more than one Event on the Lead
- If there are no Events on the Lead
trigger ownerupdate on Lead (after Update) { List<Event> aplist = new List<Event>(); for(Lead t : Trigger.new){ if ( (t.LeadSource == 'Telemarketing') && (t.Owner_Convert_Queue__c == NULL) ) {Event ap = [Select WhoId, Whatid, OwnerId from Event where WhoId = :t.Id]; ap.OwnerId = t.OwnerId; aplist.add( ap ); }} update aplist; }
Any help would be appreciated.
Thanks,
Alex
Hi,
If the query can return either 0 or more than one records you need to use an array.
{Event [] ap = [Select WhoId, Whatid, OwnerId from Event where WhoId = :t.Id};
if(ap.size() == 1){
//Business logic using ap[0]
}
Use the sample code given below to avoid the error :
List<Event> ev = [Select WhoId, Whatid, OwnerId from Event where WhoId = :t.Id];
if(ev.size() > 0)
{
//your logic here
}
Hope this helps.
I am not sure where I add those lines, sorry. Is it before the Trigger New or after?
Can you show me on my code?
Revised trigger shown below. I've highlighted the section that has changed.
Your original code was assuming that there would be exactly one event associated with each lead passed into the trigger, whereas there may be 0 or more. Thus you need to extract the list of events from the database, iterate them (if there are none, the loop will be skipped) and process each individually.
I'd also suggest that this trigger isn't bulk safe, as there is a SOQL query inside the for loop that traverses the trigger, but that's something for another day.
Thank you so much for your help! This worked perfectly. I appreciate everyones help with this one.
Alex
Of course, I should have thought ahead...I am having trouble with the Class now. I am getting the same error "ownerupdate.T1 System.QueryException: List has more than 1 row for assignment to SObject".
Not sure how to do this on a Class, since it was my first experience with this error on my Trigger. Here is what I have so far:
Help would be appreciated once again!
Thanks,
Alex
I can't see how the code you have posted could be causing that error - you are correctly storing the data in a list. Is this coming from a trigger that is assuming a soql call will always return a single value?
Here is what happens when Validate it. It works perfectly in the sandbox of course, but when I try and deploy to productions this is the error:
The trigger that you helped me with above is the one that this class belongs with. It can return any number of values.
Thanks,
Alex
//Declare a method that does not return anything and takes one input parameter of a Candidate list object called candsFromTrigger
public void createContact (List<Candidate__c> candsFromTrigger){
//Select the Recruiting record from the database and assign to an object called candAcct from the sObject Account class
Account candAcct = [SELECT a.Id FROM Account a WHERE a.Name = 'Recruiting'];
//Instantiate a Contact list object from the List class called conList
List<Contact> conList = new List<Contact>();
//Declare a For list loop to loop through the input parameter list candsFromTrigger with an iterationvariable called currentCandidate
for(Candidate__c currentCandidate:candsFromTrigger){
//Instantiate an object called con from the sObject class contact
Contact con = new Contact();
//Set the attribute AccountID of the con object to the value of the Id attribute of the candAcct object
con.AccountId = candAcct.Id;
//Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object
con.FirstName = currentCandidate.First_Name__c;
//Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object
con.LastName = currentCandidate.Last_Name__c;
//Set the attribute Email of the con object to the value of the Email__c attribute of the currentCandidate object
con.Email = currentCandidate.Email__c;
//Add the con object to the conList
conList.add(con);
}
//Persist the conList to the database
Database.insert(conList);
}
}
This is what I did in challege https://trailhead.salesforce.com/content/learn/projects/quick-start-apex-coding-for-admins/create-soql-to-return-data
but when fnally creating candidate in https://trailhead.salesforce.com/content/learn/projects/quick-start-apex-coding-for-admins/create-a-trigger
It is giving error
"CreateContact: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject Class.CreateContactFromCan.createContact: line 5, column 1 Trigger.CreateContact: line 5, column 1"
Please tell me