this ans is for Account a =[SELECT Name FROM Account WHERE industry='Energy']; ---->System.QueryException: List has more than 1 row for assignment to SObject --->reason--> storing list of records into single variable.
// Collect all Account IDs from the trigger Set accountIds = new Set(); for (Account acc : Trigger.new) { accountIds.add(acc.Id); } // Query all Contacts related to the Accounts in the trigger List conList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds]; // Process each Contact for (Contact con : conList) { // Your logic here }
1. DML Exception - Required Field Missing (LastName) 2. QueryException - No records returned or trying to assign more than 1 record to a singleton sObject Account variable.
In 1st scenario Where you iterate over accounts Mistake is that Dml statement used inside for loop So to overcome this use separate list and insert list outside for loop
Hi Ankit, You are doing a great job for the community, my request to you is that I can get the list of questions and answers you are preparing in the form of a PDF
Curious ..have you done the video on Apex Replay Debugger ? Also for last question --> will you get LIST HAS NO ROWS FOR ASSIGNMENT TO SObject error ??? And we can fix it by converting that account instance to List
Set accSet = new Set(); for (Account acc : trigger.new){ accSet.add(acc.Id); // do some processing there } List conList = [SELECT Id,FirstName,LastName FROM Contact WHERE AccountId IN : accSet]; for (Contact con : conList){ // do some processing there }
@@TechJourneyWithAnkit System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]
set accId=new set(); for(Account a:trigger.new){ accId.add(a.Id); } list conlist=[select Id,AccountId from contact where AccountId in:accId]; for(contact con:conlist){
for(Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :Trigger.New]{ for(Contacts con : a.Contacts){ //Some contact processing } }
It may throw the query exception. More than one record to a singleton sObject variable.
Hi Ankit, 🙌 Kudos to you for all the good work you have been doing! . Could you please create a separate video for "Apex replay debugger" ?
Sure will plan for it
Thank you sir 🙏
tqq Ankit sharing informatic videos
Welcome
this ans is for Account a =[SELECT Name FROM Account WHERE industry='Energy']; ---->System.QueryException: List has more than 1 row for assignment to SObject --->reason--> storing list of records into single variable.
Very good explanation
Nice video 👍 waiting for the videos on triggers batch apex and other concepts 😊
Nice
Ankit, great content. Pls do create a video on the apex replay debugger. It would be really helpful.
It's already on the channel
// Collect all Account IDs from the trigger
Set accountIds = new Set();
for (Account acc : Trigger.new) {
accountIds.add(acc.Id);
}
// Query all Contacts related to the Accounts in the trigger
List conList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds];
// Process each Contact
for (Contact con : conList) {
// Your logic here
}
Very good
1. DML Exception - Required Field Missing (LastName)
2. QueryException - No records returned or trying to assign more than 1 record to a singleton sObject Account variable.
Correct
Thank you sir, please create a video on Apex Replay Debugger
It’s already on the channel. Do check out
QueryList Exception : List has no rows exception, store it in a list
Yes correct
DMl Exception: Required Field(Last Name) is mIssing
Yes correct
In 1st scenario
Where you iterate over accounts
Mistake is that
Dml statement used inside for loop
So to overcome this use separate list and insert list outside for loop
Very good
Hi Ankit, You are doing a great job for the community, my request to you is that I can get the list of questions and answers you are preparing in the form of a PDF
Thanks
Curious ..have you done the video on Apex Replay Debugger ?
Also for last question --> will you get LIST HAS NO ROWS FOR ASSIGNMENT TO SObject error ??? And we can fix it by converting that account instance to List
Not yet, will plan for it.
hi Ankith , can you please mention the link apex debugger it would bre useful
How to debug Apex Code in salesforce #salesforce #apex
th-cam.com/video/lj4eUDt_-sE/w-d-xo.html
Is it possible to call Batch class from a future call?
No
Set accSet = new Set();
for (Account acc : trigger.new){
accSet.add(acc.Id);
// do some processing there
}
List conList = [SELECT Id,FirstName,LastName FROM Contact WHERE AccountId IN : accSet];
for (Contact con : conList){
// do some processing there
}
Any other better approach Ravi to avoid two loops
required field is missing in last question , cont.LastName
Please tell me the name of exception
@@TechJourneyWithAnkit
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]
How to get job as freshers in Salesforce developer
Get admin skills strong and have sound knowledge of Apex and LWC. With overview of Integration
Please create a vedio on apex reply debug.
Sure I will plan for it
@@TechJourneyWithAnkit Even I am expecting a video on Apex replay debugger. So, Kindly make a video on that as well. Thanks in Advance
set accId=new set();
for(Account a:trigger.new){
accId.add(a.Id);
}
list conlist=[select Id,AccountId from contact where AccountId in:accId];
for(contact con:conlist){
}
for(Account a : [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :Trigger.New]{
for(Contacts con : a.Contacts){
//Some contact processing
}
}
Correct
@@TechJourneyWithAnkit Nested for loop is not a part of best practices