You need to sign in to do that
Don't have an account?
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
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
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; }
you are creating a map of accoun id's and you arent querying for account id... and hence the error
It worked Perfect!.
John, can you let me know where i was going wrong.
Thanks,
Sales4ce
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
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
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!
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.
I am trying to do the same, but John's code does not work for me:
I get
My code is
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
Hi Julian
Please check the name of field negativesurvey.
This field is not exist on related object that's why it is throwing error.