1

hello i have written below batch class but when its running its giving an error : System.StringException: Invalid id: u.id at line number 16

global class batchleadUpdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {

    User u = [select Id from User where Profile.Name='System Administrator' Limit 1];

        String query = 'SELECT Id,Name,Status ,OwnerId  FROM Lead WHERE Status != \'Closed - Converted\' AND CreatedDate < LAST_N_DAYS:30';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
         for(Lead le : scope)
         {
             le.OwnerId = 'u.Id'  ;            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

can any one help me by telling what i did wrong . will be of great help Thanks all

i fixed the code now but my test class is covering only 44% can any one help me with this

@isTest(SeeAllData=true)
  private class batchleadUpdate_Test { 

static testMethod void batchleadUpdate_TestMethod (){
     Profile prof = [select id from profile where name='system Administrator'];
     User usr = new User(alias = 'usr', email='us.name@vmail.com',
                emailencodingkey='UTF-8', lastname='lstname',
                timezonesidkey='America/Los_Angeles',
                languagelocalekey='en_US',
                localesidkey='en_US', profileid = prof.Id,
                username='testuser128@testorg.com');
                insert usr;

          Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Open');
insert lead;  
Lead ledRec1 = [SELECT Id,Name,Status,OwnerId FROM Lead WHERE Status != 'Closed - Converted' limit 1 ];

   ledRec1.OwnerId = usr.Id;
   update ledRec1 ;
   Test.StartTest();

   batchleadUpdate objBatch = new batchleadUpdate();
   ID batchprocessid = Database.executeBatch(objBatch);
   Test.StopTest();
  }
}
5
  • 'u.Id' is a string, needs to be an Id - also, how are you hoping that u will be in scope in your execute method when it looks like you've declared it in your start method? – Phil Hawthorn Oct 1 '14 at 16:27
  • i am new to apex coding can you help me with this . i wanted to update le.ownerid with users id Thanks in advance – user9982 Oct 1 '14 at 16:37
  • are you familiar with variable scope though? You've declared a variable in one method and then tried to use it (albeit between single quotes) in another. Your query to User would probably better suited in your execute method so that your statement le.OwnerId = u.Id can access it. – Phil Hawthorn Oct 1 '14 at 16:50
  • you want to update all the Leads.ownerId in scope with the userId of some Sysad? That is what the code/comment suggests. Please update your post to state your business problem more precisely – cropredy Oct 1 '14 at 18:11
  • Yes user should be in Execute method . its working now. and in my code i am updating the owner id of leads which are created before 30 days and status is Not Closed - Converted . – user9982 Oct 1 '14 at 18:29
2

The error you are seeing is because of the line le.OwnerId = 'u.Id';. You are trying to save a String to an OwnerId field. You are also querying the User field in the start method rather than the execute method. Have a look at the code below

global class batchleadUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name,Status ,OwnerId  FROM Lead WHERE Status != \'Closed - Converted\' AND CreatedDate < LAST_N_DAYS:30';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Lead> scope) { 
         User u = [select Id from User where Profile.Name = 'System Administrator' Limit :1];
         for(Lead le : scope) {
             le.OwnerId = u.Id;            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC) {}
}
5
  • Thanks Barcotter , i made the changes already and its working – user9982 Oct 1 '14 at 19:32
  • Bu ti am having problem with the test class . i am getting only 44% code coverage can you see and help if what could be the problem? – user9982 Oct 1 '14 at 19:33
  • Hi, if you create a new question with the batch class and also the test class Im sure we could help. – BarCotter Oct 1 '14 at 19:37
  • ok i will do that – user9982 Oct 1 '14 at 19:38
  • Hello Barcotter, below is the link for my question salesforce.stackexchange.com/questions/51197/… – user9982 Oct 1 '14 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.