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
Joe_IpsenJoe_Ipsen 

Able to find the ID, but not establish the Lookup in this Trigger

To augment Web2Case's matching* in SFDC, I am attempting to build a relationship between the domain name within the submitted email address to the Asset and/or Account.

trigger domainMatch on Case (before insert) { for (Case c : Trigger.new) { String WebEmail = c.SuppliedEmail; System.debug('WebEmail = ' + WebEmail); // Find where the @ sign occurs Integer index = WebEmail.indexOf('@', 0); System.debug('index = ' + index); // Extract all text after the @ sign String emailDomain = WebEmail.substring(index+1); System.debug('emailDomain = ' + emailDomain); // When the Contact has not been matched, search through Account & Asset for the Domain if ((c.Contact == null) && (index != -1)) { if ([SELECT a.Name from Account a WHERE a.Name = :emailDomain] != null) { c.Account = [SELECT a.id from Account a WHERE a.Name = :emailDomain]; update c.Account; System.debug('c.Account = ' + c.Account); } if ([SELECT a.Name from Account a WHERE a.Name = :emailDomain] != null) { c.Asset = [SELECT a.id from Asset a WHERE a.Name = :emailDomain]; update c.Asset; System.debug('c.Asset = ' + c.Asset); } } } }

 

From looking at the debug logs (below) I have extracted the domain name from the email address and am able to find the appropriate ID in SFDC, but the lookup field on the Case record does not appear to be linked to the Asset or Account. 

 

How can I link the case to the Account and/or Asset?

 *** Beginning domainMatch on Case trigger event BeforeInsert for null

20090902190439.204:Trigger.domainMatch: line 14, column 5: SelectLoop:LIST:SOBJECT:Case
20090902190439.204:Trigger.domainMatch: line 17, column 5: WebEmail = nosuchuser@domain.com
20090902190439.204:Trigger.domainMatch: line 22, column 5: index = 10
20090902190439.204:Trigger.domainMatch: line 26, column 5: emailDomain = domain.com
20090902190439.204:Trigger.domainMatch: line 30, column 17: SOQL query with 1 row finished in 44 ms
20090902190439.204:Trigger.domainMatch: line 31, column 29: SOQL query with 1 row finished in 21 ms
20090902190439.204:Trigger.domainMatch: line 32, column 17: Update: SOBJECT:Account
20090902190439.204:Trigger.domainMatch: line 32, column 17:     DML Operation executed in 110 ms
20090902190439.204:Trigger.domainMatch: line 33, column 17: c.Account = Account:{Id=0016000000MKEMVAA5}
20090902190439.204:Trigger.domainMatch: line 35, column 17: SOQL query with 1 row finished in 30 ms
20090902190439.204:Trigger.domainMatch: line 36, column 27: SOQL query with 1 row finished in 44 ms
20090902190439.204:Trigger.domainMatch: line 37, column 17: Update: SOBJECT:Asset
20090902190439.204:Trigger.domainMatch: line 37, column 17:     DML Operation executed in 75 ms
20090902190439.204:Trigger.domainMatch: line 38, column 17: c.Asset = Asset:{Id=02i60000004B2vBAAS}

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 4 out of 20
Number of query rows: 4 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 2 out of 20
Number of DML rows: 2 out of 100
Number of script statements: 14 out of 10200
Maximum heap size: 0 out of 200000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0

*** Ending domainMatch on Case trigger event BeforeInsert for null

 

BTW, I know that I need to build better bulk processing into this code . . . that's the next challenge.

 

==================================

* By default, Web2Case only compares the complete email address to the Email address within Contact records.  If anyone else at that company submits a case and they do not have a Contact record with their email address, their case is not associated with that Account nor Asset.

Best Answer chosen by Admin (Salesforce Developers) 
IanRIanR

Hi,

 

Having successfully located the Account and Asset in your trigger, I would recommend you set the references by Id, instead of setting the objects,

 

e.g.

 

c.AccountId = a.Id (instead of) a.Account = [SELECT Id FROM Account WHERE {condition}];

 

 

 

 

Also, in terms of creating a trigger robust enough for a batch insert/update, the main concern is taking all the DML operations out of the for-loop...

 

Have a look at this...

 

 

HTH :)

All Answers

AmphroAmphro

What do you mean by does not seem to be linked?

line 33, column 17: c.Account = Account:{Id=0016000000MKEMVAA5}

line 38, column 17: c.Asset = Asset:{Id=02i60000004B2vBAAS}

Joe_IpsenJoe_Ipsen

I am expecting the Account & Asset field on the Case detail page to be filled out with the linked Asset & Account.  Here is a screenshot of the Case that was created during that debug log:

 

Screenshot of Case

IanRIanR

Hi,

 

Having successfully located the Account and Asset in your trigger, I would recommend you set the references by Id, instead of setting the objects,

 

e.g.

 

c.AccountId = a.Id (instead of) a.Account = [SELECT Id FROM Account WHERE {condition}];

 

 

 

 

Also, in terms of creating a trigger robust enough for a batch insert/update, the main concern is taking all the DML operations out of the for-loop...

 

Have a look at this...

 

 

HTH :)

This was selected as the best answer
Joe_IpsenJoe_Ipsen

Thanks Ian . . . I reworked my code after reading through your instructions and seem to have worked out all of my problems.  Starting my bulk testing now.

 

 

trigger domainMatch on Case (before insert) { // Use a Set, as add() method will only add an item if it is not already in the set, hence no duplicates.. List<string> emailDomains = new List<string>(); Map<ID, String> caseID2EmailDomainMap = new Map<ID, String>(); for (Case c : trigger.new) { String WebEmail = c.SuppliedEmail; System.debug('WebEmail = ' + WebEmail); // Find where the @ sign occurs if (WebEmail != null) { Integer index = WebEmail.indexOf('@', 0); System.debug('index = ' + index); // Extract all text after the @ sign String emailDomain = WebEmail.substring(index+1); System.debug('emailDomain = ' + emailDomain); //Add the emailDomain to the Set of emailDomains if (index != -1) { emailDomains.add(emailDomain); caseID2EmailDomainMap.put(c.ID, emailDomain); } } } Map<String, Account> accountMap = new Map<String, Account>(); List<Account> accountList = [ SELECT a.Id, a.Name FROM Account a WHERE a.Name in :emailDomains ]; for (Account a : accountList) { accountMap.put(a.Name, a); } Map<String, Asset> assetMap = new Map<String, Asset>(); List<Asset> assetList = [ SELECT a.Id, a.Name FROM Asset a WHERE a.Name in :emailDomains ]; for (Asset a : assetList) { assetMap.put(a.Name, a); } for (Case c : Trigger.new) { // When the Contact has not been matched, search through Account & Asset for the Domain if (c.Contact == null) { String emailDomain = caseID2EmailDomainMap.get(c.Id); if (emailDomain != null) { Account account = accountMap.get(emailDomain); if (account != null) { c.AccountId = accountMap.get(emailDomain).Id; } } System.debug('c.AccountId = ' + c.AccountId); if (emailDomain != null) { Asset asset = assetMap.get(emailDomain); if (asset != null) { c.AssetId = assetMap.get(emailDomain).Id; } } System.debug('c.AssetId = ' + c.AssetId); } } }