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 correct this below trigger ?

I have 2 objects   Policy , Interview. On interview record policy lookup is there.  If interview fields Contact name  matches with policy field contact name and Interview field Insured name matches with any policy record's Annuity name Then it should pull the policy on to the Interview record on  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());
}
}
logontokartiklogontokartik
Please try below

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 ];
      for(policy__c p : po){
          policyConMap.put(p.Contact__c,p);
      }


    try{
         for(Interviews__c Inr : Trigger.new){
           if(policyConMap.containsKey(Inr.Contact__c)){
              if(Inr.Annuitant_Name__c ==  policyConMap.get(Inr.Contact__c).Annuitiant_Name__c){
                 Inr.Policy1__c = policyConMap.get(Inr.Contact__c).Id
              }
           }
          }
}catch(Exception e){
   System.Debug(e.getMessage());
   }
}


Jen BennettJen Bennett
Try to toss in a couple system debug messages to check your if statement, make sure you are entering it.