0

I am trying to use Apex to extract Contact information and create the json to send to our server for updates. This has worked until I added a bit to include a field from the Account object - which links to the Contact object by the Contact AccountID and Account ID fields. For the past few days I have not been able to publish the change due to the test failing. I have encountered multiple errors and I am at the point where I could use some guidance as to how to have the test pass. I have included the actual Apex trigger and test class below - and I realize I have to clean up the building of the json, I am just waiting until everything else is in place.

The current error I am receiving when testing is

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]


This trigger shows what is to be sent

trigger ContactUpdate on Contact (after insert, after update) {
    String buildMessage;
   
    for(Contact a:Trigger.new){
        Account b = [SELECT Id, WellSky_Facility__c FROM Account WHERE Id=:a.AccountId];

            buildMessage = '{';    
            buildMessage = buildMessage + '"MailingCity": "' + a.MailingCity + '"'; 
            buildMessage = buildMessage + ', "MailingPostalCode":' + '"' + a.MailingPostalCode + '"';
            buildMessage = buildMessage + ', "MailingState":' + '"' + a.MailingState + '"';
            buildMessage = buildMessage + ', "MailingStreet":' + '"' + a.MailingStreet + '"';
            buildMessage = buildMessage + ', "Phone":' + '"' + a.Phone + '"';
            buildMessage = buildMessage + ', "Custom_Physician_c":' + '"' + a.Custom_Physician__c + '"';
            buildMessage = buildMessage + ', "Name":' + '"' + a.LastName + '"';
            buildMessage = buildMessage + ', "RecordId":' + '"' + a.Id + '"'; 
            buildMessage = buildMessage + ', "ReferralSource":' + '"' + b.Custom_Facility__c + '"'; 
            buildMessage = buildMessage + '}';
            System.debug('Message 1' + buildMessage);
            ContactDestinationCallout.putDestination(buildMessage); 
    }   
}

The test class where the error occurs:

@isTest
public class ContactUpdateTest {
@isTest static void testCallout1() 
    {    
    Contact tmp = new Contact(LastName = 'ACME', AccountId='0014C00000cu5RPQAY');
    insert tmp;

    Account tmpA = new Account(RecordTypeId = '0125Y000001GTK4QAO', Name = 'TestPerson');
    insert tmpA;
        
        Contact contactToUpdate;
        contactToUpdate = 
        [SELECT LastName FROM Contact 
         WHERE LastName = 'ACME'
         LIMIT 1];

    contactToUpdate.LastName = 'UpdateTest';
    // Make the update call.

        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.startTest();
        update contactToUpdate;  
        Test.stopTest();     
    }
}

UPDATE: With other help, I made the following changes and was able to get the test to pass and push to production:

trigger ContactUpdate on Contact (after insert, after update) {
    String buildMessage;
    String Aid;
    for(Contact a:Trigger.new){
     //   Account b = [SELECT Id, WellSky_Facility__c FROM Account WHERE Id=:a.AccountId];
        List<Account> b = [SELECT Id, WellSky_Facility__c FROM Account WHERE Id=:a.AccountId LIMIT 1];
        if(b.size() > 0) {
            Aid = b[0].WellSky_Facility__c;
        }
            buildMessage = '{';    
            buildMessage = buildMessage + '"MailingCity": "' + a.MailingCity + '"'; 
            buildMessage = buildMessage + ', "MailingPostalCode":' + '"' + a.MailingPostalCode + '"';
            buildMessage = buildMessage + ', "MailingState":' + '"' + a.MailingState + '"';
            buildMessage = buildMessage + ', "MailingStreet":' + '"' + a.MailingStreet + '"';
            buildMessage = buildMessage + ', "Phone":' + '"' + a.Phone + '"';
            buildMessage = buildMessage + ', "Custom_Physician_c":' + '"' + a.Custom_Physician__c + '"';
            buildMessage = buildMessage + ', "Name":' + '"' + a.LastName + '"';
            buildMessage = buildMessage + ', "RecordId":' + '"' + a.Id + '"'; 
      //      buildMessage = buildMessage + ', "ReferralSource":' + '"' + b[0].Custom_Facility__c + '"'; 
            buildMessage = buildMessage + ', "ReferralSource":' + '"' + Aid + '"'; 
            buildMessage = buildMessage + '}';
            System.debug('Message 1' + buildMessage);
            ContactDestinationCallout.putDestination(buildMessage); 
    }   
}

The updated test class:

@isTest
public class ContactTestTwo {


@isTest static void testCallout1() 
    {
         Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.startTest();       
     Account tmpA = new Account(RecordTypeId = '0125Y000001GTK4QAO', Name = 'TestPerson');
    insert tmpA;
        Account contactToUpdate;
        contactToUpdate = 
      [SELECT LastName FROM Account 
         WHERE Name = 'TestPerson'
         LIMIT 1];
   
    Contact tmp = new Contact(LastName = 'ACME',  AccountId= contactToUpdate.Id);
    insert tmp;


        
   //     Contact contactToUpdate;
     //   contactToUpdate = 
    //   [SELECT LastName FROM Contact 
   //      WHERE LastName = 'ACME'
    //     LIMIT 1];

  //  contactToUpdate.LastName = 'UpdateTest';
    // Make the update call.


      //  update contactToUpdate;  
        Test.stopTest();     
    }
}
1
  • Have you resolved your issue? If so, please post the answer, you can mark your own answers as solutions. Dec 18, 2021 at 20:59

1 Answer 1

0

The solution to my issue was what I posted above. The new trigger and test class with the added code:

trigger ContactUpdate on Contact (after insert, after update) {
    String buildMessage;
    String Aid;
    for(Contact a:Trigger.new){
     //   Account b = [SELECT Id, WellSky_Facility__c FROM Account WHERE Id=:a.AccountId];
        List<Account> b = [SELECT Id, WellSky_Facility__c FROM Account WHERE Id=:a.AccountId LIMIT 1];
        if(b.size() > 0) {
            Aid = b[0].WellSky_Facility__c;
        }
            buildMessage = '{';    
            buildMessage = buildMessage + '"MailingCity": "' + a.MailingCity + '"'; 
            buildMessage = buildMessage + ', "MailingPostalCode":' + '"' + a.MailingPostalCode + '"';
            buildMessage = buildMessage + ', "MailingState":' + '"' + a.MailingState + '"';
            buildMessage = buildMessage + ', "MailingStreet":' + '"' + a.MailingStreet + '"';
            buildMessage = buildMessage + ', "Phone":' + '"' + a.Phone + '"';
            buildMessage = buildMessage + ', "Custom_Physician_c":' + '"' + a.Custom_Physician__c + '"';
            buildMessage = buildMessage + ', "Name":' + '"' + a.LastName + '"';
            buildMessage = buildMessage + ', "RecordId":' + '"' + a.Id + '"'; 
      //      buildMessage = buildMessage + ', "ReferralSource":' + '"' + b[0].Custom_Facility__c + '"'; 
            buildMessage = buildMessage + ', "ReferralSource":' + '"' + Aid + '"'; 
            buildMessage = buildMessage + '}';
            System.debug('Message 1' + buildMessage);
            ContactDestinationCallout.putDestination(buildMessage); 
    }   
}

The updated test class:

@isTest
public class ContactTestTwo {


@isTest static void testCallout1() 
    {
         Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        Test.startTest();       
     Account tmpA = new Account(RecordTypeId = '0125Y000001GTK4QAO', Name = 'TestPerson');
    insert tmpA;
        Account contactToUpdate;
        contactToUpdate = 
      [SELECT LastName FROM Account 
         WHERE Name = 'TestPerson'
         LIMIT 1];
   
    Contact tmp = new Contact(LastName = 'ACME',  AccountId= contactToUpdate.Id);
    insert tmp;


        
   //     Contact contactToUpdate;
     //   contactToUpdate = 
    //   [SELECT LastName FROM Contact 
   //      WHERE LastName = 'ACME'
    //     LIMIT 1];

  //  contactToUpdate.LastName = 'UpdateTest';
    // Make the update call.


      //  update contactToUpdate;  
        Test.stopTest();     
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .