0

I have a class to create an User but I'm getting these errors trying to deploy:

=== Deploy Errors PROJECT PATH ERRORS
────────────────────────────────────────────────────────────────── ───────────────────────────────────────────────────── force-app\main\default\classes\OneSFA_SalesStructureHandler_ws.cls Field is not writeable: User.AccountId (446:49)
force-app\main\default\classes\OneSFA_SalesStructureHandler_ws.cls Field is not writeable: User.IsPortalEnabled (446:49)

10:57:47.675 ended SFDX: Deploy Source to Org

This is the code:

private without sharing class AsyncCreateUser implements Queueable {
        
        private String accId;
        private String usrRep;
        private List<Contact> lstContact;
        private List<Sales_Structure__c> lstSSUp;
        
        private asyncCreateUser(String accId, String usrRep, List<Contact> lstContact, List<Sales_Structure__c> lstSSUp){
            this.accId      = accId;
            this.usrRep     = usrRep;
            this.lstContact = lstContact;
            this.lstSSUp    = lstSSUp;
            
            System.debug('Create Contact - accId ' + this.accId);
            System.debug('Create Contact - usrRep ' + this.usrRep);
            System.debug('Create Contact - lstSSUp ' + this.lstSSUp);
        }
        
        public void execute(QueueableContext context){
            if (!Schema.sObjectType.Contact.isUpdateable()) {
                return;
            }
            
           //List<Contact> lstContact = new List<Contact>();
            List<User> lstUser       = new List<User>();
            
            Profile p = [SELECT Id FROM Profile WHERE Name = 'Customer Community Plus Login User'];
            
            for(Sales_Structure__c ss : lstSSUp){    
                for(Contact cct : lstContact){
                    User userXP               = new User(
                    AccountId                 = accId,
                    ContactId                 = cct.Id,
                    IsPortalEnabled           = true,
                    FirstName                 = cct.FirstName,
                    LastName                  = cct.LastName,
                    Alias                     = cct.LastName+ss.SalesRep__c,
                    SAP_Number_of_SalesRep__c = ss.SalesRep__c,
                    Division_Allocation__c    = cct.Department__c,
                    Email                     = cct.Email,
                    UserName                  = cct.Email+ss.SalesRep__c+'.store', 
                    ProfileId                 = p.Id, 
                    EmailEncodingKey          = 'UTF-8',
                    LanguageLocaleKey         = 'en_US',
                    LocalesIdKey              = 'en_US',
                    TimezonesIdKey            = 'America/Los_Angeles'
                    );
                    insert userXP;
                }
            }
        }
    }

How can I handle this error?

1
  • 1
    You can not attempt to write to those fields. Sorry - couldn't resist.
    – Moonpie
    Feb 4, 2022 at 14:39

1 Answer 1

2

This field is read only. You can see the documentation here. You can fill this field indirectly by populating ContactId field with a valid contact record Id. Also, please make sure that that the contact have an associated account. From the developer docs about ContactId on User Sobject, the description states

ID of the Contact associated with this account. The contact must have a value in the AccountId field or an error occurs. This is a relationship field.

You must log in to answer this question.

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