Skip to main content
deleted 22 characters in body
Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

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 *

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
        else{
        Trigger.new[i].Account__c = null;
      }
    }
  }
}

} }

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;
        }
}

} }

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;
      }
    }
  }
}
updated trigger
Source Link
gato negro
  • 315
  • 3
  • 12

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;
        }
}

} }

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?

Thanks.

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;
        }
}

} }

Tweeted twitter.com/#!/StackSalesforce/status/337388180623982592
Source Link
gato negro
  • 315
  • 3
  • 12

Typecast a string - trigger to pass value to lookup from picklist

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?

Thanks.