Skip to content

Working with ts force Objects

Charlie Jonas edited this page Nov 15, 2018 · 2 revisions

Once queried, the generated classes make working with our data very easy:

let firstContact = contacts[0];
console.log(firstContact.email, firstContact.account.name, firstContact.account.nameCustom);

for(let acc of accountsWithContacts){
    for(let contact of acc.contacts){
        console.log(contact.name, contact.phone, contact.email);
    }
}

Any SObject can be created via the constructor. The first param to the constructor is an object of fields:

let account = new Account({
    name: 'abc',
    accountNumber: '123',
    website: 'example.com'
});

Each SObject also standard DML operations on it's instance. insert(), update(), delete()

await account.insert();
console.log(account.id);
account.name = 'abc123';
await account.update();

You can specify parent relationships via the corresponding Id field (eg: accountId) or via external id

let contact1 = new Contact({
    firstName: 'john',
    lastName: 'doe',
    accountId: account.id
});
await contact1.insert();
console.log('contact1:',contact1.id);

//add an My_External_Id__c field to account and regenerate classes to test
let contact2 = new Contact({
    firstName: 'jimmy',
    lastName: 'smalls',
    account: new Account({myExternalId:'123'})
});
await contact2.insert();
console.log('contact2:',contact2.id);

NOTE: When executing DML on a record with children, the children ARE NOT included in the request!

Next Bulk