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
TilluTillu 

Please currect below trigger issue ?

I have 2 objects policy and  Interview. On Interview Record, i have policy as a lookup.

If interview fields contact name , Insured name  matches with any policy record's contact name , Annuity name  Then i have to pull the Policy record on to the Interview. I have written below code but failed. please any one update this ?


trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {

set<id> cset = new set<id>();

Map<Id,List<Policy__c>> policyMap = new Map<Id,List<Policy__c>>();
List<policy__c> poList = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from Policy__c where Contact__c in:cset ];

Map<Id,string> Intmap = new Map<id,string>();

for(Policy__c poli : PoList ){
if(policyMap.get(poli.Contact__c) !=null){
policyMap.put(poli.Contact__c,new list<policy__c>());
Intmap.put(poli.Id,poli.Annuitiant_Name__c);
}
}
for(Interviews__c Intrvews : Trigger.new){
for(Policy__c polic : PoList ){
if(policyMap.ContainsKey(Intrvews.Contact__c)&&policyMap.ContainsKey(Intrvews.Insured_Name__c)){ // Comparing  both policy,Interview matches

Intrvews.Policy1__c = Polic.Id;
}
}
}
}
Denis VakulishinDenis Vakulishin
Hi,
1) your poList will be empty because of empty cset variable
set<id> cset = new set<id>();

Map<Id,List<Policy__c>> policyMap = new Map<Id,List<Policy__c>>();
List<policy__c> poList = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from Policy__c where Contact__c in:cset ];
2) in the FOR loop this condition will be never met, because you never fill the map 
if(policyMap.get(poli.Contact__c) !=null){


pbattissonpbattisson
There are a number of issues in your code as commented below:

trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
	
	//ANS - Here you are declaring an empty set of ids for the contacts
	set<id> cset = new set<id>();

	Map<Id,List<Policy__c>> policyMap = new Map<Id,List<Policy__c>>();

	//ANS - When you run this query with the in selector using an empty set of ids then it will return no results
	List<policy__c> poList = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from Policy__c where Contact__c in:cset ];

	Map<Id,string> Intmap = new Map<id,string>();


	//ANS - This loop does nothing as the Policy Map is empty and so calling policyMap.get(poli.Contact__c) will always return null
	for(Policy__c poli : PoList ){
		if(policyMap.get(poli.Contact__c) !=null){
			policyMap.put(poli.Contact__c,new list<policy__c>());
			Intmap.put(poli.Id,poli.Annuitiant_Name__c);
		}
	}
	
	//ANS - This loop again will do nothing because the Policy Map is still empty and so the first part of the conditional is always false
	for(Interviews__c Intrvews : Trigger.new){
		for(Policy__c polic : PoList ){
			if(policyMap.ContainsKey(Intrvews.Contact__c)&&policyMap.ContainsKey(Intrvews.Insured_Name__c)){ 
				// Comparing  both policy,Interview matches

				Intrvews.Policy1__c = Polic.Id;
			}
		}
	}
}

I think the code below should do what you want - or at least act as a starting point.

trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
	
	//Declare our set
	set<id> cset = new set<id>();

	//Loop through our interviews and store the contact Ids
	for(Interviews__c interview : Trigger.new) {
		if(interview.Contact__c != null) {
			cset.add(interview.Contact__c);
		}
	}

	//Retrieve a list of policies
	List<policy__c> poList = [select Id, Contact__c, Annuitiant_Name__c from Policy__c where Contact__c in :cset ];

	//Loop through the interviews and policies checking and assigning as needed
	for(Interviews__c interview : Trigger.new){
		for(Policy__c policy : poList ){
			if(policy.Annuitiant_Name__c == interview.Insured_Name__c)
				interview.Policy1__c = policy.Id;
			}
		}
	}
}