+ Start a Discussion
StradaStrada 

Problem deploying update code to live system.

Hi guys,

I'm wondering if anyone can please help or at least point me in the direction of where I can get help for this issue.

 

For some reason I can't deploy some updated code from my sandbox to our live system. The last code deployed was on 16/07/2012, and this comprised of an updated trigger and test class.

 

Today I have tried to deploy a small change to both the trigger and test class and now I am getting a validation failure on a different object - validation of an assert in a different test class is occurring for some reason that I cannot explain. I have tried to run tests in my sandbox in order to recreate this assertion failure but I cannot reproduce the problem in my sandbox.

 

This is very strange and I am at a loss to figure out how this happened in the first place and more importantly how to resolve the issue. As it stands I cannot deploy any code to the live system now because of this.

If anyone can provide any help to me on this I would greatly appreciate it.

Best Answer chosen by Admin (Salesforce Developers) 
sbbsbb

As you discovered, the DeDupeLead is setting doNotRoundRobin__c to True. Whoever coded it probably intended it to be so. If that is the case, you just need to fix your test code to use something else (something you are expecting to happen) in those assert statements. 

 

The reason you want to use asserts is that you are expecting your code to do something with the test data you are feeding it, and you know what the result is going to be. The 'Asserts' tell you if your code behaved as you expected or not.

 

Hope that was helpful.

All Answers

@anilbathula@@anilbathula@

Hi Strada,

 

I think u have written a validation rule on a specific object in production instance.

Which was not there in sandbox.When ur deploying ur code to production the validation rule is missing

in your test method thats the reason its giving this error.

My better suggestion is make all your validation rules inactive untill u push this code into production.

After that enable ur validation rules.

 

 

Thanks

Anil.B

StradaStrada

Hi Anil,

 

Thanks for your suggestion. I have cross-checked all validation rules and I haven't been able to find any differences between the ones that are currently there on live and the ones that are in my sandbox system. I know that I didn't add any recently either so I'm not sure if this is the problem.

 

What I have tried in the meantime is I just added some debug statements to the test class where the assertion is failing and I have tried to re-deploy it to live but I get the same failure error. So it looks like I can't even update the test class where the assertion is failing.

 

Can you think of anything else that I might be missing?

 

Thanks again for your help Anil.

 

Prady01Prady01

Hello there the from what i have read in your post i could think of a few things, Is your trigger somehow related to the object thats throwing an error and probably in the test class while insering the test data you might have missed a field that had a validation rule!!... 

 

Hope it helps!!

StradaStrada

Hi Pradeep,

 

Thanks but unfortunately it's not. The code that I attempted to update and redeploy is the same trigger and class that I added back on the 16th (last week).

 

My trigger and test class deals with 2 custom objects. The error that I am getting is in a test class on the Lead object. Nowhere in my code am I referring to the lead object and I have checked all validation rules and can't see how this error is being thrown because it wasn't happening last week when I deployed the code.

 

I have tried to redeploy the test class on the Lead object, where this error is happening, but each time I try the validation refers to this same error.

 

This class called "TestDeDupeLead" hasn't been updated for a couple of months so I'm at a loss as to explain how all of a sudden this has happened.

 

The only difference I made to the code was I commented out 2 lines of code that changed the status of the custom object. The status field does not have any validation rules associated with it. The 2 custom objects being referred to in this code are completely unrelated to the "Leads" object. 

 

Thanks.

StradaStrada

-------------------------------------

PLEASE HELP IF YOU CAN

-------------------------------------

 

Just another note on this problem. I have run an "Apex Test Execution" on my code from live and here are the results:

 

Test Class that I currently want to update TestChangeRequestCompleted: 1/1 Test Methods Passed

 

Test Class that has failed (and hasn't been updated in last few months) Test DeDupeLead: 6/7 Methods Passed and the fail reads:

 

"System.AssertException: Assertion Failed: Expected: false, Actual: true"

 

I don't understand how this is preventing me from deploying the updated Trigger and Test Class as they are unrelated to this object that the other test is failing on. I have a copy of Deployment Log but it makes no sense to me as it doesn't seem to be referencing any of the test method names in this class.

 

If anyone can please let me know if they ever came across something similar and how you resolved it I would really appreciate it.

 

Many thanks again for all your help!

Prady01Prady01

Hello there put up some code or some debug logs, and when deploying any code from one environment to the other all the test classes in the org run and the average code coverage on the entire apex classes in complete org should be more than 75% then only you can deploy the code, plz its pretty difficult to understand your problem diligently in detail with only so much input, But i think the problem is that somewhere in your test class you have inserted the data thats not matching the validation rule!!

 

 

StradaStrada

Hi Pradeep,

 

I appreciate your effort to help me out here. Ok, our average code coverage is 90% so there doesn't seem to be a problem there. The code that is now failing was last updated over 3 months ago and I haven't changed or added any additional validation rules into our live system.

 

-----------------------------------------------------------

The failing method in my test class code is:

----------------------------------------------------------

 

private static testmethod void testLeadActivity3(){
      //Create an Active Account
        Account newAccount = new Account (name='XYZ Organization');
        newAccount.BillingCountry = 'Ireland';
        newAccount.Status__c = 'Active';
        newAccount.OwnerId = '00520000001dRbbAAE';
        insert newAccount;
        
        List <Account> savedAccount = [Select Status__c from Account where name = 'XYZ Organization'];
        
        //Create Active Contacts
        Contact mycontact = new Contact (lastName='XYZ Contact',Contact_Status__c = 'Active', MailingCountry='Ireland', Email='test@testmyDeDupe.com', OwnerId = '00520000001dRbbAAE');
        mycontact.accountId = savedAccount[0].Id;
        insert mycontact;
        
        Lead l = new Lead(LastName = 'AnotherRandomname1', Company = 'Another ABC, Inc.', Email='testAnother2@testmyDeDupe.com', Timestamp_Hashed__c='12');
        insert l;
        List<Lead> savedLead = [select doNotRoundRobin__c from Lead where Email = 'testAnother2@testmyDeDupe.com'];
        System.assert(savedLead[0].doNotRoundRobin__c);
    }

 

---------------------------

TRIGGER CODE IS:

---------------------------

 

trigger DeDupeLead on Lead (before insert, after insert) {
  List <Lead> dupeLeads = new List<Lead>();
  List <Contact> dupeContacts = new List<Contact>();
  String emailDomainLike = '';
  // no bulk processing; will only run from the UI
   for (Lead newLead : Trigger.new) {
    if (Trigger.new.size() == 1) {
      if(newLead.Email != '' && newLead.Email != null){
        System.debug('Inside first if statement..');
        dupeLeads = [Select l.Id, l.RecordTypeId, l.OwnerId, l.Email From Lead l Where l.Email = :newLead.Email]; //Email Exact Match
        if(dupeLeads.size() >= 1){
          doDeDupeActions(dupeLeads[0].RecordTypeId, dupeLeads[0].OwnerId, newLead);
        }else{
          String domain = getDomainString(newLead.Email);
          if((!isPublicEmailAddress(domain))){
            emailDomainLike = getDomainLikeString(domain);
            dupeLeads = [Select l.RecordTypeId, l.Id, l.OwnerId, l.Email From Lead l Where l.Email like :emailDomainLike]; //Email Domain match.
            if(dupeLeads.size() >= 1){
              doDeDupeActions(dupeLeads[0].RecordTypeId, dupeLeads[0].OwnerId, newLead);
            }          
          }
        }
      }
      if(dupeLeads.size() == 0){ // If there are no matches after email check.
         
         dupeLeads = [Select l.RecordTypeId, l.Id, l.OwnerId, l.Email From Lead l Where  l.company = :newLead.Company]; //Company Only Match
         if(dupeLeads.size() >=1){
            doDeDupeActions(dupeLeads[0].RecordTypeId, dupeLeads[0].OwnerId, newLead);
         }
         
         dupeLeads = [Select l.RecordTypeId, l.Id, l.OwnerId, l.Email From Lead l Where  (l.company = :newLead.Company and l.Phone = :newLead.Phone)]; //Company & Phone Match
         if(dupeLeads.size() >= 1){
           doDeDupeActions(dupeLeads[0].RecordTypeId, dupeLeads[0].OwnerId, newLead);
         }else{
           dupeLeads = [Select l.RecordTypeId, l.Id, l.OwnerId, l.Email From Lead l Where  (l.company = :newLead.Company and l.Name = :newLead.Name)]; //Company & Name Match
           if(dupeLeads.size() >= 1){
             doDeDupeActions(dupeLeads[0].RecordTypeId, dupeLeads[0].OwnerId, newLead);
           }
         }
      }
      
       if(dupeLeads.size() == 0  && newLead.Email != null){ // Still no leads after email + company + Phone + Name Match
         //Check contacts for email domain match
         String domain = getDomainString(newLead.Email);
        if(!isPublicEmailAddress(domain)){
        emailDomainLike = getDomainLikeString(domain);
        dupeContacts = [Select l.Id, l.OwnerId, l.Email From Contact l Where l.Email like :emailDomainLike]; //Email Domain match.      
           if(dupeContacts.size() >= 1){
             doDeDupeActions(null, dupeContacts[0].OwnerId, newLead);
           }
        }
       }
    }
  }
 
 
  private boolean isPublicEmailAddress(String domain){
    if(domain == 'gmail' || domain == 'yahoo' || domain == 'hotmail' || domain == 'googlemail' || domain == 'live'){
      return true;
    }return false;
  }
 
 
  private void doDeDupeActions(Id existingLeadRecordTypeId, Id dupeOwnerId, Lead newLead){
    if(Trigger.isBefore){
      if(existingLeadRecordTypeId == '01220000000YZ4HAAW'){ //if existing record is marketing lead set to remarket
        newLead.doNotRoundRobin__c = false; //do round robin the lead
        newLead.hasDupeInMarketing__c = true; //set hasDupeInMarketing flag to true
      }else{    
         newLead.OwnerId = dupeOwnerId;
         newLead.doNotRoundRobin__c = true;
      }    
    }else{
      //Marketing Lead & PreSlaes leads should not trigger an email to the owner: SF-680
      if ((Trigger.isAfter) && (newLead.doNotRoundRobin__c == true) && (newLead.RecordTypeId != '01220000000YZ4HAAW') && (newLead.Status != 'Presales')){
      //Do something else if the owner is a Queue!    
        //try{
          sendMail(newLead.OwnerId, newLead.Id);
        //}catch(Exception e){
            //do nothing..
        //}    
      }else if(newLead.hasDupeInMarketing__c == true){//New Lead has an existing dupe in Marketing leads
        //Do something else if the owner is a Queue!    
        //try{
          sendMailToMarketing(newLead.OwnerId, newLead.Id);
        //}catch(Exception e){
            //do nothing..
        //}
      }
    }
  }
 
  private String getDomainString(String emailAddress){
    String emailDomain = null;
    String[] firstSplit = emailAddress.split('@');
  String firstone =  firstSplit[1];
  String[] secondSplit = firstone.split('\\.');
  if(secondSplit.size() >= 3 && secondSplit[1] != 'co'){ //check for e.g. uk.mydomain.com but not mydomain.co.uk
    emailDomain = secondSplit[0] + '.' + secondSplit[1];
  }else{
    emailDomain = secondSplit[0];  
  }  
  return emailDomain;
  }
 
 
  private String getDomainLikeString(String domain){
  return '%@'+domain+'.%';
  }
 
  public void sendMail(Id uId, id leadId){
  if(!(String.valueOf(uId)).startsWith('00G')){ //dont try to send a mail if owner is a Queue
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
      mail.setTargetObjectId(uId);      
      mail.setTemplateId('00X20000001ZZycEAG');
      mail.setSaveAsActivity(false);
      mail.setWhatId(leadId);   
      System.debug('Sending Email');
      
      results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      }
  }
 
    public void sendMailToMarketing(Id uId, id leadId){
      List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
      String[] toAddresses = new String[] {'marketing@newsweaver.com'};
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId('00520000001d8eoAAA'); //mmoynihan
      mail.setToAddresses(toAddresses);    
      mail.setTemplateId('00X20000001DXuY');
      mail.setSaveAsActivity(false);
      mail.setWhatId(leadId);   
      System.debug('Sending Email');      
      results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }
}

 

 

The deployment logs run to almost 10,000 lines long so I won't even attempt to paste any of that up here. We're approaching out of business hours here now so I plan to try and debug by disabling validation rules to see if this gets rid of the failure and then re-activate the rules 1 by 1 to see if this gets me any closer to a solution.

 

In the meantime if you have any other suggestions then do let me know.

 

Once again, thank you so much for your help - it is very much appreciated!

sbbsbb

I see that you have hardcoded the IDs (owner, recordTypeID, etc.) throughout your code. It is really a bad idea, and your issue is probably related to that. Are those IDs still valid in your production environment?

 

Also note that when you deploy some code into production environment, ALL tests are run automatically. Even the ones you haven't touched in a while.

StradaStrada

Hi sbb,

 

Thanks for your response. I take onboard your point about the hard-coding the ids - I do plan to rework this code soon and remove this hardcoding but right now time isn't on my side. All of the ids are indeed valid in the production system. Thanks for clarifying what exactly goes on during code deployment.

 

I incorrectly posted the test code snippet earlier - here is the actual method where the assertion is failing:

 

---------------------

TEST METHOD

---------------------

 

private static testmethod void testTwoPartDomain3(){
        Lead l = new Lead(LastName = 'Randomname', FirstName = 'one', Company = 'ABC Inc', Email='test@in.g4s.com');
        insert l;
        List <Lead> savedLead = [select doNotRoundRobin__c from Lead where LastName = 'Randomname' and Email = 'test@in.g4s.com'];
        System.assertEquals(false, savedLead[0].doNotRoundRobin__c);
        
        Lead l2 = new Lead(LastName = 'Randomname1234', FirstName = 'one', Company = 'ABC Inc433', Email='test1@yahoo.coi.in');
        insert l2;
        savedLead = [select doNotRoundRobin__c from Lead where LastName = 'Randomname1234' and FirstName = 'one' and Company = 'ABC Inc433' and email = 'test1@yahoo.coi.in'];
        System.assertEquals(false,savedLead[0].doNotRoundRobin__c);
    }

 

Even if I try to modify this code and redeploy it the validation keeps failing at the same point in the code. The savedLead[0].doNotRoundRobin__c field keeps getting set to true hence why the assertion fails. Like I said this always worked fine before but for some reason over the last week this has changed.

 

Is there any way for me to be able to get around this and force in the code without the assertion statement just so that I can get some urgent fixes in for other triggers that need updating?

 

Many thanks again for your help!

sbbsbb

What type of field is 'doNotRoundRobin__c'? A checkbox? Does it have a default value? Maybe the default is 'True'?

StradaStrada

Hi sbb,

 

Here's a portion of the debug log that I pulled where the value of the lead owner is being changed and then the value of the doNotRoundRobin field is also being changed - I'm not great at reading debug logs yet in my early months of working in my Salesforce admin role:

 

18:00:45.306 (5306989000)|METHOD_ENTRY|[18]|01q20000000cH20|DeDupeLead.doDeDupeActions(Id, Id, Lead)
18:00:45.307 (5307017000)|VARIABLE_SCOPE_BEGIN|[63]|existingLeadRecordTypeId|Id|false|false
18:00:45.307 (5307054000)|VARIABLE_ASSIGNMENT|[63]|existingLeadRecordTypeId|"01220000000YZ4RAAW"
18:00:45.307 (5307067000)|VARIABLE_SCOPE_BEGIN|[63]|dupeOwnerId|Id|false|false
18:00:45.307 (5307079000)|VARIABLE_ASSIGNMENT|[63]|dupeOwnerId|"00520000001dH0OAAU"
18:00:45.307 (5307091000)|VARIABLE_SCOPE_BEGIN|[63]|newLead|Lead|true|false
18:00:45.308 (5308146000)|VARIABLE_ASSIGNMENT|[63]|newLead|{"RecordTypeId":"01220000000YZ4RAAW","IsConverted":false,"HasOptedOutOfFax":false,"OwnerId":"00520000001dRbbAAE","Online_Demo__c":false,"SFGA__Correlation_Da (5 more) ...":"none","Data_Quality_Score__ (1 more) ...":55,"LastName":"Randomname","IsUnreadByOwner":false,"DoNotCall":false,"Lead_Count__c":1,"doNotRoundRobin__c":false,"SFGA__CorrelationID_ (2 more) ...":"none","FirstName":"one","Company":"ABC Inc","Free_Trial__c":false,"CurrencyIsoCode":"EUR","Category__c":"Standard Account","Request_Quote__c":false,"Contact_Us__c":false,"HasOptedOutOfEmail":false,"Status":"Open","Email":"test@in.g4s.com","hasDupeInMarketing__ (1 more) ...":false,"IsDeleted":false,"Data_Quality_Descrip (7 more) ...":"Missing: Phone, Salu (34 more) ...","SFGA__Web_Source__c":"none","Existing_Customer__c":false}|0x7f00818b
18:00:45.308 (5308178000)|HEAP_ALLOCATE|[64]|Bytes:5
18:00:45.308 (5308192000)|STATEMENT_EXECUTE|[63]
18:00:45.308 (5308217000)|STATEMENT_EXECUTE|[64]
18:00:45.308 (5308226000)|HEAP_ALLOCATE|[65]|Bytes:18
18:00:45.308 (5308250000)|HEAP_ALLOCATE|[65]|Bytes:18
18:00:45.308 (5308287000)|SYSTEM_METHOD_ENTRY|[65]|Id.compareTo(Id, Boolean)
18:00:45.308 (5308315000)|HEAP_ALLOCATE|[65]|Bytes:8
18:00:45.308 (5308340000)|SYSTEM_METHOD_EXIT|[65]|Id.compareTo(Id, Boolean)
18:00:45.308 (5308358000)|STATEMENT_EXECUTE|[68]
18:00:45.308 (5308366000)|STATEMENT_EXECUTE|[69]
18:00:45.309 (5309176000)|VARIABLE_ASSIGNMENT|[69]|this.OwnerId|"00520000001dH0OAAU"|0x7f00818b
18:00:45.309 (5309195000)|STATEMENT_EXECUTE|[70]
18:00:45.309 (5309994000)|VARIABLE_ASSIGNMENT|[70]|this.doNotRoundRobin__c|true|0x7f00818b
18:00:45.310 (5310020000)|METHOD_EXIT|[18]|01q20000000cH20|DeDupeLead.doDeDupeActions(Id, Id, Lead)

 

In lines ...309 above, the ownerid is being changed to one of our Sales Reps and this seems to be having the knock on effect of changing the doNotRoundRobin__c field from False to True, which would be correct behaviour. It was initially set to false above when the owner id was not one of our sales reps which meant that then it should have been assigned to the round robin queue.

 

I'm now checking our lead assignment rules to see what I can find. Do you think this might be the problem?

 

Thanks again for all your help on this. I'm not long in this Salesforce admin role(couple of months) and the senior developer who originally implemented this code is on maternity leave - hence my predicament.

 

Your help is very much appreciated.

sbbsbb

As you discovered, the DeDupeLead is setting doNotRoundRobin__c to True. Whoever coded it probably intended it to be so. If that is the case, you just need to fix your test code to use something else (something you are expecting to happen) in those assert statements. 

 

The reason you want to use asserts is that you are expecting your code to do something with the test data you are feeding it, and you know what the result is going to be. The 'Asserts' tell you if your code behaved as you expected or not.

 

Hope that was helpful.

This was selected as the best answer
StradaStrada

Hi sbb,

 

Thank you so much for pointing me in the right direction. I really appreciate you giving your time to helping me with this issue. I have now been able to resolve this - I just needed to change my first assert statement to assert for a value of true and my second assert statement to assert for a false value.

 

This has now resolved my issue so thanks again for the help sbb!

 

Kind Regards,

 

Strada.