//Get your Lead records
List<Lead> leadList = [SELECT Id From Lead LIMIT 10];
if(!leadList.isEmpty()) {
//Run Lead assignment rule from apex
Database.DMLOptions dmlOption = new Database.DMLOptions();
dmlOption.assignmentRuleHeader.useDefaultRule = true;
Database.update(leadList, dmlOption);
}
Run Case Assignment From Apex Sample Code:
//Get your Case records
List<Case> caseList = [SELECT Id From Case LIMIT 10];
if(!caseList.isEmpty()) {
//Run Case assignment rule from apex
Database.DMLOptions dmlOption = new Database.DMLOptions();
dmlOption.assignmentRuleHeader.useDefaultRule = true;
Database.update(caseList, dmlOption);
}
In some scenarios, we need to update records which Owners are inactive. For example, we are migrating data to Salesforce using data loader, some record owners are inactive. For those inactive owner records, we will get “A record owner cannot be Inactive” error. We have to change ownership of the record to an active owner.
To update records with inactive Owners, we have to enable
Enable “Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” User Permissions.
Go to Setup | Enter User Interface in the Quick Find box | Select User Interface | Enable “Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” User Permissions.
After enabled the above setup, enable Update Records with Inactive Owners of the user Profile or in a Permission Set. You can set audit fields only in API-enabled editions of Salesforce.
//Get current date time in GMT format
Datetime dt = System.now();
//Convert it into Pacific Standard Time(PST).
String dtPST = dt.format('yyyy-MM-dd HH:mm:ss', 'PST');
System.debug('dt-' + dt);
System.debug('dtPST-' + dtPST);
//Upload File
ContentVersion cv = new ContentVersion();
cv.Title = 'Chatter Post Document';//File title
cv.PathOnClient = 'chattertestdoc.pdf';//File name
cv.VersionData = Blob.valueOf('Test Content');//File body (Add content or body of uploaded file)
cv.Description = 'Chatter Post Document';//File description
insert cv;
//Create Chatter Post
FeedItem fi = new FeedItem();
fi.Body = 'Chatter post from apex with attachment';
fi.ParentId = '0010I00002AbcMm'; //Record Id
insert fi;
//Associate attachment to the post
FeedAttachment fa = new FeedAttachment();
fa.FeedEntityId = fi.Id;//Chatter Post Id
fa.RecordId = cv.Id;//Document Id
fa.Type = 'CONTENT';
insert fa;
public class SampleAuraController {
@AuraEnabled
public string defaultRecordTypeId {get; set;}
@AuraEnabled
public Map<Id, String> contactRecordTypes {get; set;}
@AuraEnabled
public static SampleAuraController getRecordTypeValues(){
SampleAuraController obj = new SampleAuraController();
Map<Id, String> recordtypeMap = new Map<Id, String>();
//Get all record types of Contact object
List<Schema.RecordTypeInfo> recordTypeInfoList = Contact.SObjectType.getDescribe().getRecordTypeInfos();
for(RecordTypeInfo info: recordTypeInfoList) {
//Check record type is available for current user profile
if(info.isAvailable()) {
//Check master record type
if(info.getName() != 'Master' && info.getName().trim() != ''){
recordtypeMap.put(info.getRecordTypeId(), info.getName());
}
//Get the default record type for current user profile
if(info.isDefaultRecordTypeMapping()){
obj.defaultRecordTypeId = info.getRecordTypeId();
}
}
}
obj.contactRecordTypes = recordtypeMap;
return obj;
}
}