You need to sign in to do that
Don't have an account?
Pallav
Trigger : field is not getting updated instantly After Insert. Does you have any idea?
Hi All,
I am facing a issue with the trigger that I have developed.
When ever any new record is inserted then after the insert a custom field get updated. The update section of the trigger get fired up where I have written the code for before upate. Here, the fields does not reflect the value automatically. for ever record after insert I have to manually go and click edit and save to reflect the value.
Below given is the piece of code for the trigger. Please let me know what I am doing wrong with it.
trigger LeadTrigger on Lead (before update, after insert)
{
List<Id> OwnerIds = new List<Id>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
OwnerIds.add(trigger.new[i].OwnerId);
}
Map <Id, Id> contactAcc = new Map <Id, Id>();
for (User u : [Select Id, Contact.AccountId From User where ContactId != null and UserType = 'PowerPartner' and Id in : OwnerIds])
{
contactAcc.put(u.Id, u.Contact.AccountId);
}
Map <Id, Id> CAMs = new Map <Id, Id>();
for (Account acc: [Select Id, OwnerId From Account where Id in : contactAcc.values()])
{
CAMs.put(acc.Id, acc.OwnerId);
}
Map<Id, User> userRecords = new Map<Id, User>([Select Id, UserType from User where Id In : OwnerIds]);
if(trigger.isBefore && trigger.isUpdate)
{
try
{
for(Lead l : trigger.new)
{
for (Integer j = 0; j < Trigger.new.size(); j++)
{
if(userRecords.get(l.OwnerId).UserType!=null && userRecords.get(l.OwnerId).UserType=='PowerPartner')
{
l.CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId));
}
else
{
l.CAM__c= null;
}
}
}
}
catch(Exception e)
{}
}
if(trigger.isAfter && trigger.isInsert)
{
List<Lead> savedLeads = new List<Lead>();
try
{
for(Lead l : trigger.new)
{
for (Integer j = 0; j < Trigger.new.size(); j++)
{
if(userRecords.get(l.OwnerId).UserType=='PowerPartner')
{
Lead newLead = new Lead(Id=l.Id, CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId)));
//l.CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId));
savedLeads.add(newLead);
}
}
}
update savedLeads;
}
catch(Exception e)
{}
}
}
Hoping for a quick and positive response from you guys and thanks in anticipation of your answer.
Thanks and regards
Pallav
I am facing a issue with the trigger that I have developed.
When ever any new record is inserted then after the insert a custom field get updated. The update section of the trigger get fired up where I have written the code for before upate. Here, the fields does not reflect the value automatically. for ever record after insert I have to manually go and click edit and save to reflect the value.
Below given is the piece of code for the trigger. Please let me know what I am doing wrong with it.
trigger LeadTrigger on Lead (before update, after insert)
{
List<Id> OwnerIds = new List<Id>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
OwnerIds.add(trigger.new[i].OwnerId);
}
Map <Id, Id> contactAcc = new Map <Id, Id>();
for (User u : [Select Id, Contact.AccountId From User where ContactId != null and UserType = 'PowerPartner' and Id in : OwnerIds])
{
contactAcc.put(u.Id, u.Contact.AccountId);
}
Map <Id, Id> CAMs = new Map <Id, Id>();
for (Account acc: [Select Id, OwnerId From Account where Id in : contactAcc.values()])
{
CAMs.put(acc.Id, acc.OwnerId);
}
Map<Id, User> userRecords = new Map<Id, User>([Select Id, UserType from User where Id In : OwnerIds]);
if(trigger.isBefore && trigger.isUpdate)
{
try
{
for(Lead l : trigger.new)
{
for (Integer j = 0; j < Trigger.new.size(); j++)
{
if(userRecords.get(l.OwnerId).UserType!=null && userRecords.get(l.OwnerId).UserType=='PowerPartner')
{
l.CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId));
}
else
{
l.CAM__c= null;
}
}
}
}
catch(Exception e)
{}
}
if(trigger.isAfter && trigger.isInsert)
{
List<Lead> savedLeads = new List<Lead>();
try
{
for(Lead l : trigger.new)
{
for (Integer j = 0; j < Trigger.new.size(); j++)
{
if(userRecords.get(l.OwnerId).UserType=='PowerPartner')
{
Lead newLead = new Lead(Id=l.Id, CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId)));
//l.CAM__c=CAMs.get(contactAcc.get(trigger.new[j].OwnerId));
savedLeads.add(newLead);
}
}
}
update savedLeads;
}
catch(Exception e)
{}
}
}
Hoping for a quick and positive response from you guys and thanks in anticipation of your answer.
Thanks and regards
Pallav
I am not sure of the validation rule but I can see nothing on the degub Log in the monitoring section. Does the code look ok to you? update should happen that way right??
Thanks are regards
Pallav
Thanks in anticipation of your answer.
Thanks and regards
Pallav