You need to sign in to do that
Don't have an account?
Tillu
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;
}
}
}
}
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;
}
}
}
}
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
(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());
}
}
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
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
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
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;
}
}
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
}
}
}
}
}
for(Policy__c p: po)