0

I wrote Scheduler class and Apex trigger and deployed Scheduler class successfully from sandbox to production but when i deploy trigger from sandbox to production then I got an error.

enter image description here

Trigger:

    trigger UpdateAccountSubscriptionEmail on Change_Request__c (before insert,before update) {

    map<Id, Account> mapAccount = new map<Id, Account>();
    List<Id> listIds = new List<Id>();
    public boolean error = false;


     for(Change_Request__c crchild : Trigger.new)
     {
        listIds.add(crchild.Lead_Merchant_Name__c); 

     }

     mapAccount = new Map<Id, Account>([SELECT id,Subscription_Email__c,RecordTypeId,(SELECT Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Request_Type__c 
                                            FROM Change_Requests__r) FROM Account]);

      Map<String,Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Account.getRecordTypeInfosByName();
        Id rtLeadID =  rtMap.get('Lead').getRecordTypeId();                                     

     system.debug('mapAccount.values() -->'+mapAccount.values());                                       
     for(Change_Request__c CR : Trigger.new)
     {
        for(Account acc : mapAccount.values())
        {
          if(acc.Subscription_Email__c != null)
          { 

            if(trigger.isUpdate && trigger.newMap.get(CR.Id).New_Value__c != trigger.oldMap.get(CR.Id).New_Value__c && CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email')
            {
                if(CR.Updated__c == false){ 

                     system.debug('CR.New_Value__cc -->'+CR.New_Value__c);
                     system.debug('acc.Subscription_Email__c -->'+acc.Subscription_Email__c);   
                     error = true;
                     CR.New_Value__c.addError('<span><b>Duplicate email found with the same email</b></br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kindly update this email as alternate email!</b></span>',false);
                //return null;
                }


            }
          } 
        }
     }
     if(!error){
     for(Change_Request__c CR : Trigger.new)
     {  
         Account  myParentAcc = mapAccount.get(CR.Lead_Merchant_Name__c);
     if(myparentAcc.RecordTypeId == rtLeadID)
     {   
         if(CR.Request_Type__c == 'Contact Email'){
            system.debug('CR.Request_Type__c -->'+CR.Request_Type__c);
            myParentAcc.Subscription_Email__c = CR.New_Value__c;    
         }  
     }  
    }
     update mapAccount.values();    
     }                              
   }

Scheduler class:

   global class update_Account implements Schedulable    
{
    global void execute(SchedulableContext sc) 
    {
    Map<Id,Account> AccountToChangeRequest=new Map<Id,Account>([Select id,name,(Select Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Request_Type__c from Change_Requests__r where Updated__c= False  and Lead_Merchant_Name__c!= NULL order by CreatedDate asc limit 1)from Account]);
    system.debug('--AccountToChangeRequest--->>'+AccountToChangeRequest);
    list<Account>AccountToBeUpdated=new List<Account>();
    //system.debug('--AccountToBeUpdated--->>'+AccountToBeUpdated);
    list<Change_Request__c> changerequestToBeUpdated=new List<Change_Request__c>();
    ///system.debug('--changerequestToBeUpdated--->>'+changerequestToBeUpdated);
    for(Id i:AccountToChangeRequest.keyset()) 
    { 
         if(AccountToChangeRequest.get(i).Change_Requests__r.size()>0)
         {
            Account acc=new Account(id=i);
            Change_Request__c cr=new Change_Request__c(id=AccountToChangeRequest.get(i).Change_Requests__r[0].id);
            if(AccountToChangeRequest.get(i).Change_Requests__r[0].Request_Type__c=='First Name')
            {
                acc.firstname=AccountToChangeRequest.get(i).Change_Requests__r[0].New_Value__c;
                //system.debug('--acc.firstname--->>'+acc.firstname);
                cr.Updated__c=true;
                //system.debug('--cr--->>'+cr);
            }
            else if(AccountToChangeRequest.get(i).Change_Requests__r[0].Request_Type__c=='Last Name')
            {
                acc.lastname=AccountToChangeRequest.get(i).Change_Requests__r[0].New_Value__c;
                system.debug('--acc.lastname--->>'+acc.lastname);
                cr.Updated__c=true;
               // system.debug('--cr--->>'+cr);
            }
            else if(AccountToChangeRequest.get(i).Change_Requests__r[0].Request_Type__c=='Phone')
            {
                acc.Phone=AccountToChangeRequest.get(i).Change_Requests__r[0].New_Value__c;
                system.debug('--acc.Phone--->>'+acc.Phone);
                cr.Updated__c=true;
               // system.debug('--cr--->>'+cr);
            }
            else if(AccountToChangeRequest.get(i).Change_Requests__r[0].Request_Type__c=='Alternate Phone')
            {
                acc.Alternate_Phone__c=AccountToChangeRequest.get(i).Change_Requests__r[0].New_Value__c;
                system.debug('--acc.Alternate_Phone__c--->>'+acc.Alternate_Phone__c);
                cr.Updated__c=true;
               // system.debug('--cr--->>'+cr);
            }
            else if(AccountToChangeRequest.get(i).Change_Requests__r[0].Request_Type__c=='Alternate Email')
            {
                acc.Alternate_Email__c=AccountToChangeRequest.get(i).Change_Requests__r[0].New_Value__c;
                system.debug('--acc.Alternate_Email__c--->>'+acc.Alternate_Email__c);
                cr.Updated__c=true;
               // system.debug('--cr--->>'+cr);
            }
            AccountToBeUpdated.add(acc);
            changerequestToBeUpdated.add(cr);

        }

    }
            if(!AccountToBeUpdated.isEmpty())
            update AccountToBeUpdated;
            system.debug('--AccountToBeUpdated--->>'+AccountToBeUpdated);
            if( !changerequestToBeUpdated.isEmpty())
            update changerequestToBeUpdated;
            system.debug('---changerequestToBeUpdated-->>'+changerequestToBeUpdated);
    }
}
2
  • I think it is because of the test case failure. Check the test class for your Trigger. Commented Oct 20, 2015 at 10:48
  • Yes rohit my scheduler class failed. Time Started 20/10/2015 19:18 Class TestUpdate_Account Method Name test1 Pass/Fail Fail Error Message System.DmlException: Update failed. First exception on row 2 with id a0LN0000003abIaMAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, <span><b>Duplicate email found with the same email</b></br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kindly update this email as alternate email!</b></span>: [New_Value__c] Stack Trace Class.update_Account.execute: line 93, column 1
    – user24524
    Commented Oct 20, 2015 at 11:36

1 Answer 1

2

Your test class need to be updated to have data in par with the trigger functionality. You must be updating a Change Request which satisfies this condition:

if(CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email')

and hence the issue.

Its better to check for a change of value in Update trigger like:

if(trigger.isUpdate && trigger.newMap.get(CR.Id).New_Value__c != trigger.oldMap.get(CR.Id).New_Value__c && CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email') {
CR.New_Value__c.addError('msg',false);
}

Or you must be using an existing record in org which satisfies the if loop

if(CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email')

in your test class?

2
  • Thanks @Shruthi for replying comment. I updated my trigger but I am not able to show error message in that trigger.
    – user24524
    Commented Oct 26, 2015 at 8:43
  • You mean, it satisfies this condition if(trigger.isUpdate && trigger.newMap.get(CR.Id).New_Value__c != trigger.oldMap.get(CR.Id).New_Value__c && CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email') { CR.New_Value__c.addError('msg',false); but doesnt show the error message? Have you tried debugging the old value, new value and request type? May be there is a workflow rule or a before update trigger which changes request type from contact email to something else before this part of the trigger runs?
    – Shruthi
    Commented Oct 28, 2015 at 2:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .