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
karol.freebergkarol.freeberg 

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) {
        AccIDSet.add(SalesPol.Account_Name__c);
        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;
    }
}
Ramu_SFDCRamu_SFDC
Hi,

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.