You need to sign in to do that
Don't have an account?

Cross Object Field Update Trigger
I'm just getting into a SF admin seat so I don't have much Apex experience.
I want 2 fields in the opportunities object to be updated once a field in a custom object is updated.
When the text field in Object 1 (the custom object) is entered as complete, I need two seperate fields in the opportunity object updated.
I want status changed to approved and a custom checkbox to go from unchecked to checked.
Thanks in advance
All Answers
Ryan - I'm new to APEX, I'm trying to leverage your example in a cross object update I've been asked to setup. I have a custom object called "Pre-Sales" that is associated to the standard Opportunity object via a master detail relationship.
On the pre-sales object I have a field called "Client Segmentation" (this is a picklist) that I need to copy up to the opportunity into a field called "Pre Sales Client Segmentation" (this is a text field). I have used your example and made a few changes to the field names
I get this error:
Error: Compile Error: Invalid type: Opportunity__c at line 11 column 8
How do I resolve this? Is it because my association is a master/detail relationship and not a lookup? Do I need convert the pickvalue so it can be used in a text field?
Thanks in advance for any help you can provide.
Chris
Replace List<Opportunity__c> with List<Opportunity> at line 8
the __c tag is only applied to custom Objects and Opportunity is a standard Object.
Good catch, I missed that.
Follow up question for you if you have a moment. This what the trigger looks like now:
I'm getting a new error now:
Error: Compile Error: Variable does not exist: o.Client_Segmentation__c at line 12 column 51
I'm not familiar with the correct syntax to in the beginning of the trigger to properly set the variable. Could you please help with that too?
Thanks in advance.
Chris
You cannot use "o" in the location you currently have as "o" is out of context. "o" is only in context within the for loop.
as long as it meets your use case try:
If there are more than one Pre_Sales__c for an Opportunity a different approach will have to be taken. The issue is when inserting / updating more than 1 record at a time you need to make sure you are populating the correct data.
Don´t know that I can just butt in on this but request is very similar to one I have:
I have a custom object linked to accounts - it´s a related list on accounts - lookup relationship to accounts. My question is if there is a way to automatically change the record type of this custom object when I change the record type of the account? I don´t have much experience with triggers but am willing to try. Thank you.
This worked perfectly, thank you for the help!
Hey All,
Thanks for the help with this! I was actually trying to write the reverse of this; custom object updated from the Opp, and finally got it thanks to this thread.
BUT, and there's always a but, I can't find any solid Test class code that I can look at in terms of testing a cross object field update.
I really don't know where to start, any help would be appreciated.
Here is the code if that helps:
Thanks in advance!
Alex Roth
List<ID> OppIds = New List<ID>();
for (Accounts_Payable__c a : Trigger.new){
if (a.Name != null && a.Opportunity_Name__c != null){
OppIds.add(a.Opportunity_Name__c);
}
List<Opportunity> apList = [SELECT id, Record_Type_Name__c, Country_Formula__c FROM Opportunity WHERE id in :OppIds];
for(integer i = 0 ; i < apList.size(); i++){
a.Deal_Type__C = apList[i].Record_Type_Name__c;
a.Country__c = apList[i].Country_Formula__c;
update apList;
}
}
}
I am trying something similar. If the Opportunity Status field equals "Closed Won" then make the Account Status "Current"
I am getting the following error when making the Opp status Closed Won on save:
The account status could be different values and they only time this works is if the value is blank. If it is either Attrition or Prospect it gives the error.
ANy help would be greatly appreciated.
Thanks Sarah
Here is my trigger:
trigger AccountStatus on Opportunity (before insert, before update){
List<ID> OppIds = New List<ID>();
for(Opportunity a : Trigger.new){
if(a.stagename == 'Closed Won' ){
OppIds.add(a.account_status__c);
}
}
List<Account> oppList = [SELECT id, account_status_2__c FROM Account WHERE id in :OppIds];
for(integer i = 0 ; i < oppList.size(); i++){
oppList[i].account_status_2__c = 'Current';
}
update oppList;
}
try:
Thanks That did the trick!
Does anyone have test code for this? I am in need of some.
Thanks
Sarah
We have cross object field update feature in Spring 12 which will be released very soon and I believe lot of such requirements can be done using that. In winter 12 it is only working for Custom-Custom relationship and In Spring 12 we would be able to use this feature for Standard-Custom as well.
Read the Notes-
http://www.salesforce.com/customer-resources/releases/
Hoping this is a small deviation from some of the other posts.
I've created a button in my Case object to create a new Case (of a specific record type) .
I then put that button in the Case related list within the Opportunity object.
When in the resulting Case edit screen, I would like to have the name of the Opportunity that I just created the case from automatically populated into the custom field called Opportunity in my Case object.
Thanks for any help in advance.
Hi all,
i'm also totally new to Apex and am trying to leverage spraetz's template, which has been a huge help so far. (Thanks very much)
What I need is that whenever a custom picklist field called Software__c equals 'Fusion' and the record is updated on a custom object called User_Signature__c, a checkbox is ticked on the associated Contact called Fusion_user.
The code below worked first time, which seems too good to be true, so I'm worried it's executing something I can't see or haven't noticed! Does it look correct?
The only other thing is that after the trigger updates the Fusion_User__c field, you're directed to the Contact. I need the page to stay on the User Signature record updated. How would I do that?
trigger IsFusionUser on User_Signature__c (before insert, before update) {
List<ID> ContactIds = New List<ID>();
for(User_Signature__c o : Trigger.new){
if(o.Software__c == 'Fusion' && o.Contact__c != null){
ContactIds.add(o.Contact__c);
}
}
List<Contact> ContactList = [SELECT id, Fusion_user__c FROM Contact WHERE id in :ContactIds];
for(integer i = 0 ; i < ContactList.size(); i++){
ContactList[i].Fusion_user__c = true;
}
update ContactList;
}
If objecta.field = objectb.field set objecta.field2
Is that possible?
So,
If Ojbecta.field = objectb.field then set objectc.field=objectb.field2.
object a and object c are related via a lookup, but object b has no relation to either.
1. I have a custom lookup field to Contacts records on the Product Object
2. I have a custom email field at the Quote Line Item level
3. Whenever a new Quote Line Item level is created (i.e. Product is added to a quote), I want to pull the email value from the Product's contact to populate the Quote Line Item email field.
I have updated to the code below, but cannot get it to update...any help is greatly appreciated!
Iam Admin not much experience in Apex programming .so I have one cross object of trigger code and fields can u anyone please help me that Issue.
Regards
Raviteja