CRMJobs.net Homepage
Candidates
 
Employers
My Home  | Job SearchBrowse JobsCompany ProfilesCareer CenterHelpAbout Us |  Contact
Great SFDC Trigger Example to Create a new Contact based on inbound Case.

CRM Applications Discussions


Reply Great SFDC Trigger Example to Create a new Contact based on inbound Case. From:
GuestUser
Below is a great Example of Trigger on Case Object.
1. Get RecordType ID by Name (hmm Not API Name but Label Name :) unusual)
2. This trigger will also check if you have Duplicated.
Please note the "ContactMatch" class is below, too.
3. Insert a new Contact and associate it with the case.
That is all :)
_____________________________________________________
trigger CaseNameTrigger on Case (before insert) {
Id CaseRecord1TypeId =
Schema.SObjectType.Case.getRecordTypeInfosByName().get('RecordTypeName1').getRecordTypeId();
Id CaseRecordType2Id =
Schema.SObjectType.Case.getRecordTypeInfosByName().get('RecordTypeName2').getRecordTypeId();
for(Case c:Trigger.New)
{
if (c.RecordTypeId == CaseRecord1TypeId ) // check Recordtype, 1 then
{
// check if contact already exists, if not then add a new one.
If (c.First_Name__c != Null && c.Last_Name__c != Null) {
Contact con = new Contact(LastName = c.Last_Name__c,
FirstName = c.First_Name__c,
Email=c.SuppliedEmail);
if (ContactMatch.match(con)) //check for duplicate Contact
{
}
else { //New Contact
Insert con; //create
c.contactid=con.id; //get id and associate with the case
}
}
}
else if (c.RecordTypeId == CaseRecordType2Id ) // Recordtype 2
{// process here
}
}
}
________________________________________________________
public class ContactMatch {
public static Boolean match(Contact c) {
List<Contact> test = [SELECT FirstName, LastName, Email
FROM Contact
WHERE FirstName =: c.FirstName
AND LastName =: c.LastName
AND Email =: c.Email
AND Id !=: c.Id];
return (test.size() > 0);
}
}
click to view
Replies to this message: 
-None-
Login | Submit Resume | Job Search | Contact | Terms/Privacy

About Us | Site Map | Partners | Link To Us |

© Copyright 2003 - CRMJobs.NET - All Rights Reserved.