0
votes

I created a custom object XtendedUser which has an id and Name.

I created a custom lookupfield on Opportunity called "XtendedUser__c" which links the opportunity to the corresponding XtendedUser record.

Now I made it so that the name of an opportunityowner corresponds to the name of an XtendedUser-record, so I want the trigger to autopopulate the custom lookup field "XtendedUser__c" on the opportunity with the id of the corresponding XtendedUser-record of which the name matches the name of the opportunityowner.

I never wrote a trigger, always worked with workflows and fieldupdates, but I've got to make this work. So if you could please help me with this? I would be extremely greatfull!

Thanks in advance

1
Hi @jan, there is a new stackexchange site specific to Salesforce at salesforce.stackexchange.com. Come join the community over there! :)Ralph Callaway

1 Answers

0
votes

You should use a map to link the records and retrieve the value before the insert of a new record and before the update of an existing record. This technique will also allow you to bulk update all your records. It should be something like:

trigger ExtendedUser__c on Opportunity (before insert, before Update) {    

    list<id> oid = new list<id>();
    for(opportunity o: trigger.new){                   
        oid.add(o.id);
            }
    map<id, ExtendedUser__c> ExtendU = new map<id, ExtendedUser__c>(
        [select name from ExtendedUser__c where id in: oid]);

    for(opportunity o: trigger.new){
        o.name = ExtendU.get(o.id).name;
    }
}