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
sales4cesales4ce 

Update a field in Standard object when there is a change in a field in Custom Object--Help needed!

Hi,

 

I am trying to update a field value on standard object after there has been a change on a custom object field.

 

The standard Object is Account and custom object is  Accountsinaction(Account_in_action__c).

Both objects are related via a Lookup relationship. The field is Account__c on Custom Object.

 

Also, the custom object has another field called Account_Status__c. If there is a change to this value, a trigger has to fire and update the change in corresponding Standard Account Object.

 

Trigger Code so far:

 

trigger Update_Account_Status on Account_In_Action__c (after Update) 
{
      List<Account> accts_toUpdate=new List<Account>();
      Set<Id> accsId =new Set<Id>();
      
      for(Account_In_Action__c ia : Trigger.New)
          accsId.add(ia.Account__c);
          
      Map<Id,Account> AccountMap=new Map<Id,Account>([Select Status from Account where Id in :accsId]);
      List<Account_In_Action__c> account_Action= [select Account_Status__c from Account_In_Action__c where id in :Trigger.new];
      
      for(Integer i=0;i<account_Action.size();i++)
      {
        Account toupdate=new Account();
        accountMap.get(account_Action[i].idea__c).Status=account_Action[i].account_status__c;
        accts_toUpdate.add(toupdate);
      }
      
      if(accts_toUpdate.size()>0)
          Update accts_toUpdate;
}

 It gives following error msg:

 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account_In_Action__c.Account__c: Trigger.Update_Account_Status:

 

Can anyone help me with this.

 

Thanks in advance !

 

sales4ce

 

Best Answer chosen by Admin (Salesforce Developers) 
jhenningjhenning

Try this one and let me know if it works.

 

 

trigger Update_Account_Status on Account_In_Action__c (after Update) { List<Account> accts_toUpdate = new List<Account>(); for(List<Account_In_Action__c> account_Actions:[select account_status__c, account__r.Status from Account_In_Action__c where id IN :Trigger.newMap.keySet()]){ for(Account_In_Action__c act:account_Actions){ act.account__r.Status = account_status__c; accts_toUpdate.add(act.account__r); } } if(accts_toUpdate.size()>0) update accts_toUpdate; }

 

 

 

All Answers

jhenningjhenning

Try this one and let me know if it works.

 

 

trigger Update_Account_Status on Account_In_Action__c (after Update) { List<Account> accts_toUpdate = new List<Account>(); for(List<Account_In_Action__c> account_Actions:[select account_status__c, account__r.Status from Account_In_Action__c where id IN :Trigger.newMap.keySet()]){ for(Account_In_Action__c act:account_Actions){ act.account__r.Status = account_status__c; accts_toUpdate.add(act.account__r); } } if(accts_toUpdate.size()>0) update accts_toUpdate; }

 

 

 

This was selected as the best answer
Shailesh DeshpandeShailesh Deshpande

you are creating a map of accoun id's and you arent querying for account id... and hence the error

 


      Map<Id,Account> AccountMap=new Map<Id,Account>([Select Id,Status from Account where Id in :accsId]);

 

 

Message Edited by Shailesh on 03-31-2010 01:01 AM
sales4cesales4ce

It worked Perfect!.

John, can you let me know where i was going wrong.

 

Thanks,

Sales4ce

sales4cesales4ce

Thanks Shailesh for the help!

when i changed the query to include Id it did compile successfully and did not raise any run time exceptions.

But, somehow the update was not happening with the code i have written.

 

Can you let me know the what was going wrong with my code. I am new to apex and trying to learn.

 

Any help on this would be great!

 

Thanks,

Sales4ce

sales4cesales4ce

John & Shailesh,

 

I have a question regarding the SOQL query.

 

When ever we create a relationship between two objects using a field on child, we need to use the relationshipname in the query to get the fields from other object.

 

Now, for instance assume that i have a Custom object (Abc__c) and a custom field (xyz__c).

Now i use xyz to relate to a standard object (Account) using a look up or master-detail.

 

Now, does the query change to get field values from Account for both look up and master-detail?

Assume that Xyz custom field has a relationship name of (xyzconnect).

 

so finally the query looks like:

 

select id, xyzconnect__r.Type from abc__c;

 

when i use Apex Explorer to run this query it says, it didn't understand the relationship.

 

Can you clarify me on this.

 

Thanks,

Sales4ce

jhenningjhenning

Hi sales4ce:

 

Glad you got it working! I would recommend some reading:

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

 

http://wiki.developerforce.com/index.php/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com

 

Happy Force.com coding!

 

abhyaabhya

Can we use the same code for bulk update of fields when there is an update in the custom objects to the standard object such as Contacts.

 

Julian Juez AlfaroJulian Juez Alfaro

I am trying to do the same, but John's code does not work for me:

 

I get 

ErrorError: Compile Error: Variable does not exist: negativesurvey__c at line 8 column 81

 

My code is 

trigger Update_Account_negative_survey on TIMBASURVEYS__Survey_Summary__c (after Update) 
{
    List<Account> accts_toUpdate = new List<Account>();
 
    for(List<TIMBASURVEYS__Survey_Summary__c> Survey_Summary:[select TIMBASURVEYS__Total_Weight__c, TIMBASURVEYS__RelatedAccount__r.negativesurvey__c
            from TIMBASURVEYS__Survey_Summary__c where id IN :Trigger.newMap.keySet()]){
        for(TIMBASURVEYS__Total_Weight__c act:Survey_Summary){
            act.TIMBASURVEYS__RelatedAccount__r.negativesurvey__c = TIMBASURVEYS__Total_Weight__c;
            accts_toUpdate.add(act.TIMBASURVEYS__RelatedAccount__r);
        }
    }
      if(accts_toUpdate.size()>0)
          update accts_toUpdate;
}

 where:

my custom object is called "Survey Summary" (Object name = "Survey_Summary", API name = "TIMBASURVEYS__Survey_Summary__c")

 

The custom field in the custom object is called "Total Weight" (Field Name = "Total_Weight", API name = "TIMBASURVEYS__Total_Weight__c")

 

The custom field in the Account object is called "negativesurvey" (API name = "negativesurvey__c")

 

Why does it work for you and not for me?

 

Thank you!

Julian

 

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Julian

Please check the name of field negativesurvey.

This field is not exist on related object that's why it is throwing error.