I've written a trigger which will create a new child Entitlement record whenever an Account is created.
I need to populate the Account's Application_Entitlement_Id__c field with the Id of the newly created Entitlement.
I'm new to Apex and can't work out how to fetch the Id of the newly created Entitlement and add that to the Application Entitlement Id field of the new Account.
trigger AcctSubmitAppEntitlement on Account (after insert) {
List<WEID__c> weids = WEID__c.getall().values();
Set<Id> validRecordTypeIds = new Set<Id>();
for (WEID__c weid: weids) {
try {
validRecordTypeIds.add(weid.WEAccId__c);
} catch (System.StringException e) {
System.debug(
System.LoggingLevel.ERROR,
'Invalid Record Type Id ' + weid.WEAccId__c
);
}
}
List<Entitlement> entitlements = new List<Entitlement>();
for (Account newAccount: Trigger.New) {
if(validRecordTypeIds.contains(newAccount.RecordTypeId)){
Entitlement newEnt = new Entitlement();
newEnt.Name = 'Submit Application';
newEnt.AccountId = newAccount.Id;
newEnt.StartDate = Date.Today();
newEnt.EndDate = Date.Today().addYears(50);
newEnt.BusinessHoursId = '01m7000000008Ke';
newEnt.SlaProcessId = '552M000000000XA';
entitlements.add(newEnt);
}
insert entitlements;
}
}
I've also written the following, I just don't know where to go from here.
if (entitlements.size() > 0){
List<Entitlement> newEntitlements = [SELECT Id from Entitlement
WHERE AccountId = :newAccount.Id];
if (newEntitlements.size() > 0) {
newAccount.Entitlement_ID__c = newEntitlements[0].Id;
}
}
The Application Entitlement Id will be referenced when a new case is created from the related list in Opportunities, using a JavaScript button. This will allow me to assign an Entitlement for every case which is created using the custom button.