This post regarding how to create a new case when ever customer shop visit .Here shop visit is a custom object so trigger looks like this
Apex Trigger:
trigger createCaseFromBroker on ShopVisit__c (after insert) {
List<Group> sfdcGroup = [select Id, name from Group where Name = 'Support Queue' limit 1];
if(Trigger.new.size() == 1){
for(ShopVisit__c b:Trigger.new){
//for each broker, b, create a new DueDiligence case
Case newCase = new Case(
Subject = 'Perform Due Diligence',
shopvisit__c = b.Id,
Status = 'Open',
Priority = 'Medium',
Category__c = 'Pre-Approval',
Subcategory__c = 'How to become a Broker',
Origin = 'Web' //is there an origin? required in UI
);
//if broker has a primary contact
if(b.Primary_Contact__c != null){
newCase.ContactId = b.Primary_Contact__c;
}
if(!sfdcGroup.isEmpty()){
newCase.OwnerId = sfdcGroup[0].Id;
}
insert newCase;
}
}
}
Test Class:
public class CreateCase {
static testMethod void test_createCaseFromBroker(){
//create contact record
Contact contact = new Contact();
contact.FirstName = 'Test 1 - contact';
contact.LastName = 'Dummy';
insert contact;
String contactId = contact.Id;
//create broker record #1
shopvisit__c broker1 = new shopvisit__c();
broker1.Name = 'Test 1 - broker';
broker1.Primary_Contact__c = contactId;
insert broker1;
String broker1Id = broker1.Id;
//validate case was created
List<Case> bsCase = [
SELECT Id,Shopvisit__c
FROM case
WHERE shopvisit__c = :broker1Id
LIMIT 1
];
for( Case c:bsCase ){
System.assertEquals(broker1Id,c.shopvisit__c);
}
}
}

No comments:
Post a Comment