You need to sign in to do that
Don't have an account?
Lara Ballwieser
insert contact after insert case
Hello everbody,
i have to write a trigger that insert a case. if the email in this case is not in a contact object the trigger create a contact. I tried this:
trigger CaseTrigger on Case (after insert) {
for(Case ca:trigger.new){
Contact[] caseWithEmail = [SELECT Id,Email FROM Contact WHERE Email = :ca.SuppliedEmail];
if(caseWithEmail.isEmpty()){
Contact newContact = new Contact(FirstName = ca.First_Name__c, LastName = ca.Last_Name__c, Phone = ca.SuppliedPhone, Email = ca.SuppliedEmail,
MailingStreet = ca.MailingStreet__c, MailingCity = ca.MailingCity__c, MailingPostalCode = ca.MailingPostalCode__c,
MailingState = ca.MailingState__c);
insert newContact;
}
}
}
I think a piece is absence but I don't know what it is.
Can somebody help me to find it?
Thank you!
i have to write a trigger that insert a case. if the email in this case is not in a contact object the trigger create a contact. I tried this:
trigger CaseTrigger on Case (after insert) {
for(Case ca:trigger.new){
Contact[] caseWithEmail = [SELECT Id,Email FROM Contact WHERE Email = :ca.SuppliedEmail];
if(caseWithEmail.isEmpty()){
Contact newContact = new Contact(FirstName = ca.First_Name__c, LastName = ca.Last_Name__c, Phone = ca.SuppliedPhone, Email = ca.SuppliedEmail,
MailingStreet = ca.MailingStreet__c, MailingCity = ca.MailingCity__c, MailingPostalCode = ca.MailingPostalCode__c,
MailingState = ca.MailingState__c);
insert newContact;
}
}
}
I think a piece is absence but I don't know what it is.
Can somebody help me to find it?
Thank you!
for everyone how will have had this problem in future. I have found the final solution:
You also have to require the Web-Email field when you use Web-to-Case!!!
All Answers
Try below code:
And Ravi is right, you need to read about trigger bulkification as:
1. Never ever write a SOQL query inside any “for” loop. If you do that, your trigger is guaranteed to hit the governor limits.
2. Never ever perform a DML operation inside a “for” loop.
3. Do not fetch unnecessary data. Only fetch those fields and objects in your SOQL that you really require.
4. Always try to separate trigger and it's handler class.
5. In your code try to put null check conditions at every place where the record has the possibility to come null or undefined.
Follow this link also:
http://www.sfdc99.com/2014/01/18/bulkifying-code/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
@Ajay i tried your code but there is already the same problem. But thanks for your help. I will try to solve the problem and if i fixed it, I can write here again.
@Ajay I wrote a test for your solution and it is working well, but it dont create a contact in my application at all. I dont know whats the problem.
Do you have any idea what it could be?
for everyone how will have had this problem in future. I have found the final solution:
You also have to require the Web-Email field when you use Web-to-Case!!!