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
sandeepathadusandeepathadu 

hi all help for 101 soql error

trigger updatetotalapps on Appointment__c(after update)
{
public list<appointment__c> apps {get;set;}  
public list<appointment__c> apps5 {get;set;} 
apps5 = new list<appointment__c>();
apps = new list<appointment__c>();
for(Appointment__c o:Trigger.old )
{   

if (Trigger.isUpdate)
{
appointment__c  app1 =[select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where id=:o.id];
appointment__c[]  app =[select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where Appointment__c=:app1.id];
for(appointment__c ap:app)
{
ap.branch__c=app1.branch__c;
ap.Doctor__c=app1.Doctor__c;
ap.Appt_Date__c=app1.Appt_Date__c; 
apps.add(ap);
}
} 
}update apps;
}

 this is the triiger i have written for updating of records but after certain records updaTED ITS THROWING 101 SOQL ERROR PLZ REWRITE MY CODE SO THAT I CAN AVOID THE ERROR

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

try this trigger

 

trigger updatetotalapps on Appointment__c(after update)
{
public list<appointment__c> apps {get;set;}  
public list<appointment__c> apps5 {get;set;} 
apps5 = new list<appointment__c>();
apps = new list<appointment__c>();

Map<ID , appointment__c> mapAppiontment = new MAP<ID , appointment__c>([select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where 

id in: trigger.old]);

List<appointment__c> listApp = new List<appointment__c>();

MAP<ID , List<appointment__c>> mapAPPID_List = new MAP<ID , List<appointment__c>>();
for(appointment__c appObj : [select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where Appointment__c in: 

mapAppiontment.keyset()])
{
  if(mapAPPID_List.containskey(appObj.Appointment__c))
     {
         listApp = mapAPPID_List.get(appObj.Appointment__c);
     }
   else
     {
        listApp = new List<appointment__c>();
     }
  
  listApp.add(appObj);
  mapAPPID_List.put(appObj.Appointment__c , listApp);

}

for(Appointment__c o:Trigger.old )
{   

if (Trigger.isUpdate)
{
appointment__c  app1 = mapAppiontment.get(o.id);
List<appointment__c>  app  = new List<appointment__c>();

if(mapAPPID_List.containsKey(app1.id))
   app = mapAPPID_List.get(app1.id);

for(appointment__c ap:app)
{
ap.branch__c=app1.branch__c;
ap.Doctor__c=app1.Doctor__c;
ap.Appt_Date__c=app1.Appt_Date__c; 
apps.add(ap);
}
} 
}
update apps;
}

 @kirtesh ,  nice to see you my friend :)

All Answers

Kirtesh_JainKirtesh_Jain

Hi ,

 

Please Use SOQL out side of loop. Make use of List and Map to get Ids in collection form , then fire Query to get resultant data. Then Again Use List or Map to get appropriate Data.

 

Thanks,

Kirtesh

sandeepathadusandeepathadu

thanks for reply could u give me sample code how to strings and maps

Shashikant SharmaShashikant Sharma

try this trigger

 

trigger updatetotalapps on Appointment__c(after update)
{
public list<appointment__c> apps {get;set;}  
public list<appointment__c> apps5 {get;set;} 
apps5 = new list<appointment__c>();
apps = new list<appointment__c>();

Map<ID , appointment__c> mapAppiontment = new MAP<ID , appointment__c>([select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where 

id in: trigger.old]);

List<appointment__c> listApp = new List<appointment__c>();

MAP<ID , List<appointment__c>> mapAPPID_List = new MAP<ID , List<appointment__c>>();
for(appointment__c appObj : [select ID,Account__c,Name,Branch__c,Doctor__c,Appt_Date__c from appointment__c where Appointment__c in: 

mapAppiontment.keyset()])
{
  if(mapAPPID_List.containskey(appObj.Appointment__c))
     {
         listApp = mapAPPID_List.get(appObj.Appointment__c);
     }
   else
     {
        listApp = new List<appointment__c>();
     }
  
  listApp.add(appObj);
  mapAPPID_List.put(appObj.Appointment__c , listApp);

}

for(Appointment__c o:Trigger.old )
{   

if (Trigger.isUpdate)
{
appointment__c  app1 = mapAppiontment.get(o.id);
List<appointment__c>  app  = new List<appointment__c>();

if(mapAPPID_List.containsKey(app1.id))
   app = mapAPPID_List.get(app1.id);

for(appointment__c ap:app)
{
ap.branch__c=app1.branch__c;
ap.Doctor__c=app1.Doctor__c;
ap.Appt_Date__c=app1.Appt_Date__c; 
apps.add(ap);
}
} 
}
update apps;
}

 @kirtesh ,  nice to see you my friend :)

This was selected as the best answer
sandeepathadusandeepathadu

thank u very much Shashikant Sharma i really wonder how exactly u r able to understand the problem and give right on answers it worked perfectly .thanks once again and hats off to u r understanding

Shashikant SharmaShashikant Sharma

Your welcome sandeepathadu,

 

I have written a lot about triggers on my blog from basic to advance cases , you can see that.

 

http://forceschool.blogspot.com/search/label/Apex%20Triggers

sandeepathadusandeepathadu

hi 

can u plz give me the test case for trigger i am getting problems  in it pls asap.