+ Start a Discussion
jvandevejvandeve 

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?

Best Answer chosen by Admin (Salesforce Developers) 
jvandevejvandeve

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:

 

trigger PopulateXtendedUser on Opportunity (before insert, before update) {
    if (trigger.isBefore) { 
        if (trigger.isInsert || trigger.isUpdate) {
            //list XtendedUsers
            list<XtendedUser__c> lstXtendedUser = [SELECT Id, Name, Linked_to_User__c FROM XtendedUser__c];
            for (Opportunity oOpportunity : trigger.new) {
                oOpportunity.XtendedUser__c = null;
                for (XtendedUser__c oXtendedUser : lstXtendedUser){
                    if (oXtendedUser.Linked_to_User__c == oOpportunity.OwnerId) {
                        oOpportunity.XtendedUser__c = oXtendedUser.Id;
                        break;
                    }
                }
            }
        }
    }
}

 

All Answers

KodiKodi
Hi,
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]);
jvandevejvandeve
Thx for Reply Kodi but alas no. Code saved fine, but when editing an oppty where XtendedUser is blank and saving nothing happened. The field stays blanc.
Avidev9Avidev9
Can you describe a bit on your requriement ?
jvandevejvandeve
Ok maybe an exemple:

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?
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

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.

 

jvandevejvandeve

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:

 

trigger PopulateXtendedUser on Opportunity (before insert, before update) {
    if (trigger.isBefore) { 
        if (trigger.isInsert || trigger.isUpdate) {
            //list XtendedUsers
            list<XtendedUser__c> lstXtendedUser = [SELECT Id, Name, Linked_to_User__c FROM XtendedUser__c];
            for (Opportunity oOpportunity : trigger.new) {
                oOpportunity.XtendedUser__c = null;
                for (XtendedUser__c oXtendedUser : lstXtendedUser){
                    if (oXtendedUser.Linked_to_User__c == oOpportunity.OwnerId) {
                        oOpportunity.XtendedUser__c = oXtendedUser.Id;
                        break;
                    }
                }
            }
        }
    }
}

 

This was selected as the best answer