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
Matt McCortneyMatt McCortney 

Trigger that creates Location Record using shipping address from Account Record when the account is created

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;                        
        }
    }
Best Answer chosen by Matt McCortney
Niket SFNiket SF
Check in your schema if you have "Location" Object. If not then you have to create and then you can push the data in to that object. Basically without table you can not inset data which is your primary goal. (Need to creates a Location Record)

All Answers

Niket SFNiket SF
Will you please check "Location" is standard object ? I think it may be custom object "Location__c" check the API name. 
Matt McCortneyMatt McCortney
The Location object is custom. Is there any way around that?
Niket SFNiket SF
Check in your schema if you have "Location" Object. If not then you have to create and then you can push the data in to that object. Basically without table you can not inset data which is your primary goal. (Need to creates a Location Record)
This was selected as the best answer
Matt McCortneyMatt McCortney
Thanks for the help. I found the problem. One of the other developers released a new changeset that I hadn't deployed yet, so that fixed the error.