+ Start a Discussion
Yamini BathulaYamini Bathula 

apex trigger not working for certain users

Hi Guys,

We have a before Update Trigger on Lead which fires on Lead conversion to check some mandatory values and just add error using Apex.adderror() method to display the error message to the user. This trigger is not firing for some users instead of trigger's error message they are getting an error message which is the result of a process builder which tries to update an Opportunity name when an Oporutnity is created. When I(system admin) login as them and try it is displaying the trigger's error message. 

I am not sure what is happening.

Does any one have any idea or had the same issue?

 
Abhishek BansalAbhishek Bansal
Hi Yamini,

Can you please let us know if you are using any helper class for your trigger and If Yes. the  what is the mode of your helper class. Is it With Sharing or Without Sharing? If you are not using any helper class then please post your trigger code here so that we can have a look and suggest something to you.

Thanks,
Abhishek Bansal.
Yamini BathulaYamini Bathula
Thanks for replying Abhishek. there is no helper class for my trigger. here is the code.
 
trigger MandatoryFieldsLeadConversion on Lead (before Update) 
{
        for (Lead l : Trigger.new)
         {
             Boolean hasError = false;
            if (Trigger.oldMap.get(l.id).isConverted == false && Trigger.newMap.get(l.id).isConverted == true) // to check only in Lead Coversion
             {  
                 String erstring = 'Please Enter Below Mandatory Fields before converting this Lead ';
               //Lead l = [Select Id,ParentAccount__c,FirstName,Email,Phone,Company,Country__c,Industry_Ipay__c,Trading_Name__c,Street,State,City,PostalCode,Sales_Channel__c,Avg_Trans_Amount__c,Trans_Per_Month__c,Max_Single_Pay_Amt__c,Director_1_First_Name__c,Director_1_Last_Name__c,Director_Email__c,Direct_Debit_Requested__c,eCommerce__c,Video_Payment__c,Xero_Payment_Now__c,Xero_Direct_Debit__c From Lead Where Id in  :trigger.new[i]];
                 //Skip this check for V3 and IODM Leads
               if((l.ParentAccount__c != '0019000001YHuxWAAT') && (l.ParentAccount__c != '0016F00001i2yYHQAY'))
                 { 
                     //if Parent Account is Blank
                   if((l.ParentAccount__c == null) || (l.ParentAccount__r.Name ==  ' '))
                   { 
                    erstring += 'Parent Account,   ' ; 
                    hasError = true; 
                   }
                     //if First Name is Blank
                   if((l.FirstName == null) || (l.FirstName ==  ' '))
                   {
                     erstring += 'First Name,  '  ;
                     hasError = true; 
                   }
                     //if Email is Blank
                   if((l.Email == null) || (l.Email ==  ' '))
                   {
                     erstring += 'Email,  ';
             
                     hasError = true; 
                   }
                      //if Phone is Blank
                   if((l.Phone == null) || (l.Phone ==  ' '))
                   {
                     erstring += 'Phone,  ';
                     hasError = true; 
                   }
                      //if Company is Blank
                   if((l.Company == null) || (l.Company ==  ' '))
                   {
                     erstring += ' Company,  ';
                     hasError = true; 
                   }
                      //if Country is Blank
                   if((l.Country__c == null) || (l.Country__c ==  ' '))
                   {
                     erstring += 'Country,   ';
                     hasError = true; 
                   } 
                      //if Industry is Blank
                   if((l.Industry_Ipay__c == null) || (l.Industry_Ipay__r.Name ==  ' '))
                   {
                     erstring += 'Industry,  ';
                     hasError = true; 
                   }  
                      //if Parent Account is Blank
                   if((l.Trading_Name__c == null) || (l.Trading_Name__c ==  ' '))
                   {
                     erstring += 'Trading Name,  ';
                     hasError = true; 
                   }
                      //if Address is Blank
                   if((l.Street == null) || (l.Street ==  ' '))
                   {
                     erstring += 'Street,  ';
                     hasError = true; 
                   }
                    if((l.State == null) || (l.State ==  ' '))
                   {
                     erstring += 'State,  ';
                     hasError = true; 
                   }
                   if((l.City == null) || (l.City ==  ' '))
                   {
                     erstring += 'City,  ';
                     hasError = true; 
                   }   
                   if((l.PostalCode == null) || (l.PostalCode ==  ' '))
                   {
                     erstring += 'Post Code,  ';
                     hasError = true; 
                   } 
                      //if Sales channel is Blank
                   if((l.Sales_Channel__c == null) || (l.Sales_Channel__c ==  ' '))
                   {
                     erstring += 'Sales Channel,  ';
                     hasError = true; 
                   } 
                      //if Average Transaction Amount is Blank
                   if((l.Avg_Trans_Amount__c == null) )
                   {
                     erstring += 'Average Transaction Amount,  ';
                     hasError = true; 
                   } 
                      //ifTransactions per month is Blank
                   if((l.Trans_Per_Month__c == null) )
                   {
                     erstring += 'Transactions per month,  ';
                     hasError = true; 
                   }
                      //if Max Singl ePay Amount is Blank
                   if((l.Max_Single_Pay_Amt__c == null))
                   {
                     erstring += 'Maximum single payment amount,  ';
                     hasError = true; 
                   }
                      //ifDirector Details Blank
                   if((l.Director_1_First_Name__c == null) || (l.Director_1_First_Name__c ==  ' '))
                   {
                     erstring += 'Director First Name,  ';
                     hasError = true; 
                   } 
                   if((l.Director_1_Last_Name__c == null) || (l.Director_1_Last_Name__c ==  ' '))
                   {
                     erstring += 'Director Last Name,  ';
                     hasError = true; 
                   } 
                   if((l.Director_Email__c == null) || (l.Director_Email__c ==  ' '))
                   {
                     erstring += 'Director email,  ';
                     hasError = true; 
                   } 
                      //if no products selected 
                   if(!((l.Direct_Debit_Requested__c) || (l.eCommerce__c) || (l.Video_Payment__c) || (l.Xero_Payment_Now__c) || (l.Xero_Direct_Debit__c) ))
                   {
                     erstring += 'One of the products Direct Debit Requested/Ecommerce/Video Payment/Xero Payment Now/Xero Direct Debit is not selected';
                     hasError = true; 
                   } 
                      //Finally add the error message if any of the fields above are blank
                   if(hasError)
                   {
                     l.addError(erstring,false);
                   }
                  }
             }  
         }
    
   
}

 
Chellappa NagarajanChellappa Nagarajan
Hi Yamini,
Couple of questions - 
1. Is there a reason all your validations are present in trigger? Can't you put all these in a Validation rule instead of Triggers as given above.
2. Have you enabled Validations and Triggers for Lead Convert in your organization?
3. Also, is the Process builder related to Lead conversion process?

Thanks,
Chellappa
Chellappa NagarajanChellappa Nagarajan
Hi Yamini,
Couple of questions - 
1. Is there a reason all your validations are present in trigger? Can't you put all these in a Validation rule instead of Triggers as given above.
2. Have you enabled Validations and Triggers for Lead Convert in your organization?
3. Also, is the Process builder related to Lead conversion process?

Thanks,
Chellappa
Yamini BathulaYamini Bathula
1.Is there a reason all your validations are present in trigger? Can't you put all these in a Validation rule instead of Triggers as given above.
   Yes, We first tried with Validation rules. I cannot customise the Message for each field if I Use single validation rule. If i use multiple validation rules then they dont fire once. They will fire one after the other so it is difficult to the user every time they have to go to back page for every rule.

2. Have you enabled Validations and Triggers for Lead Convert in your organization? Yes
3. Also, is the Process builder related to Lead conversion process? Process builder updates the opportunity name when an opp is created. So in a way it is related to Lead Conversion as it creates an Opportunity too.
Abhishek BansalAbhishek Bansal
Hi Yamini,

I can't see or figure out any reason that the trigger is not working for some users and for others it is working fine. However, one thing which I noticed is, you are adding the error on the basis of one condition i.e. if((l.ParentAccount__c != '0019000001YHuxWAAT') && (l.ParentAccount__c != '0016F00001i2yYHQAY')). 
The users for which trigger is not working may be using leads where Parent Account is same as you have mentioned in the if condition. Can you please check this. Other than this I don't see any reason for the inconsistent behaviour of the trigger.

Thanks,
Abhishek Bansal.