+ Start a Discussion

Using a trigger to copy name fields from Accout to Custom Object

I am very new to Apex. This is the first thing I'm attempting and of course, they want it yesterday. I am trying to update some user name fields on a custom oject from name fields that are on the account form. Below is my code. I am having trouble because the Customer Service Rep field is sending the ID instead of the name. I'm not sure how to copy a name field from one object to another. Please help. Once I get this field, then I need to proceed to get the owner of the account and get the project manager field from the user record.

trigger FillApprovers on Sales_Policy_Exceptions__c (before insert) {
    Set<ID>AccIDset = new Set<ID>();
    For (Sales_Policy_Exceptions__c SalesPol : Trigger.new) {
        List<Account> Accts = [SELECT ID,Pricing_Lead__c,Customer_Service_Rep_Primary__C from Account WHERE ID  IN :accIdSet];
        SalesPol.Primary_Customer_Service_Rep__c = Accts.Get(0).Customer_Service_Rep_Primary__c;

I believe customer_service_rep_primary__c is a lookup field referring to another object. In that case you need to use relationship queries to get the name rather than id. Example : string acc=[select id,customer_service_rep__r.firstname from opportunity where id=:op.Id].customer_service_rep__r.firstname; In this example Customer_service_rep__r is a lookup field referring to User object.