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
NevDevNevDev 

Validation Rule To Only Show Certain Contact Values In A Contact Lookup Field.

Hi Guys,

I have two lookup fields on the Cases object. One field is a lookup field to Contacts and the other is a lookup field to Policy_c which is a child object of Contacts.

What I want to do is once a user types a Policy No. in the Policy_No_c Lookup field and click on the search icon next to the Contacts lookup field, I only want Contacts who have the Policy No as a related list item in the Contact record to appear.

​How can I achieve this? 
NevDevNevDev
Hi Vineet,

Can this solution work if I am referencing a related list item and not a field on the Contact record. So a Contact could have 4 related Policies in a related list item, some of which could be joint policies. If I type the policy number into the Policy_c lookup field in Cases can I filter the Contact Lookup field to only show the Contacts that have that policy number as a related list item in their Contact records?
VineetKumarVineetKumar
No, that ways not, you will have to create your own custom lookup using a VF page.
What i can suggest is to create a field on contact, that has all the related policies value in it, appended as a string (can be done using a basic trigger/ process builder).
Refer this custom field while setting up the dependent lookup.
priti jaiswal 17priti jaiswal 17
Not Possible on the standard Page. The only soultion to achieve this is replacing you standard page with VF Page and write a custom code.

See the attached link for your reference.
http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/
 
NevDevNevDev
What field type would I use for the custom field?
VineetKumarVineetKumar
Make it Text
NevDevNevDev
Vineet,

Using the Process Builder I'm not seeing how this is possible. What criteria should I use? 
VineetKumarVineetKumar
It would be combination for process builder and visual flows:
Refer the link below for step by step process :
http://focusonforce.com/process-automation/focus-on-automation-visual-workflow-loops/
NevDevNevDev
Hi Vineet,

Can you help me with the trigger that I need to autopopulate the field in the Contact record with the related Policy No's?
VineetKumarVineetKumar
This is not a working code, you make take help and update the code with proper field API name and logic
trigger PolicyTrigger on Policy_c (after insert, after update) {
	Set<Id> contactIdSet = new Set<Id>();
	for(Policy__c thisRecord : trigger.new){
		if(thisRecord.Policy_No_c != null && thisRecord.ContactId != null){
			contactIdSet.add(thisRecord.ContactId);
		}
	}
	Map<String, String> contactPolicyNoMap = new Map<String, String>();
	for(Policy__c thisRecord : [SELECT Policy_No_c, ContactId FROM Policy__c WHERE Id IN: contactIdSet]){		 
		if(contactPolicyNoMap.containsKey(thisRecord.ContactId)){
			String policyString = contactPolicyNoMap.get(thisRecord.ContactId);
			contactPolicyNoMap.put(thisRecord.ContactId, policyString + thisRecord.Policy_No_c);
		}else{
			contactPolicyNoMap.put(thisRecord.ContactId, thisRecord.Policy_No_c);
		}
	}
	List<Contact> updateContactList = new List<Contact>();
	for(String thisKey : contactPolicyNoMap.keySet()){
		Contact newContact = new Contact();
		newContact.Id = thisKey
		newContact.Policy_No_c = contactPolicyNoMap.get(thisKey);
		updateContactList.add(newContact);
	}
	if(!newContact.isEmpty()){
		update updateContactList;
	}
}
NevDevNevDev
Hi Vineet,

Thank you for this. I am receiving the below error message. The custom field API is Policy_c and the Custom Object API is Policies__c

Error: Compile Error: No such column 'Policy_No_c' on entity 'Policies__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 32
NevDevNevDev
Hi Vineet,

I keep getting an invalid SObject type name error when I use the code that you provided. I have changed the Custom object name to Policy_Information_c which is the object that relates to the Contact object. The custom field that I created to capture the Policy numbers is Policy_No_c. 

Not sure where I'm going wrong. 
VineetKumarVineetKumar
Invalid SObject means that the API Name of the object that you are referring is not correct.
Just your latest code in here, then I can have a look
NevDevNevDev
trigger PolicyTrigger on Policy_Information_c (after insert, after update) {
    Set<Id> contactIdSet = new Set<Id>();
    for(Policy__c thisRecord : trigger.new){
        if(thisRecord.Policy_No_c != null && thisRecord.ContactId != null){
            contactIdSet.add(thisRecord.ContactId);
        }
    }
    Map<String, String> contactPolicyNoMap = new Map<String, String>();
    for(Policy__c thisRecord : [SELECT Policy_No_c, ContactId FROM Policy__c WHERE Id IN: contactIdSet]){        
        if(contactPolicyNoMap.containsKey(thisRecord.ContactId)){
            String policyString = contactPolicyNoMap.get(thisRecord.ContactId);
            contactPolicyNoMap.put(thisRecord.ContactId, policyString + thisRecord.Policy_No_c);
        }else{
            contactPolicyNoMap.put(thisRecord.ContactId, thisRecord.Policy_No_c);
        }
    }
    List<Contact> updateContactList = new List<Contact>();
    for(String thisKey : contactPolicyNoMap.keySet()){
        Contact newContact = new Contact();
        newContact.Id = thisKey
        newContact.Policy_No_c = contactPolicyNoMap.get(thisKey);
        updateContactList.add(newContact);
    }
    if(!newContact.isEmpty()){
        update updateContactList;
    }
}
NevDevNevDev
Hi Vineet,

I'm still getting the same error message. User-added image
NevDevNevDev
The actual API for the Policy Information Object is Policy_Information__c with a double underscore. I'm can't change it was automatically generated when I created the Object. Is this what is causing the error?
VineetKumarVineetKumar
Yea, we were missing the double underscores
Trigger PolicyTrigger on Policy_Information__c (after insert, after update) {
    Set<Id> contactIdSet = new Set<Id>();
    for(Policy_Information__c thisRecord : trigger.new){
        if(thisRecord.Policy_No_c != null && thisRecord.ContactId != null){
            contactIdSet.add(thisRecord.ContactId);
        }
    }
    Map<String, String> contactPolicyNoMap = new Map<String, String>();
    for(Policy_Information__c thisRecord : [SELECT Policy_No_c, ContactId FROM Policy_Information__c WHERE Id IN: contactIdSet]){        
        if(contactPolicyNoMap.containsKey(thisRecord.ContactId)){
            String policyString = contactPolicyNoMap.get(thisRecord.ContactId);
            contactPolicyNoMap.put(thisRecord.ContactId, policyString + thisRecord.Policy_No_c);
        }else{
            contactPolicyNoMap.put(thisRecord.ContactId, thisRecord.Policy_No_c);
        }
    }
    List<Contact> updateContactList = new List<Contact>();
    for(String thisKey : contactPolicyNoMap.keySet()){
        Contact newContact = new Contact();
        newContact.Id = thisKey;
        newContact.Policy_No_c = contactPolicyNoMap.get(thisKey);
        updateContactList.add(newContact);
    }
    if(!newContact.isEmpty()){
        update updateContactList;
    }
}
NevDevNevDev
Hi Veenet,

It's back to this error message again


Error: Compile Error: No such column 'ContactId' on entity 'Policy_Information__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 44
 

This is a bit confusing as I am coping and pasting the actual API name directly into the code so it should definitely be correct. 
VineetKumarVineetKumar
What is the API name of the contact field on your object?
NevDevNevDev
Policy_Info__c
VineetKumarVineetKumar
Then you need to replace, contactId in your code with Policy_Info__c
NevDevNevDev
Hi Vineet,


Error: Compile Error: Field is not writeable: Contact.Name at line 21 column 9

Trigger PolicyTrigger on Policy_Information__c (after insert, after update) {
    Set<Id> Policy_Holder__cSet = new Set<Id>();
    for(Policy_Information__c thisRecord : trigger.new){
        if(thisRecord.Name != null && thisRecord.Policy_Holder__c != null){
            Policy_Holder__cSet.add(thisRecord.Policy_Holder__c);
        }
    }
    Map<String, String> contactPolicyNoMap = new Map<String, String>();
    for(Policy_Information__c thisRecord : [SELECT Name, Policy_Holder__c FROM Policy_Information__c WHERE Id IN: Policy_Holder__cSet]){        
        if(contactPolicyNoMap.containsKey(thisRecord.Policy_Holder__c)){
            String policyString = contactPolicyNoMap.get(thisRecord.Policy_Holder__c);
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, policyString + thisRecord.Name);
        }else{
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, thisRecord.Name);
        }
    }
    List<Contact> updateContactList = new List<Contact>();
    for(String thisKey : contactPolicyNoMap.keySet()){
        Contact newContact = new Contact();
        newContact.Id = thisKey;
        newContact.Name = contactPolicyNoMap.get(thisKey);
        updateContactList.add(newContact);
    }
    if(!newContact.isEmpty()){
        update updateContactList;
    }
}
 
VineetKumarVineetKumar
newContact.Name is not writeable on Contact.
What I asked you was to create a new Text field, and save the appended value on that.
Replace newContact.Name with newContact.<customField>
NevDevNevDev

Error: Compile Error: Variable does not exist: newContact at line 24 column 10
 
NevDevNevDev
Hi Vineet,

This will populate the custom text field with all of the Related Policy Numbers right? 
VineetKumarVineetKumar
Nope, your newContact is only and instance of contact object, you need to refer the contact field as well.
Something like : newContact.policy_lookup_key__c
NevDevNevDev
Do I replace     if(!newContact.isEmpty()){   with newContact.policy_lookup_key__c?

 
VineetKumarVineetKumar
Can you let me the API Name of the field on Contact object, that will contain the string of policy numbers?
NevDevNevDev
API Name Of Field On Contact Object = Policy_Info_c
API Custom Object = Policy_Information__c
API Custom Object Name Field = Policy Number (I can't find the actual API name of this field, It is the Name field renamed to Policy Number). 
VineetKumarVineetKumar
Trigger PolicyTrigger on Policy_Information__c (after insert, after update) {
    Set<Id> Policy_Holder__cSet = new Set<Id>();
    for(Policy_Information__c thisRecord : trigger.new){
        if(thisRecord.Policy_No_c != null && thisRecord.Policy_Holder__c != null){
            Policy_Holder__cSet.add(thisRecord.Policy_Holder__c);
        }
    }
    Map<String, String> contactPolicyNoMap = new Map<String, String>();
    for(Policy_Information__c thisRecord : [SELECT Policy_Info__c, Policy_Holder__c FROM Policy_Information__c WHERE Id IN: Policy_Holder__cSet]){        
        if(contactPolicyNoMap.containsKey(thisRecord.Policy_Holder__c)){
            String policyString = contactPolicyNoMap.get(thisRecord.Policy_Holder__c);
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, policyString + thisRecord.Policy_No_c);
        }else{
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, thisRecord.Policy_No_c);
        }
    }
    List<Contact> updateContactList = new List<Contact>();
    for(String thisKey : contactPolicyNoMap.keySet()){
        Contact newContact = new Contact();
        newContact.Id = thisKey;
        newContact.Policy_Info__c = contactPolicyNoMap.get(thisKey);
        updateContactList.add(newContact);
    }
    if(!newContact.isEmpty()){
        update updateContactList;
    }
}

 
NevDevNevDev
Error: Compile Error: No such column 'Policy_Info__c' on entity 'Policy_Information__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 44
 
VineetKumarVineetKumar
This is the correct code :
Trigger PolicyTrigger on Policy_Information__c (after insert, after update) {
    Set<Id> contactIdSet = new Set<Id>();
    for(Policy_Information__c thisRecord : trigger.new){
        if(thisRecord.Name != null && thisRecord.Policy_Holder__c != null){
            contactIdSet.add(thisRecord.Policy_Holder__c);
        }
    }
    Map<String, String> contactPolicyNoMap = new Map<String, String>();
    for(Policy_Information__c thisRecord : [SELECT Name, Policy_Holder__c FROM Policy_Information__c WHERE Policy_Holder__c IN: contactIdSet]){        
        if(contactPolicyNoMap.containsKey(thisRecord.Policy_Holder__c)){
            String policyString = contactPolicyNoMap.get(thisRecord.Policy_Holder__c);
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, policyString +','+ thisRecord.Name);
        }else{
            contactPolicyNoMap.put(thisRecord.Policy_Holder__c, thisRecord.Name);
        }
    }
    List<Contact> updateContactList = new List<Contact>();
    for(String thisKey : contactPolicyNoMap.keySet()){
        Contact newContact = new Contact();
        newContact.Id = thisKey;
        newContact.Policy_Info__c = contactPolicyNoMap.get(thisKey);
        updateContactList.add(newContact);
    }
    if(!updateContactList.isEmpty()){
        update updateContactList;
    }
}

Do mark this answer aas best answer if it helped you.
NevDevNevDev
Hi guys,

Can anyone please get involved here and help me resolve this? The suggestions above have worked up to a certain point but it still remains unresolved. Here is what I am trying to do and what I have done:

Created a Custom object called Policy_Information_c which has a Master Detail Relationship with Contacts
Created a Custom field in Cases object which looks up Policy_Information_c
A Policy_Information_c can be related to multiple Contacts
In the Cases object the Contact Lookup field is filtered to only show Contacts related to the Policy No. So the Policy no. is typed in first.

Looks like a simple requirement but I'm struggling Badly to find a solution, and I have been working on this simple requirement now for a couple of days.