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
SoulsSouls 

Initial term of field expression must be a concrete SObject

I have written an After Insert trigger on Case that re-assigns AccountId and Contact Id associated wiith the case based on if the case is created by a customer portal user or not.

 

The trigger will not compile because of  an error that I don't quite understand here:  Initial term of field expression must be a concrete SObject: LIST<Case>

 

It fails where I'm extracting the new Id from a map of Accounts keyed by a custom field: SSNumber__c and try to assign that Id to the Case.AccountId field (see ==> below). What am I doing wrong?

 

trigger AfterInsertCase on Case (after insert) {
	List<Id> lstCases = new List<Id>();
	List<String>lstSSNumbers=new List<String>();
// Find cases created by Customer Portals for (Case caseObj:Trigger.new) { if(caseObj.Account.IsCustomerPortal==true){ lstCases.add(caseObj.Id); lstSSNumbers.add(caseObj.SSNumber__c); } } // Create a map with the AccountID's keyed by SSNumber__c Map<String,Account>MapAccBySSNumber=new Map<String,Account>(); List<Account>lstAcc=new List<Account>([Select Id, SSNumber__c from Account where SSNumber__c IN:lstSSNumbers]); for(Account ac:lstAcc){ MapAccBySSNumber.put(ac.SSNumber__c,ac); } // Update the Case object with new references to Contact and Account List<Case>lstCaseInfo=new List<Case>([Select Id, AccountId, ContactId, SSNumber__c from Case where Id IN :lstCases]); list<Case>lstCasesToUpdate=new List<Case>(); for(Case cse: lstCaseInfo){ ==> cse.AccountId = MapAccBySSNumber.get(lstCaseInfo.SSNumber__c).Id; ==> cse.ContactId=MapAccBySSNumber.get(lstCaseInfo.SSNumber__c).Id; lstCasesToUpdate.add(cse); } update lstCasesToUpdate; }

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Modify like this

 

trigger AfterInsertCase on Case (after insert) {
	List<Id> lstCases = new List<Id>();
	List<String>lstSSNumbers=new List<String>();
// Find cases created by Customer Portals for (Case caseObj:Trigger.new) { if(caseObj.Account.IsCustomerPortal==true){ lstCases.add(caseObj.Id); lstSSNumbers.add(caseObj.SSNumber__c); } } // Create a map with the AccountID's keyed by SSNumber__c Map<String,Account>MapAccBySSNumber=new Map<String,Account>(); List<Account>lstAcc=new List<Account>([Select Id, SSNumber__c from Account where SSNumber__c IN:lstSSNumbers]); for(Account ac:lstAcc){ MapAccBySSNumber.put(ac.SSNumber__c,ac); } // Update the Case object with new references to Contact and Account List<Case>lstCaseInfo=new List<Case>([Select Id, AccountId, ContactId, SSNumber__c from Case where Id IN :lstCases]); list<Case>lstCasesToUpdate=new List<Case>(); for(Case cse: lstCaseInfo){ cse.AccountId = MapAccBySSNumber.get(cse.SSNumber__c).Id; cse.ContactId=MapAccBySSNumber.get(cse.SSNumber__c).Id; lstCasesToUpdate.add(cse); } update lstCasesToUpdate; }

One issue is there in the line

cse.ContactId=MapAccBySSNumber.get(cse.SSNumber__c).Id;

 

Here you are assigning accountid to contactid field which is not possible. So please check your code.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

pinoytechiepinoytechie

The error message tells you exactly the offending object.

 

Surely it will complain for you are using (as mentioned in the error) List<Case>.SSNumber__c to get an item from the map:

 

cse.AccountId = MapAccBySSNumber.get(lstCaseInfo.SSNumber__c).Id;

cse.ContactId=MapAccBySSNumber.get(lstCaseInfo.SSNumber__c).Id;

 

 

 

souvik9086souvik9086

Modify like this

 

trigger AfterInsertCase on Case (after insert) {
	List<Id> lstCases = new List<Id>();
	List<String>lstSSNumbers=new List<String>();
// Find cases created by Customer Portals for (Case caseObj:Trigger.new) { if(caseObj.Account.IsCustomerPortal==true){ lstCases.add(caseObj.Id); lstSSNumbers.add(caseObj.SSNumber__c); } } // Create a map with the AccountID's keyed by SSNumber__c Map<String,Account>MapAccBySSNumber=new Map<String,Account>(); List<Account>lstAcc=new List<Account>([Select Id, SSNumber__c from Account where SSNumber__c IN:lstSSNumbers]); for(Account ac:lstAcc){ MapAccBySSNumber.put(ac.SSNumber__c,ac); } // Update the Case object with new references to Contact and Account List<Case>lstCaseInfo=new List<Case>([Select Id, AccountId, ContactId, SSNumber__c from Case where Id IN :lstCases]); list<Case>lstCasesToUpdate=new List<Case>(); for(Case cse: lstCaseInfo){ cse.AccountId = MapAccBySSNumber.get(cse.SSNumber__c).Id; cse.ContactId=MapAccBySSNumber.get(cse.SSNumber__c).Id; lstCasesToUpdate.add(cse); } update lstCasesToUpdate; }

One issue is there in the line

cse.ContactId=MapAccBySSNumber.get(cse.SSNumber__c).Id;

 

Here you are assigning accountid to contactid field which is not possible. So please check your code.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
SoulsSouls

Ouch!

 

I see my mistake, and it hurts ;)

 

I had re-written the code anyway to this format (essentially doing the same thing but with a line or two less):

 

	// Update the Case object with new references to Contact and Account
	list<Case>lstCasesToUpdate=new List<Case>();
	for (case c:[Select Id, AccountId, ContactId, SSNumber__c from Case where Id IN :lstCases]){
		c.AccountId = MapAccBySSNumber.get(c.SSNumber__c).Id;
		c.ContactId = MapAccBySSNumber.get(c.SSNumber__c).Id;
		lstCasesToUpdate.add(c);
	}

	update lstCasesToUpdate;