function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tunedogtunedog 

Person Account Matching Issue

The trigger below runs through a set of logic looking for matching Contacts/Person Accounts based on email address and creating Accounts/Contacts and Person Accounts where they don't already exist.

 

Everything is working fine except for the bit that is highlighted.

It is supposed to create a new Person Account and then update a lookup with the new Person Account ID.

The Person Account record is being created just fine, but the lookup is not being populated like it is with the rest of the trigger.

If a second record is created with a matching email address, the lookup is populated on the second record as it should.

 

Any idea what I am doing wrong??

 

Thanks is advance for any help.

 

 

trigger enquiryTrigger on Enquiry__c (before insert) {

// Get the email address from the enquiry record
   List<Enquiry__c> enqEmail = [select email__c from Enquiry__c where ID in :Trigger.new];
 
   for(Enquiry__c e: Trigger.new){

// Get the contact with matching email address   	
      List<Contact> conID = [select Name from Contact where Email = :e.email__c limit 1];

// If matching contact is found update Enquiry record      
      if (conID.size() > 0) {
      	e.Matching_Contact__c = conID[0].Id;

      } 

//If no matching contact is found and enquiry DOES NOT contain a Company, create a Person Account

else if (e.company__c == null) {

		Account pa = new Account (RecordTypeId = '01220000000DpJM', LastName = e.Name, PersonEmail = e.Email__c, Phone = e.Phone__c);
		insert pa;
		
		// Update Enquiry with new Person Account      		
		e.Matching_Contact__c = pa.PersonContactId;
      	
       } else {

		// If Enquiry does contain a Company check for matching Account record       	
		       	List<Account> accName = [select Id from Account where Name = :e.Company__c limit 1];
		      
		      	if (accName.size() > 0) {
		
				// If matching Account record is found, create a contact on the existing Account
				      		Contact c = new Contact (LastName = e.Name, Phone = e.Phone__c, Email = e.email__c, AccountId = accName[0].Id);
					       	insert c;
				
				// Update Enquiry with new Contact	       	
					       	e.Matching_Contact__c = c.Id;
	       	
		      	} else {
		      		
				// If no matching Account is found, create an Account        		
					       	Account a = new Account (Name=e.Company__c);
					       	insert a;
					       	
				// Create a related Contact	       	
					       	Contact c = new Contact (LastName = e.Name, Phone = e.Phone__c, Email = e.email__c, AccountId = a.Id);
					       	insert c;
					       	
				// Update Enquiry record with new Contact	       	
					       	e.Matching_Contact__c = c.Id;
	       	
			}
		}
	}
}

 

 

 

Ispita_NavatarIspita_Navatar

Can you please share the error you are encountering ? Or just the code is not firing?

I think the field   "e.Matching_Contact__c" is the cause of problem if it is a lookup type , in case it is not of type lookup then it's ok.

The reason why it's being lookup type may give problem as then the lookup will point to any 1 type of object either contact or account, but in your code you are conditionally assigning both contactid and accountid too.

See below:-

 

  e.Matching_Contact__c = pa.PersonContactId;  // assigning accountid or if the lookup is of type contact then it will cause problem.

The fact that the second record is created fine indicates may that when you are trying to access the Contact linked to the preson account it is not created at that point of time.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

 

 

 

tunedogtunedog

Thanks for your help with this.

 

There is no error message given, it simply does not populate the Matching_Contact__c field (which is indeed a lookup).

You may very well be right about what is causing the issue. Though I can associate a Person Account to this field if I do it manually, so I know it can be done. Any ideas what I might need to change in the code to make it work?

Mats ErikssonMats Eriksson

I face the same problem.

 

As far as I understand the problem, it is because the creation of a person account seems to be a two step process behind the scene. First the account is created and then its shadow contact is created and its ID is inserted into the account. Our attempts to use the PersonContactID seems to take place after the account creation but before the ID is set (before the contact is created).

 

Did you find a way to solve the problem?

 

 

/Mats