• shaan85
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies

Hi,

I have an apex class which is being called in the Apex trigger. When the trigger is being fired an error is being thrown :

Too many Script Statements 200001.

How to avoid this? I have followed the best practices to avoid this even then i am not able to avoid this.

 

Here is my code:

 

trigger TestTrigger1 on Obj__c(after insert, after update)

{

set<Id> cntctIds = new set<Id>();

List<obj__c> list2 = new List<obj__c>();

List<obj__c> list3 = new List<obj__c>();

List<obj__c> list1=[select id,Contact__c, field1, field2,.... from obj__c where id In:trigger.newMap.keyset()];

for(Integer j=0;j< list1.size();j++)
    {
        cntctIds.add(list1[j].Contact__c);
    }

objCntct = [select field1,field2 from obj__c where Contact__c IN:cntctIds];

list2 = [select field1,field2 from obj__c where Contact__c IN:cntctIds and field1 = '123'];

list3 = [select field1,field2 from obj__c where Contact__c IN:cntctIds and field1 = ''456];

for(Integer k=0;k< objCntct.size();k++)

{

for(Integr l=0;l<list2.size();l++)

{

//code goes here...

}

 

}

}

 

 

i am doing anything wrong here?

I feel the only reason could be that i am using nested for loops so there might be infinite loops.

Or how to make use of maps to use in loops?

How to avoid this does any one have soln?

 

Thanks

Shaan

 

Hi,

I am retrieving id of a user using user method.And using the same in retrieving the details.

But a gov limit exception is thrown in soql query.

 

Here is my code....

 

 how can we avoid governor limit for this....

I have tried applying Limit and also making use of set ...

 

 

trigger Test on Lead (before insert,before update)
{
    Lead [] lead_new = Trigger.new;
    Lead [] lead_old = Trigger.old;    
    Set<Id> newLead = new Set<Id>();
    public id id1;
    public id id2;
    private User varuser;
    
    id1 = userinfo.getProfileId();
    id2 = userinfo.getUserId();
  
   varuser =[Select IsActive , Name from User where Id= :id2];
   System.debug('UserName ' + varuser.name);

    for (Integer k = 0; k <lead_new.size(); k++)
    {
      // Updation Takes place..
    }
   
           
}
  • September 14, 2010
  • Like
  • 0

I have an issue of GLimit when using the list and set.

I have written a trigger on a custom object which is in relation with contacts.

The trigger performs action based on updation or insertion of records.

Here is my code:

 

Set<String> cntctIds = new Set<String>();

List<obj__c> obj =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where id In:trigger.newMap.keyset()];

for(Integer j=0;j< obj.size();j++)

{
        cntctIds.add(obj[j].Contact__c);
}

 

List<obj__c> obj_Total =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where id IN: cntctIds];

 

//List used to retrieve contacts based on cntctIds

List<Contact> objCntct = [Select id,Account.Name,lookupOwnerID__c, SSN__c, PCN__c, Status__c, FirstName, LastName,AccountId, OwnerId,  from Contact where id IN: cntctIds];        

 

//List used to retrieve Obj__c based on cntctIds and Terminated Status.

List<obj__c> obj_Terminated =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where obj__c.Status__c = 'Terminated' and id  IN: cnctctIds];

 

//List used to retrieve Obj__c based on cntctIds and Current Status.

List<obj__c> obj_Current =[select id, SSN__c, Name, Status__c, Contact__c,ownerid, Account_Name__c, from obj__c where obj__c.Status__c = 'Current' and id  IN: cnctctIds];

 

now i need to check the conditions if all the records are terminated or current.

For that i am using the condition:

for(j=0; j<objCntct ; j++)

{

if(obj_Total.size() == obj_Terminated.size())

{

//functionality

}

else if(obj_Total.size() == obj_Current.size())

{

 

}

 

}

 

This is working perfectly fine if the batch size is 1.

Becuase in that scenario there will be only one contact but if the batch size is 200 then there will be many contacts for 200 obj__c records.And then the condition fails bacause the the obj_Total count and the terminated or current count never matches.

 

Can anyone provide a soln for this.

Or any one has the idea how to make use of Maps here to check the conditions.

 

 

 

Thanks

shaan

 

 

 

Hi,

I have an apex class which is being called in the Apex trigger. When the trigger is being fired an error is being thrown :

Too many Script Statements 200001.

How to avoid this? I have followed the best practices to avoid this even then i am not able to avoid this.

 

Here is my code:

 

trigger TestTrigger1 on Obj__c(after insert, after update)

{

set<Id> cntctIds = new set<Id>();

List<obj__c> list2 = new List<obj__c>();

List<obj__c> list3 = new List<obj__c>();

List<obj__c> list1=[select id,Contact__c, field1, field2,.... from obj__c where id In:trigger.newMap.keyset()];

for(Integer j=0;j< list1.size();j++)
    {
        cntctIds.add(list1[j].Contact__c);
    }

objCntct = [select field1,field2 from obj__c where Contact__c IN:cntctIds];

list2 = [select field1,field2 from obj__c where Contact__c IN:cntctIds and field1 = '123'];

list3 = [select field1,field2 from obj__c where Contact__c IN:cntctIds and field1 = ''456];

for(Integer k=0;k< objCntct.size();k++)

{

for(Integr l=0;l<list2.size();l++)

{

//code goes here...

}

 

}

}

 

 

i am doing anything wrong here?

I feel the only reason could be that i am using nested for loops so there might be infinite loops.

Or how to make use of maps to use in loops?

How to avoid this does any one have soln?

 

Thanks

Shaan

Issue in apex trigger while retrieving the information from user object.

 

string uid = userinfo.getUserId();

 

User[] user = [select name from User where u.Id = uid];

 

 

for this i am getting an exception in apex trigger as:

 

Too many SOQL queries: 21

I tried using sets but how do we get total collection of userInfo to iterate in for loop and add ids of users to set .

please let me know if any soln.