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
sfdc_apex_learnersfdc_apex_learner 

Trying to update a lookup field via Trigger

Hello All,

 

This is my first attempt at writing an apex trigger. I got the code from another post on this board at http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-Update-Lookup-Field/m-p/354509/highlight/true#M62906

 

I am trying to update the Store__c field on the standard Case object. This Store__c field is a lookup to a custom object called Store.

 

Below is my code. Any ideas?

 

trigger UpdateStoreName on Case (before update, before insert) {
List<String> StoreNames = new List<String>();
for (Case a: Trigger.new)
{
StoreNames.add(a.Store__c);
}
List <Store__c> StoreNameList = [Select Id from Store__c where Name in :StoreNames];
for (integer i = 0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].Store__c != null)
{
Trigger.new[i].Store__c = StoreNameList[i].ID;
}
else
{
Trigger.new[i].Store__c = null;
}
}
}

Peter.GPeter.G

Hi,

  It looks like you are using the Store__c field (lookup) to get the store name.    Getting the value of this filed will give you either an ID or null.  Is there another field that is being populated with the name of the store you are attenpting use to set the lookup field?

 

Also, you are relying on the list fo stores to be returned but the SOQL query in the same order that you added them to the StoreNames list in the first for loop.

  This will work when only a single record is updated/inserted, but will most likely fail if a bulk operation updated or inserts multiple records as the order of the returned values is not guarenteed.  You would need to map the stores returned in your query to the store name's that are included in the case fields to have the lookup set properly.

 

Andrew WilkinsonAndrew Wilkinson

I looked at the previous post you are referencing. In their scenario they are taking the Billing Postal Code on the account object and using that to compare Zip Code objects and populate the zip code lookup to the queried object. In your case...you are grabbing the same field to populate itself. Also, the code referenced will not work for bulk and is not guaranteed to work. If you can please post your requirement and explain what you are trying to do I believe I may be able to help. What field on the case are you using to identify the store object and what field do you want updated? They can't be the same field as in the code you posted.

sfdc_apex_learnersfdc_apex_learner

Andrew & Peter thanks for the responses and in hindsight your suggestions make sense. I was developing this code in my Dev org before putting it in my Production org

 

Here is my requirement in my Production org. I have new cases coming in from Web-to-case or Email-to-Case. These newly created cases correctly get auto-populated with standard Case fields such as: Contact Name, Contact Email, Account Name. The field that I need to get populated is called AB Store (API name = AB_Store__c) and it is a lookup to the AB Store custom object's standard field called AB Store Name. My intention is to get the AB Store field auto-populated by the trigger based on any of the Contact fields mentioned above getting populated. 

 

To complicate things a bit, I have custom Junction Object called "Contact-Store Link" which has the correct AB Store that I need to get populated. However, one contact can have multiple "Contact-Store Link" each with different "AB Store" values. The relationship between the Contact and the Contact-Store Link is a Master Detail relationship where standard object Contact and custom object AB store are master objects.

 

On the Contact-Store Link custom object, there is a AB Store field (API name = AB_store__c ) which is a master-detail to the AB Store custom object

APathakAPathak

Hi,

Since a contact is related to multiple stores through junction object, what is the rule to identify the particular store record that will be populated in case object?

 

Or is the store information also captured in the email-to-case?

 

sfdc_apex_learnersfdc_apex_learner

@APathak - I capture the store URL in the Web-to-Case form however, in email-to-case it is not captured.

APathakAPathak

I guess if the store URL is being captured then store lookup field value can be found based on this URL :-

 

	trigger UpdateStoreName on Case (before update, before insert) 
	{
		List<String> StoreNames = new List<String>();
		//Fetch List of Store URLs
		for (Case a: Trigger.new)
		{
			StoreNames.add(a.StoreURL__c);//assuming there is a field called storeURL in both case object & Store object.
		}

		List <Store__c> lStores = [Select Id from Store__c where Name in :StoreNames];
		Map <String,Id> mapStore = new Map <String,Id>();
		
		//Prepare a map of store URL and store ID
		for(Store__c s : lStores)
		{
			mapStore.put(s.storeURL__c,s);
		}
		
		//Populate the Store Ids in respective case records.
		for(case c : Trigger.new)
		{
			if(mapStore.contains(c.StoreURL__c))
			c.Store__c = mapStore.get(c.StoreURL__c);
		}
	}