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
NightShadeNightShade 

Help with a Trigger for a Custom Object

Hi All,

 

I have a trigger scenario below and a small piece of trigger code which I have written and which is not working . If someone could please help me .

 

Thanks and Regards

 

 

Scenario : I have a custom object DMA and it has two fields currently on it ZIp Code and DMA (it is basically a City name). Also on the Account Object I have a DMA field  and the BillingPostalCode is a Standard Field .Now by comparing the Zip Codes between the Account and DMA object , I want to take the DMA field value from DMA obejct and populate the field DMA on the Account Object . I have begun the trigger as follows but I am running into lot of errors .Right now there is no relationship between the objects , I am wondering if I should be creating a field of some type to establish the relationship.Please help.

 

Error : Initial term of field expression must be a concrete SObject: LIST<Account> at line 11 column 6

 

 

 

trigger UpdActDma on DMA__c (After Update , After Insert ) {


 
 List<Account> act  = new List <Account>();
 act= [Select BillingPostalCode, DMA__c from Account];
 for (DMA__c dma :Trigger.new) {
   
    If( dma.Zip_Code__c == act.BillingPostalCode ){
    
     act.DMA__r.DMA__c = dma.DMA__c;   << ----- Line 11
     
     }
 }

}

 

 

Cory CowgillCory Cowgill

You are trying to update a field on through a relationship, which is not a concreate SObject so you can't do this.

 

You need to update instances of the Object type, update those values in those instances, and then perform a update DML statement.

 

What you can do is something like this:

 

 List<Account> act  = new List <Account>();

 List<DMA__c> updateDMAs = new List<DMA__c>():

act= [Select BillingPostalCode, DMA__c from Account];
 for (DMA__c dma :Trigger.new) {
   
    If( dma.Zip_Code__c == act.BillingPostalCode ){
     DMA__c updateDMA = new DMA__c();

     updateDMA.Id = act.DMA__c;

     updateDMA.DMA__c = dma.DMA__c;
     updateDMAs.add(updateDMA);
     
     }
 }

 if(updateDMAs.size() > 0)

{

 try{ update updateDMAs;}Catch(Exception e){System.debug(LoggingLevel.ERROR,'Add Error Handling Here....');}

}

Cory CowgillCory Cowgill

BTW I just freehanded that so the syntax might not be 100% but should be good enough to get you moving along.

NightShadeNightShade

Hi Cory ,

 

I wrote my code as below just one update the Api name for DMA field on Account is DMAValue__c . I am still getting the same error as below . Can you please help me since I am not very good at apex these days .

 

Regards

 

 

Error : Initial term of field expression must be a concrete SObject: LIST<Account> at line 14 column 7

 

 

 

trigger UpdActDma on DMA__c (After Update , After Insert ) {


 List<Account> act  = new List <Account>();

 List<DMA__c> updateDMAs = new List<DMA__c>();

act= [Select BillingPostalCode, DMAValue__c from Account];
 for (DMA__c dma :Trigger.new) {
   
    If( dma.Zip_Code__c == act.BillingPostalCode ){
     DMA__c updateDMA = new DMA__c();

     updateDMA.Id = act.DMAValue__c;    <<------ Here is where the error points to.

     updateDMA.DMA__c = dma.DMA__c;
     updateDMAs.add(updateDMA);
     
     }
 }

 if(updateDMAs.size() > 0)

{

 try{ update updateDMAs;}Catch(Exception e){System.debug(LoggingLevel.ERROR,'Add Error Handling Here....');}

}

}

forecast_is_cloudyforecast_is_cloudy

NightShade - can you plese explain your use case in more detail? Do you need to update all Account records with matching Zip Codes every time that a DMA record is updated or inserted? Or is the reverse - i.e every time that an Account Zip Code is updated, you want to update the DMA field on the Account based on some data in a custom object (DMA__c)? 

bob_buzzardbob_buzzard

I don't think you can specify the id of an sobject in that way - try changing it to use the constructor, e.g.

 

 

DMA__c updateDMA = new DMA__c(id=act.DMAValue__c);

 

 

forecast_is_cloudyforecast_is_cloudy

The Id field on any object is not updatable/creatable. It is assigned automatically by the system every time that a new record is inserted and so there is no way to manually set the value of the Id field.

Cory CowgillCory Cowgill

Forecast_is_cloudy is right. You can't update an ID field in an SObject instance.

 

When I freetyped out that response above I forgot this, but the point of my post is you need to have a concrete SObject to peform your updates on.

 

Forecast_is_cloudy is also right, your use case is not clear, please elaborate on what your trying to accomplish.

 

 

bob_buzzardbob_buzzard

 


forecast_is_cloudy wrote:

The Id field on any object is not updatable/creatable. It is assigned automatically by the system every time that a new record is inserted and so there is no way to manually set the value of the Id field.


 

This isn't this case though.

 

I have a visualforce page that allows me to edit an account and related contacts.  Part of the functionality allows me to click a contact to delete it.  The contact id gets passed back to the controller from the page and the delete works as follows:

 

          if (null!=chosenContactId)
          {
          	  Contact cont=new Contact(Id=chosenContactId);
                  delete cont;
          }

 I've just retested this and it works.