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 

unable to update field when match occures for below trigger ?

HI Folks,

I have 2 objects   Policy , Interview. On interview record policy lookup is there.  If interview fields Contact name , Insured name matches with any  policy record's Contactname,Annuity name Then it should pull the policy on to the Interview record policy look up field.

I have writtened below code. But it was not works properly. There is no immediate update for the Policy look up field. Can any one correct below code ?


trigger Interview_PolicyUpdate on Interviews__c

(before insert,before update) {

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

for(Interviews__c Intr : Trigger.new){

If(Intr.Contact__c != Null){
cset.add(Intr.Contact__c);
}

}
List<policy__c> po = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from
Policy__c where Contact__c in:cset ];
try{
for(Interviews__c Inr : Trigger.new){
for(Policy__c p : po){
//If(Inr.Annuitant_Name__c !=null){
if(p.Contact__c == Inr.Contact__c && Inr.Insured_Name__c == p.Annuitiant_Name__c){
Inr.Policy1__c = p.Id;
}
}
}
}
catch(Exception e){
System.Debug(e.getMessage());
}
}
Subhash GarhwalSubhash Garhwal
Try this one
trigger Interview_PolicyUpdate on Interviews__c (before insert,before update) {

set<id> cset = new set<id>();
Map<String, Id> mapConWithPolicies = new Map<String, Id>();

for(Interviews__c Intr : Trigger.new){

If(Intr.Contact__c != Null && Intr.Insured_Name__c != null){
cset.add(Intr.Contact__c);
}

}

//Loop
for(Policy__c po : [Select Id, Contact__c, Name,Type__c,Annuitiant_Name__c From Policy__c Where Contact__c IN : cset]) {
	
	//Check for Annuitiant_Name__c
	if(po.Annuitiant_Name__c != null && !mapConWithPolicies.containsKey(po.Contact__c + '-' + po.Annuitiant_Name__c))
		mapConWithPolicies.put(po.Contact__c + '-' + po.Annuitiant_Name__c, po.Id);
}

for(Interviews__c Inr : Trigger.new){
	
	//Check
	if(Intr.Contact__c != Null && Inr.Insured_Name__c != null && mapConWithPolicies.containsKey(Inr.Contact__c + '-' + Inr.Insured_Name__c))
		Inr.Policy1__c = mapConWithPolicies.get(Inr.Contact__c + '-' + Inr.Insured_Name__c);	
}
	
}

Regards
Subhash
TilluTillu
no , I dont have any immidiate update here.
TilluTillu
While record creation it works good. But  there is no  policy populated on Updation ?