0

I am trying to create an Account sObject so that I can reference at my later code. For the purpose I am running a query on Contacts, and then I pick fields from it and assign them to my Account sObject. The issue is that I am trying to assign a value to an Account field called Salesforce_Account_ID__c which is set "External Auto Number". When I try to assign the value, I get the following error:

Field is not writeable: Account.Salesforce_Account_ID__c

I am aware that the reason for this issue is due to the datatype of the field, but was wondering if there is a workaround without changing the field's datatype.

if(constType == 'IN'){
List<Contact> contactResults = [SELECT Id, accountid, Master_ID__c, account.id, account.Status__c, account.Include_in_Funnel__c, account.Salesforce_Account_ID__c FROM Contact WHERE Master_ID__c IN :masterIdSet];

    for(Contact c:contactResults){

                     Account tmpAcct = new Account(
                         Id = c.AccountId,
                         Status__c = c.Account.Status__c,
                         Include_in_Funnel__c = c.Account.Include_in_Funnel__c,
                         Master_ID__c = c.Master_ID__c,
                         Salesforce_Account_ID__c = c.account.Salesforce_Account_ID__c

                     );
             if(acctMap.containsKey(c.Master_Id__c)){
                 acctMap.get(c.Master_Id__c).add(tmpAcct);
             } else {
                 acctMap.put(c.Master_Id__c, new List<Account>{tmpAcct});
             }
    }
2
  • I think I'm confused. You understand the field is an auto number and therefore cannot be set, but you want to set it anyway. What is it you're hoping to achieve? Commented Oct 11, 2019 at 19:04
  • I am trying to add the Account fields into an sObject from the Contact's query, so that I can add the Account to a map. I updated my code.
    – Ivo Dimov
    Commented Oct 11, 2019 at 19:34

3 Answers 3

1

Just pull the related object from your name pointing reference.

Account tempAccount = c.Account;
0

Do a query to return the account instead:

List<Contact> contactResults = [SELECT  accountid FROM Contact WHERE Master_ID__c IN :masterIdSet];
Map<Id, Contact> accountIdKeyContactValue = new Map<Id, Contact>();
for(contact c : contactResults){
   accountIdKeyContactValue.put(c.accountId, c);
}
Set<Id> accountIDSet = accountIdKeyContactValue.keyset();
    for(account a: [SELECT Id, status__C, etc. etc. FROM Account WHERE id IN accountIdSet] ){
             contact currentContact = accountIdKeyContactValue.get(a.Id);
             if(acctMap.containsKey(currentContact.Master_Id__c)){
                 acctMap.get(currentContact.Master_Id__c).add(a);
             } else {
                 acctMap.put(currentContact.Master_Id__c, new List<Account>{a});
             }
    }

Or something like that

0

I really appreciate the help. I did follow your instructions, but I came to find that there is another a bit cleaner way to do it.

if(constType == 'IN'){

          List<Contact> contactResults = [SELECT Id, accountid, Master_ID__c, account.id, account.Status__c, account.Include_in_Funnel__c, account.Salesforce_Account_ID__c FROM Contact WHERE Master_ID__c IN :masterIdSet];

          for(Contact c:contactResults){   
                 ExistingAccountResult tempResult = new ExistingAccountResult();
                      tempResult.accountid = c.accountid;
                      tempResult.salesforceAccountid = c.account.Salesforce_Account_ID__c;
                      tempResult.status = c.account.Status__c;
                      tempResult.includeinfunnel = c.account.Include_in_Funnel__c;
                      tempResult.masterId = c.Master_ID__c;

                  if(acctMap.containsKey(c.Master_Id__c)){
                         acctMap.get(c.Master_Id__c).add(tempResult);
                  } else {
                  acctMap.put(c.Master_Id__c, new List<ExistingAccountResult>{tempResult});
                  }

                  }

    }   

             public class ExistingAccountResult{
                  public String salesforceaccountid {get;set;}
                  public String status {get;set;}
                  public Boolean includeinfunnel {get;set;}
                  public String masterid {get;set;}
                  public string accountid{get;set;}

              } 

You must log in to answer this question.

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