3

Created a VF page to create Opportunity. While saving the Opportunity on Page it is throwing exception on Account lookup Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

how to resolve this issue ? can anyone please help me

Here is my class:

public class oppcreate
{

    public Opportunity opp{get; set;}
    public oppcreate ()
    {
        opp= new opportunity();

    }
    public void Saveto(){

        opportunity oppRec= new opportunity();

        oppRec.AccountId= opp.AccountID; // throwing exception on this line

        oppRec.name= opp.name;

        oppRec.closedate= opp.closedate;
        insert oppRec;
    }
}

VF Page:

 <apex:page Controller="oppcreate">
    <apex:form >
    <apex:pageBlock title="Opportunity Edit" mode="edit">
    <apex:pageBlockButtons >
    <apex:commandButton value="Save" action="{!Saveto}"/>
    </apex:pageBlockButtons>

    <apex:pageBlockSection  title="Opportunity Information">

    <apex:inputField value="{!opp.AccountId}"/>
    <apex:inputField value="{!opp.name}"/>
    <apex:inputField value="{!opp.Closedate}"/>

    </apex:PageBlockSection>
    </apex:pageBlock>

    </apex:form>

    </apex:page>
  • Are you passing an AccountId to the controller? You need to force some kind of validation on the page to ensure that the user selects an account. – abhi Jan 26 '17 at 16:05
0

First of all, there are some useless code in your controller.

You don't need to instanciate a new opportunity in the save method. The opportunity variable which is shared with your visualforce page have already all the values assigned.

You only have to do this:

public void Saveto(){
        insert opp;
    }

About your error, it seems that you never assigned an account to the opportunity using the VF page. Just add some check before inserting your opportunity:

public void Saveto(){
            if(opp.AccountId != null && opp.Name != null){
                insert opp;
            }
        }
  • Thanks for your response. I implemented your logic but it is giving same exception – Learner Jan 26 '17 at 16:17
  • Are you selecting an Account in your VF page before clicking on the save button? – Martin Lezer Jan 26 '17 at 16:19
  • Yes I am selecting the account – Learner Jan 26 '17 at 16:19
  • Sorry the error was with accounts not with the code. Thanks for your response – Learner Jan 26 '17 at 20:17
0

The field AccountId from Opportunity refears to an Account object.

So you need to set this field with an Account ID like this :

Account acc = [SELECT Id, Name FROM Account WHERE Name = 'XXX' LIMIT 1]; // Replace XXX by the Account name you want to retrieve

Opportunity oppRec = new Opportunity();

oppRec.AccountId= acc.Id;

insert oppRec;

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunity.htm

  • Thanks for your response. I want to select the Accounts which already in the system. I don't want to create any new account. – Learner Jan 26 '17 at 16:19
  • I edited my answer ! – SF_user Jan 26 '17 at 16:23
  • Sorry if I miss communicated .. I want to show all the accounts in the lookup (it should work as standard one), not only the specific one. – Learner Jan 26 '17 at 16:28

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.