You need to sign in to do that
Don't have an account?
pdvapor
Update Lookup in Trigger
Hi All,
I am trying to update a Lookup Field on an Account with a Trigger. Essentially I have a Custom Object (Box_Assumptions_c) related to an Account (Account1) and I want to update an a Custom Field (X9Box_Assumptions__c) on a different Account (Account2) with a Box Assumption that is related to Account1 by using the Owner's (Portal User) Contact Account (Account1).
trigger updatecustomer9box on Account (before insert, before update) { //Get Account ID of the current record. set<id> acctid = new set<id>(); for (account a : trigger.new) acctid.add(a.owner.contact.account.id); map<id, box_assumptions__c> boxmap = new map<id, box_assumptions__c>([select id from box_assumptions__c where account__c in : acctid]); for (account a : trigger.new){ box_assumptions__c thisbox = boxmap.get(a.x9box_assumptions__c); a.x9box_assumptions__c = thisbox.id; } }
I am getting a Null Pointer Exception so any advice would be helpful!
-Thanks in advance!
All Answers
In a before insert, the account object you're inserting doesn't yet have an owner associated with it, that is assigned to the record after the before insert trigger. If you change it to after insert, it should work.
So I made some modifications (including the "after update, after insert" change and now it saves, but is not making the association...any other suggestions?
Can you please Elaborate problem you are facing.
When I update the record with a new Owner, it does not update the x9box_assumptions__c field.
Thank you for your help!
Try this
trigger updatecustomer9box on Account (after insert, after update) {
//Get Account ID of the current record.
set<id> acctid = new set<id>();
account[] updateaccts = new account[]{};
for (account a : trigger.new){
if (a.RecordType.Name == 'CC Client Customer Account'){
acctid.add(a.owner.contact.account.id);
}
}
if (acctid.size() == 0) return;
for ( box_assumptions__c BA : select id from box_assumptions__c where account__c in : acctid){
map<id, box_assumptions__c> boxmap = new map<id,box_assumptions__c>();
boxmap.add(BA.Account__c , BA);
}
//map<id, box_assumptions__c> boxmap = new map<id, box_assumptions__c>([select id from box_assumptions__c where account__c in : acctid]);
for (account a : trigger.new){
a.x9box_assumptions__c = boxmap.get(a.id);
updateaccts.add(a);
}
if(updateaccts.size()>0){
update updateaccts;
}
}
I am getting:
Error: Compile Error: unexpected token: 'select' at line 14 column 33
try this,
trigger updatecustomer9box on Account (after insert, after update) {
//Get Account ID of the current record.
set<id> acctid = new set<id>();
account[] updateaccts = new account[]{};
for (account a : trigger.new){
if (a.RecordType.Name == 'CC Client Customer Account'){
acctid.add(a.owner.contact.account.id);
}
}
if (acctid.size() == 0) return;
for ( box_assumptions__c BA : [select id from box_assumptions__c where account__c in : acctid]){
map<id, box_assumptions__c> boxmap = new map<id,box_assumptions__c>();
boxmap.add(BA.Account__c , BA);
}
//map<id, box_assumptions__c> boxmap = new map<id, box_assumptions__c>([select id from box_assumptions__c where account__c in : acctid]);
for (account a : trigger.new){
a.x9box_assumptions__c = boxmap.get(a.id);
updateaccts.add(a);
}
if(updateaccts.size()>0){
update updateaccts;
}
Now getting
Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Box_Assumptions__c>].add(Id, SOBJECT:Box_Assumptions__c) at line 16 column 9
My bad,
use put instead of add
Hi pdvapor,
I did not get the requirement clearly, but have optimized Prakash's code for you:
Hope it helps. :)
Let us know if you face any issues furthur
Hi Rahul,
You are definitely on the right track, but now I am facing the following error:
So I tried a different approach to this. But now it is not updating anything...can you take a look at this approach?
Steve, I appreciate your help. It is tough to describe, but I will try.
1. I have created a custome object "Box Assumptions" ((Box_Assumptions__c) that has a lookup field to an Account (Account__c), let's call this "Portal Account".
2. I have Partal Users who are related to a Portal Account through their Contact record and are the Owners of their own Accounts. Let's call these "Customer Accounts". You can get to their Account ID by going through this path from their Customer Account - Account > Owner > Contact > Account.
3. I am trying to find the Box Assumptions record that is related to the Portal User's Account (Portal Account) by taking the Portal Account ID and querying for the Box Assumptions record.
4. Then, I want to take the Box Assumptions record ID and associate it to the "Customer Account" through a Lookup field (x9box_assumptions__s) Lon the "Customer Account record.
I hope this helps to explain. I have tried many variations of this, but cannot seem to get it to work.
Really appreciate your help on this!
Note: I'm not going to be surprised if this craps out on this "acc.owner.contact.account.id". Triggers generally have just the fields in the record being insert/updated. I'd be surprised if owner.contact.account.id was actually data that was in the record. Throw a system.debug(acc) into your loop and you'll see the data that's actually in the trigger.new records.
If that's the case, then you're going to have to issue the query yourself to get all that information. -S
And practice using comments... it helps clarify your thinking. :-)
I thought he wanted to update the Account records in the trigger, not the portal accounts. If that's the case, he just needs to do aread-only query to get the portal account id's, but he doesn't need to worry about the recursion, etc. Best, Steve.
Wizrad, this looks pretty amazing! When I try to paste this in, I am getting "Compile Error: unexpected token: public at line 7 column 0".
I'm embarrased...I see, thanks for your help!