A Visualforce remote objects that is type safe and immutable.
npm install typed-remote-objects``sh`
npm i typed-remote-objects
`sh`
yarn add typed-remote-objects
`
`
Define the overwrite method with Apex,
`java`
public with sharing class ExamplePageClass {
@RemoteAction
public static Map
CustomObject__c co = new CustomObject__c();
insert co;
return new Map
}
}
Please call it from Visualforce.
`
...
`
Define the overwrite method with Apex,
`java
public with sharing class ExamplePageClass {
@RemoteAction
public static map
String[] fs = new String[0];
for (String f : fields) {
if (f.equals('Body')) continue;
fs.add(f);
}
return RemoteObjectController.retrieve(object_name, fs, criteria);
}
@RemoteAction
public static map
return RemoteObjectController.create(
object_name,
new Map
'Body' => EncodingUtil.base64Decode((String) fields.get('Body')),
'ContentType' => fields.get('ContentType'),
'Name' => fields.get('Name'),
'ParentId' => fields.get('ParentId')
}
);
}
@RemoteAction
public static map
Attachment a = [SELECT Id, Body FROM Attachment WHERE Id = :record_ids.get(0)];
return RemoteObjectController.updat(
object_name,
record_ids,
new Map
'Body' => EncodingUtil.base64Decode(EncodingUtil.base64Encode(a.Body) + (String) fields.get('Body'))
}
);
}
}
`
Please call it from Visualforce.
`html`
retrieve="{!$RemoteAction.ExamplePageClass.retrieveAttachment}"
create="{!$RemoteAction.ExamplePageClass.createAttachment}"
update="{!$RemoteAction.ExamplePageClass.updateAttachment}"
>
`ts
import { init, Record } from 'typed-remote-objects'
type SObject = {
Id?: string | null
Name?: string | null
CreatedById?: string | null
CreatedDate?: Date | null
LastModifiedById?: string | null
LastModifiedDate?: Date | null
OwnerId?: string | null
FieldString__c?: string | null
FieldNumber__c?: number | null
FieldBoolean__c?: boolean | null
FieldDate__c?: Date | null
}
// Extensions is optional
type Extensions = {
getFormattedCreatedDate(this: SObject): string
getText(this: SObject & Extensions): string
}
const CustomObject__c = () => {
return init
object_name: 'CustomObject__c',
time_zone_offset: 9, // In Visualforce remote objects, dates included in records are acquired in local time, but when insert and update records they are saved as UTC and differences will occur, so adjust with this property.
extensions: {
getFormattedCreatedDate() {
const cd = this.CreatedDate!
return ${cd.getFullYear()}-${cd.getMonth() + 1}-${cd.getDate()}${this.Name} - ${this.getFormattedCreatedDate()}
},
getText() {
return
},
},
})
}
/**
// No extensions
const CustomObject__c = () => init
*/
;(async () => {
// Retrieves
const result1: Record
.where('Id', { eq: 'salesforce_id' })
.one() // Retrieve with limit (1)
const obj = result1!.toObject() // Returns an object literal without methods or functions
const result2: Record
.where('Id', { in: ['salesforce_id_1', 'salesforce_id_2'] })
.size(256) // If limit() offset() is not set, you can specify the number of records with size() (Maximum 2000)
.all()
const result3: Record
.where('OwnerId', { eq: 'salesforce_user_id' })
.order('CreatedDate', 'ASC')
.order('FieldDate__c', 'DESC NULLS LAST') // Multiple orders can be specified
.limit(5) // Maximum 100
.offset(10) // Maximum 2000
.all()
const result4: Record
.where('Name', { eq: 'foo' })
.where('FieldNumber__c', { ne: 0 }) // Multiple conditions can be specified
.all() // If limit() offset() size() is not set, that retrieve up to 2000 records that exist
// AND, OR pattern
const result5: Record
.and(_ => _.where('FieldString__c', { eq: 'text' }), _ => _.where('FieldBoolean__c', { eq: false }))
.all()
const result6: Record
.or(_ => _.where('FieldString__c', { eq: 'text' }), _ => _.where('FieldBoolean__c', { eq: false }))
.all()
// You can also specify conditions in the conventional format
const co = CustomObject__c()
co._wheres = {
and: {
FieldString__c: { eq: 'text' },
FieldBoolean__c: { eq: false },
},
}
const result7: Record
// CUDs
const inserted_record: Record
.record({ FieldBoolean__c: false })
.set('FieldString__c', 'text')
.insert()
const formatted_created_date = inserted_record.getFormattedCreatedDate() // formatted_created_date => YYYY-MM-DD
const updated_record: Record
.set('FieldDate__c', new Date())
.set('FieldString__c', null)
.update()
await updated_record.delete()
// Custom metadata example
const result8: Record
.limit(100) // Please specify limit for error avoidance``
.all()
})()