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
AndegosuAndegosu 

Autopopulate/suggest record based on Contact address

We have nonprofit asssociations that are responsible for different areas in Sweden. When I create a new contact in lightning I want the association field to be autofilled or suggested for the user with the corresponding association based on the Contact's postalcode.

We will have a table with each association and their respective postalcodes. So based on the postalcodes in this table I want to suggest a association for the contact that has a corresponding postalcode.

Is this possible? I'm not sure how to implement this requirement and would be grateful for some assistance.
Best Answer chosen by Andegosu
Sujeet PatelSujeet Patel
Hiii Andegosu
You can also use after trigger.
to getting the data about 16000 Postal code we should go for the batch class which help us to get records easily.
and According to me map will help full for us
because of below line
"ass.get(c.PostalCode)" 
will return name of association according to postal code.
As i say above to get the data about postal we use batch apex class and a method which return data of postal codes.

And
i have another method which also help full for you
trigger ContactBI on Contact(before insert){
	
	String str;
	for(Contact c:trigger.new){
			str=c.PostalCode;
	}
	
	Association as=[select id,name,PostalCode from Association where PostalCode=:str limit 1];
	
	for(Contact c:trigger.new){
			str=ac.Name;
	}
	
}
I think this code is help for you
and my this code is not exact as you think this is only demo 

 

All Answers

Sujeet PatelSujeet Patel
Yes we can do that with the help of trigger.
we need to write before insert trigger on contact object.
I am writing simple logic of that implementation.
trigger ContactBI on Contact(before insert){
	
	List<Association> as=[select id,name,code from Association];
	Map<String,String> ass=new Map<String,string>();
	for(Association ac_ob:as)
	{
		ass.put(ac_ob.Name,ac_ob.Code);
	}
	
	for(Contact c:trigger.new){
		c.Association=ass.get(c.PostalCode);
	}
	

}


 
AndegosuAndegosu
Thank you for your answer Sujeet.

I don't think the before trigger would allow us to get a suggestion when creating the contact? So after you filled in the postalcode you get a suggestion for the appropiate association. Sorry for being unclear about this.

Also I'm not sure a map would work, in your example you are doing a get on postalcode so the postalcode would have to be the key (first param of the map) and not the value. Sweden has over 16000 postal codes that would have to populate a map everytime you create a contact which would take some time.
Sujeet PatelSujeet Patel
Hiii Andegosu
You can also use after trigger.
to getting the data about 16000 Postal code we should go for the batch class which help us to get records easily.
and According to me map will help full for us
because of below line
"ass.get(c.PostalCode)" 
will return name of association according to postal code.
As i say above to get the data about postal we use batch apex class and a method which return data of postal codes.

And
i have another method which also help full for you
trigger ContactBI on Contact(before insert){
	
	String str;
	for(Contact c:trigger.new){
			str=c.PostalCode;
	}
	
	Association as=[select id,name,PostalCode from Association where PostalCode=:str limit 1];
	
	for(Contact c:trigger.new){
			str=ac.Name;
	}
	
}
I think this code is help for you
and my this code is not exact as you think this is only demo 

 
This was selected as the best answer