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
PallavPallav 

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
werewolfwerewolf
There may be a validation rule or something that is preventing your trigger from having the desired effect.  Have you tried debugging it by going to Setup->Monitoring and turning on a Debug Log?
PallavPallav
Thanks for the response.

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
PallavPallav
This trigger is not updating only in the case of inserting records using Data Loader. Else it is working fine. What is wrong with the code??

Thanks in anticipation of your answer.

Thanks and regards
Pallav

werewolfwerewolf
Well I don't know what exactly your code is supposed to do so I have no idea whether it's right or not.  Try those debug logs.  Nothing will show up in the logs until you create a new one, so go to that page and hit the New button, then try loading some stuff in the data loader to reproduce the issue.  Then you'll see a log in there and you can trace through and see what exactly is happening.