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
mac adminmac admin 

Error in trigger in production

Hi all,
Below is my trigger to update the status in the other object if the email is matched then it will update the status in the other object.
trigger verifyUG on UnderGradute__c (after insert) {
list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();

    list<Form_Leads__c> test  = new   list<Form_Leads__c> ();
    list<Form_Leads__c> test1  = new  list<Form_Leads__c> ();
    list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();
    
 for(UnderLad__c IL :trigger.new){
if (IL.Email__c != null){
Email.add(IL.Email__c);
}
if (IL.Cell_Phone_Number__c!= null){
phone.add(IL.Cell_Phone_Number__c);
}
}
test1=[SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c FROM Form_Leads__c WHERE Email__c IN : Email limit 5000 ];
    
test=[select Cell_Phone_Number__c,Email__c, Converted_Status__c from Form_Leads__c where Id IN: test1 limit 5000];
    
system.debug('////'+test);
    
for(Form_Leads__c t :test){
if(test.size()!=null){
t.Applied__c= true; 
t.Converted_Status__c='Matched';
test2.add(t);
}

}
update test2;
}
In Sandbox it is working fine but in production it throwing an error as  System.LimitException: Too many query rows: 50001.

Can anyone help me over here.
Thanks in advance.


Regards,
mac.

 
RD@SFRD@SF
Hi Mac, 

Can you also share the debug logs with the governor limits from the production?

Regards
RD
brahmaji tammanabrahmaji tammana
Hi mac,

That is a governor limit exception. It is recommended to use Limits class to avoid such error. 

I have little enhanced your trigger. Hope it works now.
trigger verifyUG on UnderGradute__c (after insert) {
    list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();
    
    list<Form_Leads__c> test  = new   list<Form_Leads__c> ();
    list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();
    
    for(UnderLad__c IL :trigger.new){
        if (IL.Email__c != null){
            Email.add(IL.Email__c);
        }
        if (IL.Cell_Phone_Number__c!= null){
            phone.add(IL.Cell_Phone_Number__c);
        }
    }
    test=[SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c 
           FROM Form_Leads__c WHERE Email__c IN : Email limit 5000 ];
    
    system.debug('////'+test);
    if(test.size() > 0){ 
        for(Form_Leads__c t :test){        
            t.Applied__c= true; 
            t.Converted_Status__c='Matched';
            test2.add(t);
        }  
        update test2;
    }
    
}

Thanks
Brahma
Vijay NagarathinamVijay Nagarathinam
Hello,

Can you try the below code and let me know its working or not.
 
trigger verifyUG on UnderGradute__c (after insert) {
list<string> Email = new    list<string> ();
list<string> phone = new    list<string> ();

list<Form_Leads__c> test1  = new  list<Form_Leads__c> ();
list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();
Set<Id> underLeadIdSet = new Set<Id>();

for(UnderLad__c IL :trigger.new){
	if (IL.Email__c != null){
		Email.add(IL.Email__c);
	}
	if (IL.Cell_Phone_Number__c!= null){
		phone.add(IL.Cell_Phone_Number__c);
	}
	underLeadIdSet.add(IL.id);
}
test1=[SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c FROM Form_Leads__c WHERE Email__c IN : Email AND Id IN: underLeadIdSet limit 5000 ];


system.debug('////'+test);

for(Form_Leads__c t :test1){
		t.Applied__c= true; 
		t.Converted_Status__c='Matched';
		test2.add(t);
}
update test2;
}

Thanks,
Vijay
mac adminmac admin
Hi brahmajit,
Thanks  for the replay. I changed my trigger with your code but no change in the issue. It's working fine in Sandbox but throwing the error as System.LimitException: Too many query rows: 50001. May I know why this of issue will occour in Production and not why in sandbox...?
And can you help me on this.

Thanks in advance.
Regards,
mac.
mac adminmac admin
Hi Vijay,
Thanks for the reply and I'm able to save the record but the fields in the Form_Leads are not updating can you please help over here.

for(Form_Leads__c t :test1){
      t.Applied__c= true;
      t.Converted_Status__c='Matched';
       test2.add(t);
}
update test2;
}

Can I replace the IF condition ( if(test.size() > 0){ ) here..?
Thanks in advance.

Regards,
mac.
Vijay NagarathinamVijay Nagarathinam
Hi,

Can you try the below updated code.
 
trigger verifyUG on UnderGradute__c (after insert) {

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

list<Form_Leads__c> test1  = new  list<Form_Leads__c> ();
list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();
Set<Id> underLeadIdSet = new Set<Id>();
Map<String,Id> underLeadMap = new Map<String,Id>();

for(UnderLad__c IL :trigger.new){
	if (IL.Email__c != null){
		Email.add(IL.Email__c);
		underLeadMap.put(IL.Email__c,IL,Id);
	}
	
	
}
test1=[SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c FROM Form_Leads__c WHERE Email__c IN : Email AND Id IN: underLeadMap.values() limit 5000 ];


system.debug('////'+test);

for(Form_Leads__c t :test1){
Form_Leads__c fl = new Form_Leads__c();
if(underLeadMap.containsKey(t.Email__c)){
		fl.Id = underLeadMap.get(t.Email__c);
		fl.Applied__c= true; 
		fl.Converted_Status__c='Matched';
		test2.add(fl);
}
}
update test2;
}

 
RD@SFRD@SF
Hi Mac

Its because of the query hit

Please go through the following link (http://salesforce.stackexchange.com/questions/28228/system-limitexception-too-many-query-rows-50001)for the details

Hope it helps
RD
Vijay NagarathinamVijay Nagarathinam
Hi Mac,

Have you fixed your limitation issue?? Hope my code will resolve your issue.

Thanks,
Vijay
mac adminmac admin
Hi Vijay,

The limitation issue is cleared but the fields are not updating.

for(Form_Leads__c t :test1){
Form_Leads__c fl = new Form_Leads__c();
if(underLeadMap.containsKey(t.Email__c)){
      fl.Id = underLeadMap.get(t.Email__c);
       fl.Applied__c= true;
       fl.Converted_Status__c='Matched';
        test2.add(fl);
}
}
update test2;
}


can you help me over here

Thanks in advance.

regards,
mac.
Vijay NagarathinamVijay Nagarathinam
Try the below code,
 
for(Form_Leads__c t :test1){
Form_Leads__c fl = new Form_Leads__c();
       fl.Applied__c= true;
       fl.Converted_Status__c='Matched';
        test2.add(fl);
}
update test2;
}

 
mac adminmac admin
No Vijay ,
Still the fields are not updating. Below is my code which I have changed as per your code.
trigger verifyUG on UnderLdes__c (after insert) {

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

list<Form_Leads__c> test1  = new  list<Form_Leads__c> ();
list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();
Set<Id> underLeadIdSet = new Set<Id>();
Map<String,Id> underLeadMap = new Map<String,Id>();

for(UnderLdes__c IL :trigger.new){
	if (IL.Email__c != null){
		Email.add(IL.Email__c);
		underLeadMap.put(IL.Email__c,IL,Id);
	}
	
	
}
test1=[SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c FROM Form_Leads__c WHERE Email__c IN : Email AND Id IN: underLeadMap.values() limit 5000 ];


system.debug('////'+test);

for(Form_Leads__c t :test1){
Form_Leads__c fl = new Form_Leads__c();
       fl.Applied__c= true;
       fl.Converted_Status__c='Matched';
        test2.add(fl);
}
update test2;
}

 
Vijay NagarathinamVijay Nagarathinam
Is there any relationship between Form_Leads__c  and UnderLdes__c  object.

If the object having relationship means can you add relationship field in Form_Leads__c   object.

Thanks,
Vijay
mac adminmac admin
There is no relationship between both the objects.
Vijay NagarathinamVijay Nagarathinam
Can you explain your scenario, I will try to update your logic.

Thanks,
Vijay
mac adminmac admin
My requirement is as follow.
I want to update the Converted_Status__c field in Form_Lead__c, when ever the email ID & Phone Number in the UnderLdes__c is same as the email ID and Phone Number in the Form_Leads__c.

Thanks in advance.

Regards,
mac.
Vijay NagarathinamVijay Nagarathinam
Hi,

Can you try the below code, Hope it will work
 
trigger verifyUG on UnderLdes__c (after insert) {

Set<String> Email = new Set<String>();
Set<String> PhoneNumberSet = new Set<String>();

list<Form_Leads__c> test1  = new  list<Form_Leads__c> ();
list<Form_Leads__c> test2  = new  list<Form_Leads__c> ();

for(UnderLdes__c IL : trigger.new){
	if (IL.Email__c != null){
		Email.add(IL.Email__c);
	}
	if (IL.Cell_Phone_Number__c!= null){
		PhoneNumberSet.add(IL.Cell_Phone_Number__c);
	}
}
test1 = [SELECT id, Email__c, Cell_Phone_Number__c, Converted_Status__c FROM Form_Leads__c WHERE Email__c IN : Email AND 
Cell_Phone_Number__c IN: PhoneNumberSet limit 5000 ];


if(test1.size() > 0){
	for(Form_Leads__c t :test1){
		Form_Leads__c fl = new Form_Leads__c();
		f1.Id = t.Id;
		fl.Applied__c = true;
        fl.Converted_Status__c = 'Matched';
        test2.add(fl);
	}
}
if(test2.size() > 0{
	update test2;
}
}

 
mac adminmac admin
Vijay will saving the record it throwing error as follow:
Error: Compile Error: Variable does not exist: f1.Id at line 24 column 9
Vijay NagarathinamVijay Nagarathinam
Use

fl.Id = t.Id

Thanks,
Vijay
mac adminmac admin
It's again throwing error as  System.LimitException: Too many query rows: 50001. in my Production.