We are looking for sample APEX that would, upon Case creation or changing the existing Account on a case to perform the following:
Perform a lookup to the related Account and find the Entitlement Associated to the Account with a 'Type' field value of "Support Contract"
There can be from zero to many Entitlements associated to an Account, but there would only be one Entitlement with a the 'Type' field set to "Support Contract", so the results would return one record.
We need this entitlement to be automatically associated to the Case. There are no Milestones to worry about.
Essentially, when a Case is created or the Case Account field is modified. Then we can create lookup fields to display (Status, Contract Start and End Date) on the case.
Does anyone have any sample APEX I can look at that might point me in the right direction.
Currently 99% of cases are created through the "email to case" functionality, not manually.
* For the BeforeUpdate portion of the trigger, you should process only Cases where the Account was changed. You can use Trigger.oldMap to compare the values.
* For the BeforeUpdate portion of the trigger, you should process only Cases where the Account was changed. You can use Trigger.oldMap to compare the values.
I wrote found this code. I just need to figure out how to write the test code to approve it for production.
trigger SupportPlanEntitlement on Case (Before Insert, Before Update) {
/*
When a case is created, auto-populate with the active support plan
If the Entitlement Name is not set then, check to see if the Contact on the Case has an active Entitlement
and select the first one. If not then check to see if the Account on the Case has an active Entitlement.
*/
List<Id> contactIds = new List<Id>();
List<Id> acctIds = new List<Id>();
for (Case c: Trigger.new){
if (c.EntitlementId == null && c.ContactId!= null && c.AccountId!= null){
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
}
if(contactIds.isEmpty()==false || acctIds.isEmpty()==false){
/* Added check for active entitlement */
List <EntitlementContact> entlContacts = [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId From EntitlementContact e
Where e.ContactId in:contactIds
And e.Entitlement.Type = 'Support Plan' And e.Entitlement.EndDate >= Today And e.Entitlement.StartDate <= Today];
if(entlContacts.isEmpty()==false){
for(Case c: Trigger.new){
if(c.EntitlementId == null && c.ContactId!= null){
for(EntitlementContact ec:entlContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId=ec.Entitlement.AssetId;
break;
}
} // end for
}
} // end for
} else{
List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
From Entitlement e
Where e.AccountId in:acctIds And e.Type = 'Support Plan' And e.EndDate >= Today And e.StartDate <= Today];
if(entls.isEmpty()==false){
for(Case c: Trigger.new){
if(c.EntitlementId == null && c.AccountId!= null){
for(Entitlement e:entls){
if(e.AccountId==c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
} // end for
}
} // end for
}
}
} // end if(contactIds.isEmpty()==false)
}
I presume you have an Entitlement lookup field in the Case? If so, your case trigger should be straightforward.
* You'll need to specify BeforeInsert and BeforeUpdate.
* You can use a SOQL relationship query to get the correct Entitlement for the account. See http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
* For the BeforeUpdate portion of the trigger, you should process only Cases where the Account was changed. You can use Trigger.oldMap to compare the values.
All Answers
I presume you have an Entitlement lookup field in the Case? If so, your case trigger should be straightforward.
* You'll need to specify BeforeInsert and BeforeUpdate.
* You can use a SOQL relationship query to get the correct Entitlement for the account. See http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
* For the BeforeUpdate portion of the trigger, you should process only Cases where the Account was changed. You can use Trigger.oldMap to compare the values.
Thank you
I wrote found this code. I just need to figure out how to write the test code to approve it for production.