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
Terry411Terry411 

Updating code in the cloud

I've ran into this problem before which logically doesn't make sense to me.  It seems that when I make a change to an existing piece of code, force.com doesn't test my updated local code but instead uses the code on the server.  This creates a real problem when the server code throws an exception error or doesn't pass your test.assertEqual logic.

 

In my current situation, I have a class that changes the ownerid of a lead.  The problem with the server code is that I didn't account for converted leads so it's attempting to update converted records which isn't allowed.  I've corrected the code locally but can not get it pushed up to the server as the testing process continues to read only the server code (which has the error):

 

System.DmlException: Update failed. First exception on row 0 with id 00Q6000000TKaxSEAT; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead:

 

The server code uses this query:

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId]) {

 

My currected local code uses this query:

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.ConvertedDate <> null]) {

 

My question is how do I get my code corrections to move to the server?  I've attempted "Deploy to server" as well as a simple "Save to Server" and either way the new code is not upload and the test logs show it's not being used.  I'm assuming I'm missing something but I don't know what that is.  Please help.

 

 

Here's a copy of the entire class:

public class clsLeadOwnerChange {
	public void LeadToUpdate(){
		List<Lead> LeadToUpdate = new List<Lead>();

		// Set Today's Date
		Date dExpDate = date.today();
		
		// Query for the Lead Queue 
		Id NewOwnerId = [Select q.QueueId from QueueSobject q where q.SobjectType = 'Lead'].QueueId ;
		
		// Query for Leads which have an Owner_Expiration_Date__c <= today (the less than adds a measure of security in case the scheduled class didn't run)
		for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.ConvertedDate <> null]) {
		
			// Loop through UpdateCon
			for (Lead l : UpdateLead) {
				// Change the OwnerID
				Lead lUpd = New Lead(ID=l.Id, OwnerId=NewOwnerId);
				LeadToUpdate.add(lUpd);
			}
		}
		update LeadToUpdate;
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Terry411Terry411

I found the problem.  I changed the soql from

 

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.ConvertedDate <> null]) {

 

to

 

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.IsConverted = False]) {

 

Note the change was from l.ConvertedDate <> Null to l.IsConverted = False.  Why that made a difference, I have no idea but it uploaded into production as soon as I saved the change.  Thank you everyone for your help.

All Answers

granigrani

Make sure you update the corresponding test class. Run test on the test class to make sure it is not failing. Then deploy both together.

Terry411Terry411

Running the test class fails when it runs the class becuase it's not running the local code with the correction.  It's continues to run the old code on the server.  As I walk through the debug log, it's not using the new code.  You can see from the debug log entries below that the SOQL statement is still the original query.  I am correct that it should (in theory) use the local code when I run the test, right?

 

Deploying both the new code and the test class together still fails.

 

21:27:35.388|METHOD_ENTRY|[1]|01p600000008llC|clsLeadOwnerChange.clsLeadOwnerChange()
21:27:35.388|METHOD_EXIT|[1]|clsLeadOwnerChange
21:27:35.388|CONSTRUCTOR_ENTRY|[65]|01p600000008llC|<init>()
21:27:35.388|CONSTRUCTOR_EXIT|[65]|<init>()
21:27:35.388|METHOD_ENTRY|[66]|01p600000008llC|clsLeadOwnerChange.LeadToUpdate()
21:27:35.388|METHOD_ENTRY|[6]|Date.today()
21:27:35.388|METHOD_EXIT|[6]|Date.today()
21:27:35.388|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|Select q.QueueId from QueueSobject q where q.SobjectType = 'Lead'
21:27:35.391|SOQL_EXECUTE_END|[9]|Rows:1
21:27:35.391|SOQL_EXECUTE_BEGIN|[12]|Aggregations:0|Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId
21:27:35.400|SOQL_EXECUTE_END|[12]|Rows:10

 

dmchengdmcheng

You're using the Eclipse IDE, right?  Are you saying that IDE project is connected to your Sandbox, and if you make a change in your code and save it, the change doesn't get saved up into the Sandbox?  What happens if you create a new project linked to the same sandbox and make a change in there?

Terry411Terry411

Sorry for the delay...  Yes, I am using the force.com Eclipse IDE howver I was doing so in the production environment.  So I setup the test sandbox and all uploads and works fine there.  I created a Change set to move the new code into production and recieve the error: 

 

Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 00Q6000000TKaxSEAT; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []",

 

This is consistent with the error I received attempting to upload directly into production.

 

I also created a new project and had no luck with that either.

 

I'm questioning the idea of deleting the cls from the server and uploading the corrected version.  If it works that solves the immediate problem but it will not explain the root cause.  Any other ideas?

dmchengdmcheng

I have not used Change sets yet - I always deploy from the IDE.  It sounds like your test code is failing during the deployment but it's hard to say since there's no log or line number for the failed code line.  I would say clean out your production environment and do all your development only in the sandbox.

Terry411Terry411

I found the problem.  I changed the soql from

 

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.ConvertedDate <> null]) {

 

to

 

for (Lead[] UpdateLead : [Select l.Id, l.OwnerId from Lead l where l.Owner_Expiration_Date__c <= :dExpDate and l.OwnerId <> :NewOwnerId and l.IsConverted = False]) {

 

Note the change was from l.ConvertedDate <> Null to l.IsConverted = False.  Why that made a difference, I have no idea but it uploaded into production as soon as I saved the change.  Thank you everyone for your help.

This was selected as the best answer