You need to sign in to do that
Don't have an account?
Need help with trigger to autopopulate lookup field on oppty
Hi guys,
I never wrote a trigger myself but I need one now, so I searched the internet and have found what I want but for some reason it doesn't work for me. I hope you can help me with this.
Here is the situation:
New custom Object: XtendedUser__c
This object is linked with a lookupfield to a SF-user by the field Linked_to_User__c
On the opportunity i created a new lookupfield called XtendedUser__c wich has to link to a record of the custom object XtendedUser__c where the opportunityownerid = the id of the user in Linked_to_User__c of the custom object.
This is the code so far:
trigger PopulateXtendedUser on Opportunity (before update, before insert) {
//Set of XtendedUser Ids
Set<Id> XtendedUserIds = new Set<Id>();
for (Opportunity opportunityNew : Trigger.new) {
XtendedUserIds.add(opportunityNew.XtendedUser__c); //A set of XtendedUsers
}
Map<Id, XtendedUser__c> pXtendedUsers = new Map<Id, XtendedUser__c>([SELECT a.id, a.linked_to_User__c, a.Name FROM XtendedUser__c a WHERE ownerid IN :XtendedUserIds]);
for(Opportunity fcon : Trigger.New){
system.debug('Going to update OpptyXtendedUserlookupfield');
if(pXtendedUsers != null && pXtendedUsers.get(fcon.XtendedUser__c) != null)
fcon.XtendedUser__c = pXtendedUsers.get(fcon.XtendedUser__c).Id;
System.debug('OpptyXtendedUserfield should be updated');
}
}
The code saves so no error, but the field on Oppty doesn't autopopulate.
Can someone help me a bit please?
Hi all,
thanks for your help. But sorry none of the above did the trick. After some searching and rewriting follwing does the trick perfectly:
All Answers
Can you try this type of code, you have to replace that,
Map<Id,XtendedUser__c> pXtendedUsers=new Map<Id,XtendedUser__c>([select a.id,a.linked_to_User__c, a.Name from XtendedUser__c a where linked_to_User___c IN : XtendedUserIds]);
I created an XtendedUser object record called A (recordid: BBBB) with a lookupfield called Linked_to_Userkup with value Carl(Carl s id is CCCC)
Now when i create an opportunity and the opportunityowner is also Carl I want another custom lookupfield on the Opportunity to automatically be filled with the id of the XtendedUser record where Carl is attached as Linked_to_User. So the id to be filled in should be BBBB on the opportunity.
Hope this clears it up?
Hi jvandeve,
Based on your requirement, if an opportunity is created with XtendedUser__c field lookup not null, you need to get the User lookup field linked_to_User__c to become the owner of opportunity. Isn't it?
Is it so, Try the following:
trigger PopulateXtendedUser on Opportunity (before update, before insert) {
//Set of XtendedUser Ids
Set<Id> XtendedUserIds = new Set<Id>();
for (Opportunity opportunityNew : Trigger.new)
{
XtendedUserIds.add(opportunityNew.XtendedUser__c);
//get the list for xtendeduser lookup id
List<XtendedUser__c> pXtendedUsers = [SELECT id, Name, linked_to_User__c FROM XtendedUser__c WHERE id IN : XtendedUserIds];
if(pXtendedUsers.size() > 0 && pXtendedUsers[0].linked_to_User__c != null)
{
opportunityNew.ownerID = pXtendedUsers[0].linked_to_User__c;
}
}
}
Hope so this helps you...!
Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.
Hi all,
thanks for your help. But sorry none of the above did the trick. After some searching and rewriting follwing does the trick perfectly: