I'm trying nothing more than to update a lookup field when I create a new record. The field it should pull the value from is a lookup.
I have this code:
List <Job__c> opListInsert = new List<Job__c>();
List <Job__c> opListUpdate = new List<Job__c>();
if(trigger.isInsert){
for(Job__c op:trigger.New){
if(op.Acct__c != Null){
op.Account__c = op.Acct__c;
opListInsert.add(op); }
}
}
else if(trigger.isUpdate){
for(Job__c op:trigger.New){
if(op.Acct__c != Null && op.Acct__c !=trigger.oldMap.get(op.id).Acct__c){
op.Account__c = op.Acct__c;
opListUpdate.add(op);
}
}
}
But it's throwing the following error:
Error:Apex trigger UpdateAccounts caused an unexpected exception, contact your administrator: UpdateAccounts: execution of BeforeUpdate caused by: System.StringException: Invalid id: accountname : Trigger.UpdateAccounts: line 20, column 1
Anyone know how I can convert the account to string or vice versa?
Update
Thanks for the help, don't know why I figured it would add the string not the ID.
Here is my updated trigger that works (might not be the cleanest option):
trigger UpdateAcct on Job__c (before insert, before update) {
List<String> Accounts = new List<String>();
for (Job__c obj: trigger.new){
Accounts.add(obj.Acct__c);
}
list<Account> acctlist = [select Name from account where Name in :Accounts];
if (acctlist.size() > 0 ){
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].Acct__c != null){
Trigger.new[i].Account__c = acctlist[i].ID;
}
else{
Trigger.new[i].Account__c = null;
}
}
}
}