You need to sign in to do that
Don't have an account?

how to Updating field of the same object record in after insert?
Hi All,
I need some help. In my custom object i have one formula field it is displaying user name based on value in the pick list value. Based on the picklist value the user value is displaying in one field for that created formula field.
I created one look up field(User) in my object now i want to populate that field value based on the user name retrived in the formula field so now i want to update this look up field value based on the formula field for this in after insert have to do this update but salesforce does not allows to update the current record in the after insert event. So can any one help how to acheive this functionality.
Is there any other way to do this. Please help me on this
Thanks in advance.
Hi Kittu try with below code it will work
Regards
Kakarla
Trigger
_________________
trigger UpdateUserName on Employee__c (after insert,after update)
{
if(updation.isfutureupdate!=true)
{
set<id> lst=new set<id>();
for(Employee__c e :Trigger.new)
{
lst.add(e.id);
system.debug('$$$$$$$$$$$'+lst);
}
kakarla.proccedaccounts(lst);
}
}
class1
__________
public class updation
{
public static boolean isfutureupdate=false;
}
class2
____________
public class kakarla
{
//user u;
@future
public static void proccedaccounts(set<id> IDs)
{
list<Employee__c> lstname=new list<Employee__c>();
for(Employee__c a:[select id,name,Account__c from Employee__c where id IN:IDs])
{
system.debug('####################'+ a);
user u=[select Id,Name from User where Name='thulasi alla' limit 1];
a.First_Name__c='Skypower';
lstname.add(a);
}
updation.isfutureupdate=true;
update lstname;
}
}
All Answers
Hi I am not sure what your doing.
But Try use update with @future annotation.
Better post your example to can provide other Solution as well.
You can't modify Trigger.new in an after trigger, nor can you call a DML function on Trigger.new. So, what's a programmer to do? There happens to be a solution for this, and it goes as follows:
This trigger is working fine with out error for the "User__c" field am able to update the value. The following is "Before Insert" trigger
trigger UpdateUserName on Employee1__c (before insert)
{
User u;
for(Employee1__c e :Trigger.new)
{
if(Trigger.isBefore && Trigger.isInsert)
{
u = [select Id,Name from User where Name='Test User'];
System.debug('User Name '+u);
e.User__c = u.Id;
}
}
}
But In the following example in the SOQL query i given user name as "Test User". Actually this value will get based on one formula field in my object.I want to use those formula field value to update another field in the same object for that am using "After Insert" Trigger. But in the "After insert" am not able to update the current record so how to achieve this functionality through "After insert" Trigger. I read some where with the help of "@future" annotation. Any one help me on this how to do this. Thanks in advance.
trigger UpdateUserName on Employee1__c (after insert)
{
User u;
for(Employee1__c e :Trigger.new)
{
if(Trigger.isAfter && Trigger.isInsert)
{
u = [select Id,Name from User where Name='Test User'];
System.debug('User Name '+u);
e.buchi__User__c = u.Id;
update e;
}
}
}
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger buchi.UpdateUserName caused an unexpected exception, contact your administrator: buchi.UpdateUserName: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.buchi.UpdateUserName: line 11, column 1
Hi Kittu try with below code it will work
Regards
Kakarla
Trigger
_________________
trigger UpdateUserName on Employee__c (after insert,after update)
{
if(updation.isfutureupdate!=true)
{
set<id> lst=new set<id>();
for(Employee__c e :Trigger.new)
{
lst.add(e.id);
system.debug('$$$$$$$$$$$'+lst);
}
kakarla.proccedaccounts(lst);
}
}
class1
__________
public class updation
{
public static boolean isfutureupdate=false;
}
class2
____________
public class kakarla
{
//user u;
@future
public static void proccedaccounts(set<id> IDs)
{
list<Employee__c> lstname=new list<Employee__c>();
for(Employee__c a:[select id,name,Account__c from Employee__c where id IN:IDs])
{
system.debug('####################'+ a);
user u=[select Id,Name from User where Name='thulasi alla' limit 1];
a.First_Name__c='Skypower';
lstname.add(a);
}
updation.isfutureupdate=true;
update lstname;
}
}
I this the way salesforce people handels this type of situation.
Thanks,
Shiv