1

I have the following snippet of code:

    List<ID> AccountIDx = new List<ID>();
List<ID> ContactIDx = new List<ID>();
List<ID> OppIDx = new List<ID>();

AccountIDx.add(AssetList.get(0).AccountId); 
ContactIDx.add(AssetList.get(0).ContactId);
OppIDx.add(AssetList.get(0).Opportunity_Name__c);



List <Account> AccountList = [SELECT ID
                                    ,NAME
                                    ,(SELECT ID
                                            ,Name 
                                            FROM Contacts 
                                                WHERE ID IN: ContactIDx),
                                            (SELECT ID
                                                    ,NAME
                                                    ,SUPPORT_ONLY__C
                                                    ,CLOSEDATE
                                                    ,ADMIN_NEXT_STEP__C
                                                    ,NEXTSTEP
                                                    ,Opportunity_Discount_program__c
                                                    ,MAIN_MARKET__c
                                                    ,End_User__c
                                                    ,End_User_Contact__c
                                                    ,OwnerId,Opportunity_Contact__c 
                                                    FROM Opportunities 
                                                        WHERE ID IN: OppIDx)
                                    FROM ACCOUNT 
                                        WHERE ID IN: AccountIDx ];

//NEW OPP CREATED FROM OLD OPPORTUNITY WITH RELEVANT FIELDS!!!
newOpp = new Opportunity(Name=AccountList.get(0).Opportunities.Name)+' - '+ 'Renewal',Description=(AccountList.get(0).Opportunities.Name)+' - '+ 'Renewal',OwnerId=AccountList.get(0).Opportunities.OwnerId,Support_Only__c=true,CloseDate=mydate.addDays(30),Admin_Next_Step__c='Send Quote',NextStep='Send Quote',AccountId=AccountList.get(0).Opportunities.Id,End_User__c=AccountList.get(0).Opportunities.End_User__c,Opportunity_Contact__c=AccountList.get(0).Opportunities.Opportunity_Contact__c,End_User_Contact__c=AccountList.get(0).Account.End_UserContact__c,Opportunity_Discount_program__c=' ',Type='Partner Sell Thru', Configuration__c='No Special Configuration Needed',Main_Market__C=AccountList.get(0).Opportunties.Main_Market__c);

insert newOpp;

The error that I'm getting is Invalid Foreign key relationship: Account.Opportunities.

So the error is occurring when I try to set the Name as Name=AccountList.get(0).Opportunities.Name)+' - '+ 'Renewal'. I'm sure that the same error will occur for the rest of the fields that I'm inserting with the Opportunity newOpp.

What is the correct way to create a relationship between the two?

1 Answer 1

3

Opportunities is an Opportunity[], not an Opportunity. You can use:

AccountList[0].Opportunities[0].Name;
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .