1

I created formula field and is automatically populated the value and its working properly,please find the below formula field code

IF(
  ISBLANK(Community__c),
  Name,
  Community__r.Name & " > " & Name
)

Above code working properly while creating the record manually and the formula field is automatically created but the issue with Community__r (lookup relation) while mapping value to the relation field I'm facing the field is not writable issue. Please find the below code

 Community__c domain = new Community__c();                          
  domain.External_Id__c= communityid;
  **domain.Community__r= new Community__c(External_Id__c=parentid);**
  datadomainrelationList.add(domain);                              

I'm facing this issue only for relation field ( Community__r) but while mapping value to the Name field I didn't faced any issue. Please find the below issue ,I'm facing this issue

 I'm facing this issue

Please find below code image as well,I tried with these two ways but the issue is same

I tried with these two ways but the issue is same

Can anyone please help me on this issue ?

2
  • 1
    I know it's a stupid question. But can you confirm that there is a lookup field in the Community__c object, that it is called Community__c and it is pointing to itself? Mar 25, 2020 at 18:16
  • yes, it is a self lookup relation. Mar 26, 2020 at 3:33

2 Answers 2

3

I believe the SObject class has a function called putSobject(..) for this exact purpose. Instead of directly assigning the new instance of Community to the Community__r, try the following:

domain.putSObject('Community__r', new Community__c(Extenal_Id__c='xyz'));

SObjects are not simply Map of strings to objects or flat classes and require use of the above interface function to set an Object value, unlike primitives where you can just assign them with = operator.

Also, the first argument can be of type Schema.SObjectType which makes it cleaner.

1

Having the same issue, but it works on sandbox and breaks in production..

I'm running the following code snippet:

Account acc = new Account();
acc.Custom_account_relationship__r = new Account(Some_external_Id__c = '999999');

In any sandbox it runs ok, in productions throws an error.

But if I use a standard relationship like ParentId, it works ok in all environments:

Account acc = new Account();
acc.Parent = new Account(Some_external_Id__c = '999999');

EDIT: FIXED.

The problem was that there is another lookup relationship in Account that has as the secondary relationship name the same value as the lookup that I was trying to relate through External ID.

So when I accessed the Custom_account_relationship__r field Salesforce though that I was trying to fill the childs records for the Account.

You must log in to answer this question.

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