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
jrosser1.39050288155405E12jrosser1.39050288155405E12 

Apex Trigger on new web to cases

We are using the web to case feature and are able to populate P21_Customer_ID (custom field, this is the customer number in our ERP system). However we are not able to populate the account name on the case which assings the case to the account in sales force.  This is my first time wrtting a trigger in Apex.  This is what I came up with so far.

Basically I want to take the P21_Customer_ID entered on the case, look up the account name from the account table and add it to the case.

trigger populate_customer_name on Case (after insert) {
Case TheCase = trigger.new[0];
if(TheCase.P21_Customer_ID != null) {
Account theAccount = [Select Name from Account
where Account.P21_CompanyCustomer_ID__c = TheCase.P21_Customer_ID];
TheCase.AccountId = theAccount.Name;
update TheCase;
}
}

Thank you,
Sonam_SFDCSonam_SFDC
Was wondering if this same functionality that you have written for a trigger can be implemented through a workflow field update.

Did you happen to try that?
jrosser1.39050288155405E12jrosser1.39050288155405E12
I have not tried that, I am very new to sales force, would you be able to point me in the right direction to see if "workflow" would be a bettter option vs using a trigger?

Thank you,
Sonam_SFDCSonam_SFDC
Sure, please check the following help doc which explains how you can create a workflow on any Salesforce Object :
https://help.salesforce.com/apex/HTViewHelpDoc?id=customize_wf.htm&language=en_US
https://help.salesforce.com/apex/HTViewHelpDoc?id=customize_wfrules.htm&language=en_US

Since you wish to update a field on Case - I am assuming the Acocunt field is a custom field - please aslo read the field update help link
https://help.salesforce.com/htviewhelpdoc?id=workflow_defining_field_updates.htm&siteLang=en_US



jrosser1.39050288155405E12jrosser1.39050288155405E12
Thank you for your reply, but I do not think that work flow will work.  I am trying to update a lookup field "Account Name" which does not appear to be part of the Cases Object.  When we enter a Case through the web form we are providing the account number (which is a customer field).  I want to take that value then lookup and populate the account name on the Case which will link that case to the correct account.