Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Salesforce by (11.9k points)

I have a Lead and a custom object called Social Account (API Name = Social_Account__c).

I have set a relationship as follows: Lead is a parent of Social Accounts. So a lead has many social accounts.

In the Social Account, I made a custom field named Lead (Data Type: Lookup) to make the relationship.

and here is a lookup detail:

API Name: Lead__c

Related to Lead

Child Relationship Name: Social_Accounts

Related List Label: Social Accounts

I would like to add new social accounts to existing lead if there is a lead with the same email address.

Social_Account__c social_account = new Social_Account__c();

/*add whatever fields on social_account*/

List<Lead> leads =[select Id from Lead where Email =:emailAddress ];

if(leads.size()>0)

    Lead existing_lead = new Lead(Id = leads[0].id);

    //ideally i would like to do something like this

    social_account.Lead__c.id = existing_lead.id; //this is where I get an error from

    insert social_account;

    update existing_lead;

}

But I get the following error message:

Error: Compile Error: Invalid foreign key relationship: Social_Account_c.Lead_c

what am I doing wrong? I would appreciate any suggestions.

thanks

1 Answer

0 votes
by (32.1k points)

You can't "go through relation" with dot (.) operator with updates, with just reading data.

Change social_account.Lead__c.id = existing_lead.Id; to social_account.Lead__c = existing_lead.Id;

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.7k users

Browse Categories

...