3

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());



}
}
2
  • Where do you insert job? You refer to its ID in the query but I don't see you insert it.
    – Doug B
    Sep 25 '13 at 15:04
  • I left that snippet out, just added it for clarity though
    – gfppaste
    Sep 25 '13 at 16:08
5

Assuming that Associate_Creative_Director__c is a lookup field in this statement:

if(j.Associate_Creative_Director__c != null && j.Associate_Creative_Director__c != 'N/A')

Then you are trying to compare an ID with the String 'N/A'. To do the comparison the system automatically converts each side to the same type, and in this instance it's trying to convert 'N/A' into an ID which is failing for obvious reasons.

You want to select the name of the related record using Associate_Creative_Director__r.Name and then if the ID is null do the comparison against 'N/A' using the name field and not the ID. That will be a String/String comparison and so won't throw an exception.

5

An ID isn't ever N/A. It will always be one of null, a 15 character ID, or in most cases, an 18-character ID. If you're checking to see if the ID refers to a record whose name is N/A, you'll need to query that data separately. Most likely, you can just change the query to include, for example, Associate_Creative_Director__c, Associate_Creative_Director__r.Name, then check Associate_Creative_Director__r.Name != 'N/A'.

3
  • I agree with the ID field never being N/A. Since I'm not referencing the associated object other than to grab it's ID string (e.g. object.User_Lookup__c), I can't see the issue with what I'm doing. I figure that if an ID field does not exist, it will have a null value. Additionally, I figure that there cannot be an ID which references a null object, or an N/A object, correct?
    – gfppaste
    Sep 25 '13 at 19:20
  • Sidenote- tried your suggestion, no dice =/ this is really perplexing me!
    – gfppaste
    Sep 25 '13 at 19:20
  • There is a special ID that sometimes appears: 000000000000000AAA, which is the same as a null ID. However, you should never see this value in Apex Code; it commonly appears in Weekly/Monthly export feature, though.
    – sfdcfox
    Oct 1 '13 at 20:17
2

I think it might be the If statements themselves that are causing the exception. I can recreate the StringException like this:

Contact con = [select Id from Contact limit 1];
if (con.Id == 'N/A') system.debug('id is n/a');

which throws this:

17:49:04.053 (53023000)|FATAL_ERROR|System.StringException: Invalid id: N/A
AnonymousBlock: line 2, column 1

It seems to me like Apex is seeing that your comparison is on an Id/Reference type field and trying to cast the thing being compared against into an Id. If you remove all your 'N/A' references, do you still get the error?

0

Like sfdcfox said, a Lookup (Id) is either empty or a valid id.

Why wouldn't you just use String.isNotBlank(String id)? It checks for != null and for length > 0.

-1

You can also use String.ValueOf() on the id field to get around this error.

1
  • 2
    Although that wouldn't be very helpful since String.valueOf(<id>) != 'N/A' is always true.
    – martin
    Mar 22 '17 at 13:17

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.