You need to sign in to do that
Don't have an account?
Why are these pesky Inner Join Fields unavailable? Compile Error: Variable does not exist: Replaced_By_ID__c at line 14 column 25
Hello All,
I am trying to write Batch APEX Class to Join Lead with User.
I have added fields to the User object to hold the ID of the who the user was replaced by (Replaced_By_ID__c).
The scenario is an AE (User) leaves the company and is replaced by a new AE (User).
In my APEX Class I want to find the Leads that were owned by the departed AE (User) by joining to User where User.Replaced_By_ID__c <> '' and User.Accounts_Traansferred__c = FALSE.
If I find a match I want to update Lead.OwnerID with User.Replaced_By_ID__c.
Unfortunately, User.Replaced_By_ID__c and User.Accounts_Traansferred__c are unavailble.
Compile Error: Variable does not exist: Replaced_By_ID__c at line 14 column 25
Can someone help me? Please.
Here is my dumb code.
global class LeadTransferTEST04 implements Database.Batchable<sObject>{
global integer recordsProcessed = 1;
string NoValue = ' ';
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator('SELECT ID, OwnerID, Replaced_By_ID__c from Lead where OwnerID IN (SELECT ID FROM User WHERE Accounts_Transferred__c = FALSE and Replaced_By_ID__c <> NoValue)');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l : scope){
l.OwnerID = Replaced_By_ID__c;
}
update scope;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
// EmailUtils.sendMessage(a, recordsProcessed);
// Send an email to the Apex job's submitter notifying of job completion.
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// String[] toAddresses = new String[] {CreatedBy.Email};
// mail.setToAddresses(toAddresses);
// mail.setSubject('Apex Lead Transfer Job ' + a.Status);
// mail.setPlainTextBody
// ('Apex Lead Transfer Job has been completed. There were total of ' + a.TotalJobItems
// + ' batches with ' + successCounter + ' success and ' + failureCounter
// + ' failures.');
// Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
l.OwnerID = l.Replaced_By_ID__c;
Hi Raj,
Thank you for your reply...I tried your suggestion with no success so I tweaked my code.
I think my issue is now trying to move a value to an "ID" field. Oddly, a text value is accepted?
global class LeadTransferTEST05 implements Database.Batchable<sObject>{
global integer recordsProcessed = 1;
string NoValue = ' ';
global Database.QueryLocator start(Database.BatchableContext bc){
// return Database.getQueryLocator('SELECT ID, OwnerID, Replaced_By_ID__c from Lead where OwnerID IN (SELECT ID FROM User WHERE Accounts_Transferred__c = FALSE and Replaced_By_ID__c <> NoValue)');
return Database.getQueryLocator('SELECT Lead.ID, Lead.OwnerID, User.Replaced_By_ID__c FROM Lead INNER JOIN User ON Lead.OwnerID = User.ID WHERE Accounts_Transferred__c = FALSE and Replaced_By_ID__c <> NoValue');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l : scope){
//l.OwnerID = l.Replaced_By_ID__c; Error: Compile Error: Variable does not exist: Replaced_By_ID__c at line 14 col 27
//l.OwnerID = User.Replaced_By_ID__c; Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 13 col 13
l.OwnerID = '005L0000004P785';
}
update scope;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
// EmailUtils.sendMessage(a, recordsProcessed);
// Send an email to the Apex job's submitter notifying of job completion.
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// String[] toAddresses = new String[] {CreatedBy.Email};
// mail.setToAddresses(toAddresses);
// mail.setSubject('Apex Lead Transfer Job ' + a.Status);
// mail.setPlainTextBody
// ('Apex Lead Transfer Job has been completed. There were total of ' + a.TotalJobItems
// + ' batches with ' + successCounter + ' success and ' + failureCounter
// + ' failures.');
// Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
then try like below
The "Lookup Relationship" Data Type is not available on our User Object?
There are already 7 LookUp fields defined on our User Object.
My understanding is that it is not possible to create "Look Up" fields on the User object.
I found that I can move a string to Lead.OwnerID and update the Lead Object.
I am going to build a string from Replaced_By_ID__c and see if that works.
For those following at home...here is the latest edition from my adventures in APEX.
I am still unable to get User.Replaced_By_ID__c and its driving $^*%@ nuts!!!
I want to update Lead.OwnerID with User.Replaced_By_ID__c.
I found that I can update Lead.OwnerID with a string value...for some reason it allows me to get aorund the "Illegal assignment from Schema.SObjectField to Id" error.
global class LeadTransferTEST04 implements Database.Batchable<sObject>{
global integer recordsProcessed = 1;
string NoValue = ' ';
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator('SELECT ID, OwnerID, (SELECT Replaced_By_ID__c FROM User WHERE Accounts_Transferred__c = FALSE and Replaced_By_ID__c <> :NoValue) from Lead where OwnerID IN (SELECT ID FROM User WHERE Accounts_Transferred__c = FALSE and Replaced_By_ID__c <> \'\')');
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
for(Lead l : scope){
l.OwnerID = string.valueOf(User.Replaced_By_ID__c);
}
update scope;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
}
}