• Matt McCortney
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
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!


 
Hello, I'm trying to make a trigger that A) creates a Location Record (named Location 1) when a new Account Record is made, and B) automatically populates the new Location Record's address information using the new Account Record's shipping information. The problem that I seem to be having is that there's an error thrown when I try to compile complaining that the variables that I used for the Location object do not exist, when in fact they do. I think the problem is that there is some kind of difference between creating a location for the account and creating a location record, since the user is not asked their address when a location is added to the account. Thanks in advance!

trigger SetsLocationAddress on Account (after insert) {
    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 
            Location newLocation = new Location();
                       
            newLocation.salesReach__Location__c = 'Location 1';
            newLocation.salesReach__Street__c = accountInLoop.salesReach__Address__c;
            newLocation.salesReach__City__c = accountInLoop.salesReach__City__c;
            newLocation.salesReach__State__c = accountInLoop.salesReach__State__c;
            newLocation.salesReach__Zip_Code__c = accountInLoop.salesReach__ZIP__c;                        
        }
    }
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!


 
Hello, I'm trying to make a trigger that A) creates a Location Record (named Location 1) when a new Account Record is made, and B) automatically populates the new Location Record's address information using the new Account Record's shipping information. The problem that I seem to be having is that there's an error thrown when I try to compile complaining that the variables that I used for the Location object do not exist, when in fact they do. I think the problem is that there is some kind of difference between creating a location for the account and creating a location record, since the user is not asked their address when a location is added to the account. Thanks in advance!

trigger SetsLocationAddress on Account (after insert) {
    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 
            Location newLocation = new Location();
                       
            newLocation.salesReach__Location__c = 'Location 1';
            newLocation.salesReach__Street__c = accountInLoop.salesReach__Address__c;
            newLocation.salesReach__City__c = accountInLoop.salesReach__City__c;
            newLocation.salesReach__State__c = accountInLoop.salesReach__State__c;
            newLocation.salesReach__Zip_Code__c = accountInLoop.salesReach__ZIP__c;                        
        }
    }