0

I have a custom object, Deal_Reg__c. On this object, i have a Contact lookup field, Submitter_Contact__c. I would like the trigger to populate this Submitter_Contact__c field. The trigger will fire when the Status field on the Deal_Reg__c is changed to Submit.

In order to populate the Submitter_Contact__c field, the user will change the Status field to "Submit" and the field will populate with the current user's contact.

My thought is that i will need to use the LastModifiedBy field on Deal_Reg__c to get the current User, and then from there get the Contact. Here is what i currently have which is not working properly:

trigger UpdateDealSubmitterContact on Deal_Reg__c (before Update) {

    list<id> oid = new list<id>();
    User user;
    for(Deal_Reg__c o: trigger.new){                   
        oid.add(o.id);
    }

    for(Deal_Reg__c o: trigger.new){
        if(o.Deal_Registration_Status__c == 'Submit') {
            map<id, Deal_Reg__c> ExtendU = new map<id, Deal_Reg__c>(
                [select LastModifiedByID from Deal_Reg__c where id in: oid]);
            user = [select ContactID from User where id =: ExtendU.get(o.id).LastModifiedBy.Id];

            //o.Submitter_Contact__c = ExtendU.get(o.id).LastModifiedBy.ContactID;
            o.Submitter_Contact__c = user.ContactID;
        }
    }
}

Any suggestions or ideas would be greatly appreciated! Thanks

  • Current user means the user currently logged in is it? – highfive Dec 17 '14 at 8:01
  • Yes, the user currently logged in – RayRay Dec 17 '14 at 21:19
1

You are probably looking for the current user. See (UserInfo documentation).

Try Userinfo.getuserid()

1

Few suggestions

  1. You should avoid using SOQL inside for loops.
  2. Here is my code / trigger that I would use to fulfill the requirement you specified above (plz understand by reading apex best practices).

    trigger UpdateDealSubmitterContact on Deal_Reg__c (before Update) {
        // populate user ids 
        Set<Id> UserIds = new Set<Id>();
        for (Deal_Reg__c dReg : Trigger.new) 
            UserIds.add(dReg.LastModifiedBy);
    
        // get the contacts against the userids 
        map<id, user> userContact = 
                    new map<id, user>([SELECT user.Id , user.Contact.id, user.name
                                       FROM User
                                       WHERE user.id IN:UserIds]);
    
        // check and update submitter contact
        for(Deal_Reg__c o: trigger.new){
            if(o.Deal_Registration_Status__c == 'Submit') {
                o.Submitter_Contact__c = userContact.get(o.LastModifiedById).Contact.Id;
            }
        }
    }
    
  • thanks for the response. I actually plugged in the code you sent and got the following error: Error: Compile Error: Incompatible element type SOBJECT:User for collection of Id at line 5 column 9 In order to get around the error i changed both "LastModifiedBy" to "LastModifiedById". This allows the code to compile but doesn't populate the Submitter_Contact__c field with anything. Any suggestions? – RayRay Dec 17 '14 at 21:30
  • @RayRay: you need to check whether there is any contact against the logged-in user. The reason you are not getting any ContactId might be that there is no contact against the user you are looking. – Mahmood Dec 18 '14 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.