You need to sign in to do that
Don't have an account?
Return ID empty when creating person account: Urgent
trigger CreateContactOnNewCase on Case (after insert) {
//Returns a list of the new versions of sObject records
for (Case createdCase : Trigger.new) {
//Check if the case & person account is not already linked by web to case
if (createdCase.ContactId == null) {
//Check if the email address is not null, need to use this to check if a person account already exist
Integer intCount = 0;
if ( createdCase.SuppliedEmail != null ) {
intCount = [Select count() From account Where personemail = :createdCase.SuppliedEmail AND account.RecordTypeId = '01240000000DQUpAAO'];
} else { intCount = 0; }
//validate if the person account is present
if ( intCount <= 0 ){
//Provide values from the case to the new person account and create the account.
Account newAccount = new Account();
newAccount.RecordTypeId = '01240000000DQUpAAO';
newAccount.PersonEmail = createdCase.SuppliedEmail;
newAccount.Phone = createdCase.phone_number__c;
newAccount.FirstName = createdCase.First_Name_Billing__c;
//If the case's lastname is null then make the lastname as 'UpdateNeeded'
if ( createdCase.Bill_to_Name__c == null ) {
newAccount.LastName = 'UpdateNeeded';
} else {
newAccount.LastName = createdCase.Bill_to_Name__c;
}
if ( createdCase.Billing_Address_2__c != null && createdCase.Billing_Address_2__c.length() > 0 ) {
newAccount.BillingStreet = createdCase.Billing_Address_1__c + ' ' + createdCase.Billing_Address_2__c;
} else { newAccount.BillingStreet = createdCase.Billing_Address_1__c; }
newAccount.BillingCity = createdCase.Billing_City__c;
newAccount.BillingState = createdCase.Billing_State__c;
newAccount.BillingPostalCode = createdCase.Billing_Zip__c;
try {
insert newAccount;
} catch (System.DmlException e) {
System.debug('Problem creating person account = ' + e.getDmlMessage(0));
}
//Update case with the newly created person account
try {
Id thisId = [SELECT id FROM contact, contact.account Where contact.account.Id = :newAccount.Id].Id;
//UpdateCaseContact.updateCase( createdCase.Id, newAccount.PersonContactId );
UpdateCaseContact.updateCase( createdCase.Id, thisId );
} catch (System.DmlException e) {
System.debug('Problem linking the case to the new person account = ' + e.getDmlMessage(0));
}
} else {
//Query the person account for the above email address, check if the person accont already exist
Id thisPersonAccount = [SELECT id FROM contact, contact.account Where contact.email = :createdCase.SuppliedEmail AND contact.account.RecordTypeId = '01240000000DQUpAAO' limit 1].Id;
//if exist then update the case with the existing person account id.
UpdateCaseContact.updateCase( createdCase.Id, thisPersonAccount );
} }}}
CLASS:
global class UpdateCaseContact {
public static void updateCase(Id strCaseID, Id strPersonID) {
//Lookup the case with the provided case Id
Case newCase = new Case(Id = strCaseID);
//Update the case's contact id with the provided person id (i.e. strPersonID variable)
newCase.ContactId = strPersonID;
update newCase;
} }
THanks,
rk
Do you hit any goverlimit? I am doing the same thing, and I get the following error.
Too many SOQL queries: 21
I am told to change the code to bulk, and remove my SOQL out of for loop?
Did you update your code to use "set" "map"?
Thanks
Uma