sobject
Relation is not created, as Account Id is not created in Force.com database. I want to omit many inserts in a loop. It's a process with many records so I don't want to meet the limits and write it efficient.public class MyProcessor implements Queueable{
public void execute(QueueableContext context) {
List<Stage_Object__c> counterparties = [SELECT Counterparty_Identifier__c, Full_Name__c, Processed__c
FROM Stage_Object__c
WHERE Processed__c = false LIMIT 3300];
List<Account> accList = new List<Account>();
List<Marker__c> markerList = new List<Marker__c>();
for (Stage_Object__c so : counterparties){
Account na = new Account(Name = so.Full_Name__c);
accList.add(na);
Marker__c m = new Marker__c(Account_ref__c = na.Id,
Identifier__c = so.Counterparty_Identifier__c);
m.Account_ref__c = na.Id;
markerList.add(m);
so.Processed__c = true;
}
try {
insert accList;
insert markerList;
update counterparties;
} catch (DMLException de){
//handling Exception
}
}
}
I tried even something like that:
Account na = new Account(Name = 'Salesforce');
Marker__c m = new Marker__c(Counterparty_Identifier__c = 'some test id',
Account_ref__r = na);
insert m;
ResultError: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: []
or
Marker__c m = new Marker__c(Identifier__c = 'some test id');
Account na = new Account(Name = 'Hollywood Inc.');
na.Marker_r = m;
insert na;
ResultError:
Field is not writeable: Account.Marker_rI tried also add them in one List<SObject> and insert in one transaction (two chunks), but reference also wasn't added.Thanks for your ideas and thoughts,DJpublic class MyProcessor implements Queueable{ public void execute(QueueableContext context) { List<Stage_Object__c> counterparties = [SELECT Counterparty_Identifier__c, Full_Name__c, Processed__c FROM Stage_Object__c WHERE Processed__c = false LIMIT 3300]; list<Stage_Object__c> lststro = new list<Stage_Object__c>(); List<Account> accList = new List<Account>(); List<Marker__c> markerList = new List<Marker__c>(); map<Name,Stage_Object__c> mapstring = new map<Name,Stage_Object__c>(); for (Stage_Object__c so : counterparties){ mapstring.put(so.name,so); Account na = new Account(Name = so.Full_Name__c); accList.add(na); so.Processed__c = true; lststro.add(so); } if(lststro.size()>0) update lststro; if(accList.size()>0) insert accList; for(Account ObjAcc : accList) { Marker__c m = new Marker__c(); m.Identifier__c = mapstring.get(ObjAcc.name).Counterparty_Identifier__c; m.Account_ref__c = ObjAcc.id; markerList.add(m); } if(markerList.size()>0) insert markerList; }}