How to populate lookup field value based on record type without using code. i have tired with lookup field filter but there is no record type functionality.
[SalesForce] how to auto populate lookup field value based on record type
Related Solutions
Just create a trigger on OrderItem__c. Since the shipping customer number is unique, when an OrderItem__c record is updated, you can query for the Contact whose Shipping Customer Number matches the new value of the OrderItem__c, and set the Contact lookup on OrderItem__c to match that queried Contact.
You can find information on developing triggers here.
I didn't attempt to compile this and there might be a more elegant way to write it, but it's bulkified and is generally what the logic should be:
trigger SetContactOnOrderItem on OrderItem__c (before update, before insert) {
Set<Integer> shippingNumbers = new Set<Integer>();
for (OrderItem__c collectNumFromOrder : Trigger.new) {
shippingNumbers.add(collectNumFromOrder.Shipping_Customer_Number__c);
}
List<Contact> contactList = [SELECT id, Shipping_Customer_Number__c FROM Contact WHERE Shipping_Customer_Number__c IN :shippingNumbers];
Map<Integer, Contact> shippingNumToContactMap = new Map<Integer, Contact>();
for (Contact c : contactList) {
shippingNumToContactMap.put(c.Shipping_Customer_Number__c, c);
}
for (OrderItem__c o : Trigger.new) {
if (o.Shipping_Customer_Number__c != null) {
o.Contact__c = shippingNumToContactMap.get(o.Shipping_Customer_Number__c).id;
}
else {
o.Contact__c = null;
}
}
}
You can use an if condition if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c)
This will make sure that code is executed only if contact lookup is changed.
if(trigger.isbefore && trigger.isinsert)
{
Set<ID> ConIds = new Set<ID>();
for(Opportunity opp : trigger.new)
{
ConIds.add(opp.RSM_Shipping_Contact__c);
}
list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds];
MAP<ID ,contact> mapCon = new MAP<ID ,Contact>();
for(Contact c : conlist)
{
mapcon.put(c.id,c);
}
for(Opportunity opp : trigger.new)
{
if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c){
if(opp.RSM_Shipping_Contact__c!=null)
{
if(mapcon.containskey(opp.RSM_Shipping_Contact__c))
{
contact c = mapcon.get(opp.RSM_Shipping_Contact__c);
opp.Shipping_Street__c = c.MailingStreet;
opp.Shipping_City__c = c.MailingCity;
opp.Shipping_State__c = c.MailingState;
opp.Shipping_Country__c= c.MailingCountry;
opp.Shipping_postal_code__c = c.MailingPostalCode;
opp.Shipping_Email__c = c.Email;
opp.Shipping_Phone__c = c.phone;
}
}
else
{
opp.Shipping_Street__c = null;
opp.Shipping_City__c = null;
opp.Shipping_State__c = null;
opp.Shipping_Country__c= null;
opp.Shipping_postal_code__c = null;
opp.Shipping_Email__c = null;
opp.Shipping_Phone__c = null;
}
}
}
}
Best Answer
Lookup field filters do not populate data. They only filter available values for a user populating the lookup field or apply constraints to the values that can be added there.
You can easily achieve this objective using Process Builder. Simply set the criteria on your action node to filter based upon the record type. Then add an "Update Records" immediate action and choose the record that started your process. Add a record update entry for the lookup field and populate the appropriate value.
This works for cases where the lookup field should be populated with a constant, an entry stored in custom settings (Process Builder formulas can reference Custom Settings), or a reference to an object that's already related to the source object. If you need to perform a query to locate the record you want to lookup to, you'll have to have your Process call an autolaunched Flow to perform that query.