views:

45

answers:

1

Hi there, 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 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 a 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

+2  A: 

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

Change your social_account.Lead__c.id = existing_lead.Id; into social_account.Lead__c = existing_lead.Id;

Should be all that's needed. Salesforce relations can be

SET by directly modifying the field you have created: Social_Account__c.Lead__c, putting there Id of object you want to point to.

GET (explored) by modifying the field name a bit and using the dot, in your case it's probably Social_Account__c.Lead__r.(whatever fields on Lead you want).

The "_c" and "_r" are for custom objects, for standard ones for example there is Opportunity.AccountId field for setting but if you want to explore up to Account you type Opportunity.Account.Name.


If you'll have trouble remembering it - don't worry, me too ;) Usually when I get such compilation error I try it with query builder (in Apex Explorer or the Eclipse plugin). I click through hierarchy on the object, it's children, it's parents etc and usually can use pieces of generated query directly in my Apex code.

eyescream
thank you so much for the comment. it works nicely there.I don't get a foreign key error anymore.but when I do this: Lead existing_lead = new Lead(Id = leads[0].id); social_account.Lead__c = existing_lead.Id; insert social_account; I get a following error message: (runtime error when I send an email and try to insert a social_account)554 System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [Lead__c] do you know why?I would appreciate any suggestions.Naoya
Naoya Makino
You could cut your code to just `social_account.Lead__c = leads[0].id`, that's for sure. As for the error: you have converted "this" Lead record into Account, Contact, Opportunity or something like that, no further actions with it are possible (check value of the boolean field Lead.isConverted?). You can't update and delete such records, they're just for reporting... apparently can't link to them as well... Maybe modify your code to filter on this field? Or add more lookup fields (to Contact?) on your Social_Account__c and fill them instead?
eyescream

related questions