+ Start a Discussion
Matt McCortneyMatt McCortney 

Error when trigger attempts to create Location Record

Hi, I'm writing a trigger that creates a new Location Record using the shipping information from an Account after the Account is created. I'm having a strange problem where there's an error thrown complaining that the Account Name (a required field for creating a new Location Record) is an invalid ID, even though I get no such problem when manually creating the Location Record from the related list on the Account page. 

Here's my code:

trigger SetsLocationAddress on Account (after insert) {

    List <PSI_Location__c> locationToInsert = new List <PSI_Location__c>();

    for(Account accountInLoop : Trigger.new )
    {
        //tests if account is type Customer
        if( accountInLoop.salesReach__Account_Type__c == 'Customer - Partner' || accountInLoop.salesReach__Account_Type__c == 'Customer - Direct')
        {
            //creates new location record based on shipping address on account record 
            PSI_Location__c newLocation = new PSI_Location__c();
                       
            locationToInsert.add(new PSI_Location__c(
            Name = 'Location 1',
            Account__c = accountInLoop.Name, 
            Street_Address__c = accountInLoop.salesReach__Address__c, 
            City__c = accountInLoop.salesReach__City__c, 
            State__c = accountInLoop.salesReach__State__c, 
            Zip_Code__c = accountInLoop.salesReach__ZIP__c));  
                                 
        }
    }
        insert locationToInsert;
}


and here's my error message when I try to create a new Account (to test if my trigger works):

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SetsLocationAddress caused an unexpected exception, contact your administrator: SetsLocationAddress: execution of AfterUpdate caused by: System.StringException: Invalid id: test2: Trigger.SetsLocationAddress: line 13, column 1


So you can see that my error is coming from my line of code "Account__c = accountInLoop.Name," that is trying to fill one of the required fields to create the location record. As I mentioned before, if I try to manually create a location record, I don't have any error creating one no matter what I put for the Account field. So I thought of just throwing out that line of code, but then I get this error:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SetsLocationAddress caused an unexpected exception, contact your administrator: SetsLocationAddress: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account]: [Account]: Trigger.SetsLocationAddress: line 23, column 1

So I'm pretty confused as to what's going on. I think that it needs to reference the account's id and not the name, but I don't know how to do that since ideally the trigger will fire once the new account is created.

Any help is greatly appreciated and thanks in advance!


 
Matt McCortneyMatt McCortney
I also tried making the trigger work "after update" since the trigger should ideally be able to reference the account's ID since it already exists but it's still not working.
Matt McCortneyMatt McCortney
PS, I tried to delete the Account Name from the Location Object, but it is labeld as Master Detail(Account) so I can't delete it without messing up the relationships.
Matt McCortneyMatt McCortney
I found my problem. It turns out I needed to reference the account's name as Account__c = accountInLoop.Id and not Account__c = accountInLoop.Name. If anyone has a similar problem, I posted my final code below:

trigger SetsLocationAddress on Account (after insert) {

    List<PSI_Location__c> locationToInsert = new List<PSI_Location__c>();

    for(Account accountInLoop : Trigger.new )
    {
        //tests if account is type Customer
        if( accountInLoop.salesReach__Account_Type__c == 'Customer - Partner' || accountInLoop.salesReach__Account_Type__c == 'Customer - Direct')
        {
            //creates new location record based on shipping address on account record 
            locationToInsert.add(new PSI_Location__c(
            Name = 'Location 1',
            Account__c = accountInLoop.Id, 
            Street_Address__c = accountInLoop.salesReach__Address__c, 
            City__c = accountInLoop.salesReach__City__c, 
            State__c = accountInLoop.salesReach__State__c, 
            Zip_Code__c = accountInLoop.salesReach__ZIP__c));  
              
              
        }
    }
    
    insert locationToInsert;
}