0

I am trying to bulkify a callout where I want to pass the recordtype in my loop but I get:

Error: Variable does not exist: RecordType

If I try public RecordType personAccountRecordType I get public not allowed on locals

Below is my code

 RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'][0].RecordType;

            List<Account> persAccount  = new List<Account>();

            for(integer x=0; x<guestsClass.size();x++){
            Account acc = new Account(lastname=guestsClass[0].lastName, RecordType = personAccountRecordType);
            persAccount.add(acc);
            }
            insert persAccount;

Update:

          Http http = new Http();
        HttpRequest request = new HttpRequest();
        //Catch is not needed since soql's cant be empty in this case 
        os = [SELECT Url__c, ApiKey__c, agencyUid__c FROM Orbirental_Service__c LIMIT 1]; 

        request.setEndpoint(os[0].Url__c +'v1/guests?agencyUid='+os[0].agencyUid__c);
        request.setMethod('GET');
        request.setHeader('X-ORBIRENTAL-APIKEY', os[0].ApiKey__c);
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
                    System.debug(response.getBody());
            List<or_guestsJSON> guestsClass = or_guestsJSON.parse(response.getBody());
            System.debug(guestsClass);
            System.debug('guestsClass[0].lastName '+guestsClass[0].lastName);
            Id personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'][0].Id;
            List<Account> persAccount  = new List<Account>();
            for(integer x=0; x<guestsClass.size();x++){
            Account acc = new Account(name=guestsClass[0].lastName, RecordTypeId = personAccountRecordType);
            persAccount.add(acc);
            }
            insert persAccount;
            system.debug('persoon '+persAccount);
}

ERROR:INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: 0000000000000: [RecordTypeId]

1 Answer 1

2

Couple of issues here on your current code:

RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'][0].RecordType;

Account acc = new Account(lastname=guestsClass[0].lastName, RecordType = personAccountRecordType);
  1. RecordType is not a Type for variable declaration
  2. [0].RecordType -- there's no column named RecordType on the Object itself.
  3. While assigning a record type for Account, you need to use RecordTypeId

Your code for retrieving the record type and assigning it to Account should look like as below:

// get the Record Type's id from here
Id personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'][0].Id;

// assign it to Account. 
// notice the field name here for record type assignment, it is RecordTypeId
Account acc = new Account(
    lastname=guestsClass[0].lastName, 
    RecordTypeId = personAccountRecordType); 
7
  • I get the same error in my loop missing ID. The purpose is to add multiple accounts to a list with the same recordtype person account.
    – Thomas
    Commented Oct 15, 2018 at 21:02
  • It's tough to understand without looking at it.
    – Jayant Das
    Commented Oct 15, 2018 at 21:15
  • I added an update to clarify
    – Thomas
    Commented Oct 15, 2018 at 21:17
  • What is the error you are getting? It must be unrelated as you don't have the complete code. As for just fetching the record type id and assigning it to account, that will just work.
    – Jayant Das
    Commented Oct 15, 2018 at 21:21
  • I added the complete code and error I run this from anonymouse window.
    – Thomas
    Commented Oct 15, 2018 at 21:28

You must log in to answer this question.

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