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
Nageswara  reddyNageswara reddy 

Trigger Error

HI All,

 

  I  have written trigger  on School_del__c, that  will be updated phone number filed in acoount. but Now  I am  creating   a record in School_del__c  i am getting errormeassage like   

 

""Apex trigger UpdatephoneNumber caused an unexpected exception, contact your administrator: UpdatephoneNumber: execution of AfterInsert caused by: System.StringException: Invalid id: 467894: Trigger.UpdatephoneNumber: line 6, column 1"

 

 the  trigger code is

 

 

 

 

trigger UpdatephoneNumber on School_del__c (after insert ,after update) {

set<id> ss=new set<id>();
for(School_del__c sc:trigger.new) {
if(sc.PhoneNumber__c == null || sc.PhoneNumber__c != null) {
ss.add(sc.PhoneNumber__c );
}
}

map<id, Account> app=new map<id, Account>();
List<Account> accountsToUpdate = new List<Account>();

for(account a:[select id,phone from account where id in:ss limit 50000])
app.put(a.id, a);

for(School_del__c sc:trigger.new) {
if(sc.PhoneNumber__c == null ||sc.PhoneNumber__c != null) {
Account acc = app.get(sc.PhoneNumber__c );
acc.phone = sc.PhoneNumber__c;
accountsToUpdate.add(acc);
}
}

if(!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}

 

  any one can help to solve this error

 

 

Thanks

Nageswara Reddy.

Navatar_DbSupNavatar_DbSup

Hi,

 

You are making query on Account. So inside the where clause you are checking the id field with a list which store the phone number. So you have to make the below changes:

 

set<string> ss=new set<string>(); and

for(account a:[select id,phone from account where phone  in:ss limit 50000])

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SRKSRK

U create a set of ID i.e            "set<id> ss=new set<id>();"

 

Then u put phone number in it

ss.add(sc.PhoneNumber__c );   

 

// i belive PhoneNumber__c fields dose not have ID of account??

 

& the u pass these phoneset in to Id in select query

for(account a:[select id,phone from account where id in:ss limit 50000])

 

 

& if PhoneNumber__c field have the account id
then at line number 19 u write
acc.phone = sc.PhoneNumber__c;

 

 

& if u updating or inseritng any record in trigger i perfer u use before trigger

SRKSRK

I belive this is what u want

u have a lookup of account in   School_del__c 

& when user insert the School_del__c rec u want to update the parent record phone number field with the phone number fields at child ???

If yes just let me know i give u a simple code

Nageswara  reddyNageswara reddy

HI

 

 Yes  I   Have already relationship with   between them ,  Now i want   update phone(Filel)d of with my custom object  Phone number .

 

 

 

Thanks Nageswara Reddy

Nageswara  reddyNageswara reddy

Hi ankit,

 

 I ahve done with u r modification , but  still I am  getting  same error.,  do u provide any  alternate solution for that

 

 

Thanks,

 

Nageswara

SRKSRK

I belive this is what u want

u have a lookup of account in   School_del__c 

& when user insert the School_del__c rec u want to update the parent record phone number field with the phone number fields at child ???

If yes just let me know i give u a simple code