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 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;
}
}
}
}
Best Answer chosen by Tillu
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
for(Interviews__c Intr : Trigger.new)
{
List<policy__c> po = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from
Policy__c where Contact__c=:Intr.contact__C];
If(po.size()>0)
{
For(i=0;i<po.size();i++)
{
if(po[i].Contact__c == Intr.Contact__c && po[i].Annuitiant_Name__c==Intr.Insured_Name__c)
{
Intr.Policy1__c = po[i].Id;
\\add other fields common for both objects to be updated
}
}
}
}
}

All Answers

TilluTillu
trigger Interview_PolicyUpdate on Interviews__c

(before insert,before update) {

set<id> cset = new set<id>();
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());
}
}
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Try this code
trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
for(Interviews__c Intr : Trigger.new)
{
List<policy__c> po = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from
Policy__c where Contact__c=:Intr.contact__C];
If(po.size()>0)
{
For(i=0;i<po.size();i++)
{
for(Policy__c p : po)
{
if(p.Contact__c == Intr.Contact__c && p.Annuitiant_Name__c==Intr.Insured_Name__c)
{
Intr.Policy1__c = p.Id;
\\add other fields common for both objects to be updated 
update Intr;
}
}
}
}
}
}
*This code will work like this.if you insert a record on interview object it will check the policy object and if contact name and annuitiant name match it will update the interview policy id and other fields.if it doesnot work let me know
TilluTillu
I am getting exception for Old Records. I am unable to update record.



Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Interview_PolicyUpdate caused an unexpected exception, contact your administrator: Interview_PolicyUpdate: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.Interview_PolicyUpdate: line 16, column 1
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Remove the update intr line
TilluTillu
yes i have already tried . but updation is not taking place
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
can you paste your code what error you are getting?
TilluTillu
after removing update Intr...i am not getting any error.   Old record is saved but there is no policy record population. there is no update
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
for(Interviews__c Intr : Trigger.new)
{
List<policy__c> po = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from
Policy__c where Contact__c=:Intr.contact__C];
If(po.size()>0)
{
For(i=0;i<po.size();i++)
{
if(p[i].Contact__c == Intr.Contact__c && p[i].Annuitiant_Name__c==Intr.Insured_Name__c)
{
Intr.Policy1__c = p[i].Id;
\\add other fields common for both objects to be updated
}
}
}
}
}
If record doesnot match only contact id and insuredname will be inserted in interview object
TilluTillu
I have modified this with For(Policy__c  p : po)  ...because it was giving error. no variable p.  But  i am getting other error 

Error: Compile Error: Loop variable must be of type SOBJECT:Policy__c at line 10 column 17

For(Policy__c[] p : po){  // Error
if(p[i].Contact__c == Intr.Contact__c && p[i].Annuitiant_Name__c==Intr.Insured_Name__c)
{
Intr.Policy1__c = p[i].Id;
}
}
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger Interview_PolicyUpdate on Interviews__c(before insert,before update) {
for(Interviews__c Intr : Trigger.new)
{
List<policy__c> po = [select Id,name,Contact__c,Type__c,Annuitiant_Name__c from
Policy__c where Contact__c=:Intr.contact__C];
If(po.size()>0)
{
For(i=0;i<po.size();i++)
{
if(po[i].Contact__c == Intr.Contact__c && po[i].Annuitiant_Name__c==Intr.Insured_Name__c)
{
Intr.Policy1__c = po[i].Id;
\\add other fields common for both objects to be updated
}
}
}
}
}
This was selected as the best answer
NekosanNekosan
Try using 
for(Policy__c p: po)