So essentially I'm trying to write simple script that, on record creation, will cause users referenced by the record's lookup fields to auto-follow the record in question. To this end, I'm creating a list of the User ID's referenced in the object, and then comparing those ID's to the SubscriberID's of the existing associated EntitySubscription objects. If an EntitySubscription object for a given ID does not exist, one is created.
Now, I am getting the System.StringException: Invalid id: N/A
Error in my test method, at jobPage.AutoProcess(). All of the fields being referenced are lookup fields to Users. I am checking for null and N/A values before trying to push the users into the lest. I believe the error is occurring when I am trying to push an invalid ID into the List jobTeam, but I cannot see where this invalid ID is coming from.
Code is below- public class newJobController { private List jobTeam;
public newJobController(ApexPages.StandardController stdController){
j = (Job__c)stdController.getRecord();
}
public integer AutoFollowClass()
{
try
{
List<EntitySubscription> followersList = new List<EntitySubscription>();
followersList = [SELECT subscriberID, ID FROM entitysubscription WHERE parentID = :j.id ];
Map<Id, EntitySubscription> followersMap = new Map<Id, EntitySubscription>();
for (EntitySubscription f : followersList)
{
followersMap.put(f.subscriberID, f);
}
for(ID i :jobTeam)
{
if( !followersMap.containsKey(i))
{
EntitySubscription e = new EntitySubscription();
e.SubscriberId =i;
e.parentID = j.id;
insert e;
}
}
return 0;
}
catch(Exception e)
{
return 1;
}
}
AutoProcess()
{
jobTeam = new List<Id>();
/*
//not working reading ID as N/A?
if(j.Associate_Creative_Director__c != null && j.Associate_Creative_Director__c != 'N/A')
{
jobTeam.add(j.Associate_Creative_Director__c);
}
//Also reading ID as N/A
if(j.Digital_Creative_Director__c != null && j.Digital_Creative_Director__c != 'N/A')
{
jobTeam.add(j.Digital_Creative_Director__c);
}
//Also passing in as N/A
if(j.Internal_Operations__c != null && j.Internal_Operations__c != 'N/A')
{
jobTeam.add(j.Internal_Operations__c);
}
//job id = N/A error
if(j.iMedia_Coordinator__c != null && j.iMedia_Coordinator__c != 'N/A')
{
jobTeam.add(j.iMedia_Coordinator__c);
}
*/
if(j.Account__c != null && j.Account__c != 'N/A')
{
jobTeam.add(j.Account__c);
}
if(j.Digital_Producer__c != null && j.Digital_Producer__c != 'N/A')
{
jobTeam.add(j.Digital_Producer__c);
}
if(j.Executive_Creative_Director__c != null && j.Executive_Creative_Director__c != 'N/A')
{
jobTeam.add(j.Executive_Creative_Director__c);
}
if(j.Creative_Director__c != null && j.Creative_Director__c != 'N/A')
{
jobTeam.add(j.Creative_Director__c);
}
if(j.Co_Creative_Director__c != null && j.Co_Creative_Director__c != 'N/A')
{
jobTeam.add(j.Co_Creative_Director__c);
}
if(j.Copywriter__c != null && j.Copywriter__c != 'N/A')
{
jobTeam.add(j.Copywriter__c);
}
if(j.Art_Director__c != null && j.Art_Director__c != 'N/A')
{
jobTeam.add(j.Art_Director__c);
}
if(j.Designer__c != null && j.Designer__c != 'N/A')
{
jobTeam.add(j.Designer__c);
}
if(j.Producer__c != null && j.Producer__c != 'N/A')
{
jobTeam.add(j.Producer__c);
}
if(j.Production__c != null && j.Production__c != 'N/A')
{
jobTeam.add(j.Production__c);
}
if(j.Media_Lead__c != null && j.Media_Lead__c != 'N/A')
{
jobTeam.add(j.Media_Lead__c);
}
if(j.iMediaLead__c != null && j.iMediaLead__c != 'N/A')
{
jobTeam.add(j.iMediaLead__c);
}
integer i;
i = AutoFollowClass();
if(i == 0)
{
PageReference jobPage = new ApexPages.StandardController(j).view();
jobPage.setRedirect(true);
return jobPage;
}
else
{
PageReference jobPage = new PageReference('/apex/UhOh?id=' + j.id);
jobPage.setRedirect(true);
return jobPage;
}
}
public static testMethod void AutoFollowControllerTest()
{
Job__c job = new Job__c();
job.name='test123';
job.type__c='interactive';
job.livedate__c=date.today();
Profile p = [select id from profile where name='Saatchi - Standard User'];
User testUser3 = new User(alias = 'u1', email='rtu11111' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com',
emailencodingkey='UTF-8', lastname='Testing3', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='rtu11111' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com', division = 'NYC', isActive=true);
insert testUser3;
User testUser2 = new User(alias = 'u2', email='rtu11112' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com',
emailencodingkey='UTF-8', lastname='Testing2', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='rtu11112' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com', division = 'NYC', isActive=true);
insert testUser2;
User testUser1 = new User(alias = 'u3', email='rtu11113' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='rtu11113' + datetime.now().format('MMMMM.dd.yyyy.hh.mm.ss') + '@testorg.com', division = 'NYC', isActive=true);
insert testUser1;
job.iMediaLead__c = testUser3.Id;
job.Designer__c = testUser2.Id;
job.Copywriter__c = testUser1.Id;
PageReference jobPage = new PageReference('null');
System.assertNotEquals(control.save(), null);
jobPage = control.autoProcess);
List<EntitySubscription> entities = new List<EntitySubscription>();
entities = [SELECT subscriberID, ID FROM entitysubscription WHERE parentID = :job.id ];
System.assert(!entities.isEmpty());
}
}