function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mw6mw6 

Trigger to populate lookup field on custom object from contact

I have a custom object called 'Class_Allocation__c' and have a lookup field on this object called 'Related_Contact__c.  I need expert help to write a trigger on Class_Allocation__c to auto copy the contact Id into this field based on 'Student__Name__c' field on Class_Allocation__C and Student_id_from_Student_object__c (on contact)
NagendraNagendra (Salesforce Developers) 
Hi,

May I suggest you please check with below link from stack exchange community exactly with a similar issue and a suggested workaround.

Please tweak the below code as per your requirement which will guide you further.
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;
        }
    }
}
For more information please check with below post.
Hope this helps.

Please mark this as solved if it;s resolved.

Thanks,
Nagendra
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,

Trigger to populate Contact lookup on a custom object.please refer the below link for reference. hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar