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
sultansultan 

can anybody Resolve this code getting :Error: Compile Error: Variable does not exist: id at line 7 column 10

Trigger updatecontact on Account(after insert,after update)
{
set<id> set1=new set<id>();
list<contact> aconlist=new list<contact>();
for(account a:Trigger.new)
{
set1.add(a.id);
}
list<contact> conlist=[select id,name,accountid from contact where accountid in:set1];

for(account d:Trigger.new)
{
for(contact c:conlist)
{
if(d.id==c.accountid)
{
d.industry=c.department;
aconlist.add(c);
}
}
}
update aconlist;
}
Shyam BhundiaShyam Bhundia
I tired your code in my org and it compiles fine.

I've re-writen your code with some improvments.  Please find it below with comments.
//you need a before insert/update not after insert/update because on after the account record is locked
Trigger updatecontact on Account(before insert,before update){

    //you can use the trigger.new in the where clause of soql. 
    //Note if included the department field, as you need it below
    list<contact> conlist=[select id,name,accountid, Department  from contact where accountid in : Trigger.new];
   
    //you can get rid of the account for loop and use the newmap instead
    for(contact c: conlist){
        Trigger.newMap.get(c.accountid).industry = c.department;
    }
}

I've also removed the aconlist contact list and the update because as you are not making an changes to the contact, there is no point updating it.
sultansultan
Hi shyam,

while updating a record I am getting this error on Account user interface.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatecontact caused an unexpected exception, contact your administrator: updatecontact: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.updatecontact: line 17, column 1
Shyam BhundiaShyam Bhundia
You need to have your logic on the before trigger not after, the account record is locked on after events..  

Have a look at the code i posted above.
sultansultan
Hi shyam,

I Tried you code but I am getting this error while insertion on Account user interface

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatecontact caused an unexpected exception, contact your administrator: updatecontact: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updatecontact: line 17, column 1

Shyam BhundiaShyam Bhundia
Can you post your code here please. As on my example there is no line 17.

Cheers

sultansultan
Hi shyam,

Have a look at this code:

Trigger updatecontact on Account(before insert,before update){
list<contact> conlist=[select id,name,accountid, Department  from contact where accountid in : Trigger.new];
for(contact c: conlist){
Trigger.newMap.get(c.accountid).industry = c.department;
}
}
sultansultan
Hi shyam,

plz check the code:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatecontact caused an unexpected exception, contact your administrator: updatecontact: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updatecontact: line 4, column 1
Shyam BhundiaShyam Bhundia
try this:
Trigger updatecontact on Account(before insert,before update){

//you can use the trigger.new in the where clause of soql. 
//Note if included the department field, as you need it below
list<contact> conlist=[select id,name,accountid, Department  from contact where accountid in : Trigger.new];

for(contact c: conlist){
  if(null != Trigger.newMap.get(c.accountid)){
      Trigger.newMap.get(c.accountid).industry=c.department;
  }
}
}


sultansultan
Hi shyam,

Same Error I am getting again and again after tried you sent the last code
Shyam BhundiaShyam Bhundia
interesting, I tried exactly the same code and it works.  Have you got any other triggers on the account object?
sultansultan
I Written some Triggers on Account object but I have de-activated all Triggers which I written in past.

But still I am getting this Error .what is the reason behind this? 
Shyam BhundiaShyam Bhundia
try this....comment out the whole for loop, including its content and try again.  This wont make your logic work, but it will help in debuging.
sultansultan
Thanks shyam .....
Shyam BhundiaShyam Bhundia
did you manage to get it working?
sultansultan
I am still  Trying  shyam but it  is not being resolved
Shyam BhundiaShyam Bhundia
ive put some further comments on my code below, this may help clear things up...

Trigger updatecontact on Account(before insert,before update){

	//you can use the trigger.new in the where clause of soql.  
	//Note if included the department field, as you need it below
	list<contact> conlist=[select id,name,accountid, Department  from contact where accountid in : Trigger.new];

	//ConList will never be null it will either have some elements or none
	for(contact c: conlist){
		//trigger.newMap will never be null on a insert or update trigger, so that can't be the issue
		//the trigger map will always contain the account associated with the contact because of the query used,
		//but we still put a null check just incase
		if(null != Trigger.newMap.get(c.accountid)){
			//the only thing we are doing here is that we are getting the contact from the list,
			//getting its department and assigning it to the industry.  On this line the contact (c) cannot be null
			//and if the department is null, it will just assign null to the industry field on the account	
	  	  Trigger.newMap.get(c.accountid).industry=c.department;
		}
	}
}