2

So I'm getting the following erre:

FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

For this code snippet -

public object__c j{get; set;}

public newcustomController(ApexPages.StandardController stdController){
    j = (object__c)stdController.getRecord();        
}

public PageReference save() {
    upsert j;



    EntitySubscription e = new EntitySubscription();
    e.SubscriberId = j.iMedia_Lead__c;
    e.parentID = j.id;
    insert e;

    PageReference jobPage = new ApexPages.StandardController(j).view();
    jobPage.setRedirect(true);
    return jobPage;
}

The idea is that the field iMedia_Lead__c is a user lookup. When someone edits a record and adds a value to the lookup field, the associated user then autofollows the record.

Any ideas what could be causing this error?

EDIT Just ot make sure I was referencing an ID object, I added the following:

public PageReference save() {
    upsert j;


    ID d = j.iMedia_Lead__c;
    EntitySubscription e = new EntitySubscription();
    e.SubscriberId = d;
    e.parentID = j.id;
    insert e;

    PageReference jobPage = new ApexPages.StandardController(j).view();
    jobPage.setRedirect(true);
    return jobPage;
}

Still no dice, but I'm pretty sure I'm passing in the ID value correctly.

EDIT 2 So after some manipulation, I am now getting this error -

Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: duplicates value on record with id: : []

with this code snippet:

public PageReference save() {
    upsert j;

    if(j.iMediaLead__c != null)
    {
        EntitySubscription e = new EntitySubscription();
        e.SubscriberId = j.iMediaLead__c;
        e.parentID = j.id;
        insert e;
    }
}

And the error is occuring at insert e. Any insights into this?

1 Answer 1

1

If iMedia_Lead__c is a lookup, you might be referencing the id incorrectly in the controller. Try this:

e.SubscriberId = j.iMedia_Lead__c.Id;

Update

Assuming all of the field types are correct in the assignments:

e.SubscriberId = j.iMedia_Lead__c;
e.parentID = j.id;

Since the above didn't work, my next steps in debugging would be to use System.Debug to view j.iMedia_Lead__c and j.id to verify they are as expected. After that I would try hard-coding values to verify the assignment works as expected.

4
  • That didn't quite do it... still getting an error on the insert. Could it be that the entitysubscription isn't being assigned an ID somehow?
    – gfppaste
    Sep 18, 2013 at 15:36
  • The object's id is auto-generated so I don't see that being a problem.
    – Mike Chale
    Sep 18, 2013 at 16:38
  • So it looks like I wasn't checking for a null value before trying to pass in an ID, hence the reference error. Now there's a duplicate value error though! Any experience with these?
    – gfppaste
    Sep 19, 2013 at 14:45
  • Is one of the fields configured to be unique?
    – Mike Chale
    Sep 19, 2013 at 15:02

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.