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
Marián ČekanMarián Čekan 

Getting null instance of custom object

Hi, I'm working on trigger that will [after insert] get Street from ContactPointAddress object connected via lookup (Contact__c) and re-write MailingStreet field in Contact object ONLY if checkbox isDefault is checked. This is what I've done so far:

public static void AfterInsert(ContactPointAddress[] lstContactPointAddresses)
		{
			List<Contact> allPassagersToBeUpdated= new List<Contact>();
			for (ContactPointAddress tmpContactPointAddress : lstContactPointAddresses)
				{
					// created contactPointAddress is checked - is default
					if (tmpContactPointAddress.IsDefault == true) {
						Contact passenger = [SELECT Name,LastName,MailingStreet FROM Contact WHERE Id =: tmpContactPointAddress.Contact__c];

						Address addressCompoundField = (Address)tmpContactPointAddress.Address;
						passenger.MailingStreet = addressCompoundField.Street;
						allPassagersToBeUpdated.add(passenger);

					}
					else {
						return;
					}

				}
			if(allPassagersToBeUpdated.size()>0){
				update allPassagersToBeUpdated;
			}

		}

The problem is, my compound field Addres in Contact Point Address is always null. I don't know why as I can acces values of other fields. Can anyone help me please? Thanks a lot:)
Karan KeharKaran Kehar
Hi Marian,
As mentioned here in the documentation, you can directly access Street field from ContactPointAddress..

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contactpointaddress.htm

Please mark this as best answer if it helps you.!
Marián ČekanMarián Čekan
Hello, thank you for your answer. Unfortunately these are only fields I can access. SO I can directly access compound field Address and from this I can access Street, but as soon as I access Address It returns null for some reason. But it's not like that with whole object - if I access AddressType It returns value from picklist so there is only problem with accessing Address fields I guess.
User-added image
Karan KeharKaran Kehar
OK. Then I guess it is same as getting parent object field values as null in child trigger.    I guess this might help you. https://salesforce.stackexchange.com/questions/106047/before-insert-and-before-update-trigger-returning-null-for-lead-address-field
Marián ČekanMarián Čekan

Unfortunately didn't as I can't access fields Street,City,PostalCode right away from my tmpContactPointAddress as they access from lead.

I even tried to modify my code but nothing seems to work.

 

this is my next version:

 

public static void AfterInsert(List<ContactPointAddress> lstContactPointAddresses) {

		List<Contact> allPassengersToBeUpdated = new List<Contact>();
		set<Id> setOfPassengersIds = new set<Id>();

		for (ContactPointAddress tmpContactPointAddress : lstContactPointAddresses) {
			// created contactPointAddress is checked - is default
			if (tmpContactPointAddress.IsDefault == true) {
				setOfPassengersIds.add(tmpContactPointAddress.Contact__c);
			} else {
				return;
			}
		}

		map<Id, Contact> mapPassengers = new map<Id, Contact>([SELECT Id, MailingStreet FROM Contact WHERE Id IN :setOfPassengersIds ]);
		map<Id, ContactPointAddress> mapCPA = new map<Id, ContactPointAddress>([SELECT Id, Address FROM ContactPointAddress WHERE Contact__c IN :setOfPassengersIds ]);

		for(Contact passenger: mapPassengers.values()) {
			passenger.MailingStreet = ((Address)mapCPA.get(passenger.Id).Address).Street;
			allPassengersToBeUpdated.add(passenger);
		}


		if (allPassengersToBeUpdated.size() > 0) {
			update allPassengersToBeUpdated;
		}
	}
Marián ČekanMarián Čekan
I modyfied code in order to follow this: https://developer.salesforce.com/forums/?id=906F000000091alIAA but nothing...