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
Varun99Varun99 

Apex Trigger

Hi am creating a trigger on child object it will fire on account(parent) when am insert a record in child at 

 

address__c field that text will update in this parent account in billing city.

 

is it correct this code please help me am getting an Error : Error: "Compile Error: Initial term of field expression must be

 

a concrete SObject: String at line 17 column 23"

 

 

 

trigger updatingaccount on Work_flow__c (after insert,after update)
{
set<id> xyzset=new set<id>();
for(work_flow__c c:trigger.new)
{
if(c.address__c == null || c.address__c != null)
{
xyzset.add(c.account__c);
}
}
map<id,string> app=new map<id,string>();
for(account a:[select id,Billingcity from account where id in:xyzset limit 50000])
app.put(a.id,a.Billingcity);
for(work_flow__c c:trigger.new)
{
if(c.address__c == null || c.address__c != null)
app.get(c.account__c).Billingcity = c.address__c;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Eugene PozniakEugene Pozniak

You put String in map instead of Account. Modify your code:

 

trigger updatingaccount on Work_flow__c (after insert,after update) {
	set<id> xyzset=new set<id>();
	for(work_flow__c c:trigger.new) {
		if(c.address__c == null || c.address__c != null) {
			xyzset.add(c.account__c);
		}
	}

	map<id, Account> app=new map<id, Account>();
	List<Account> accountsToUpdate = new List<Account<();
	
	for(account a:[select id,Billingcity from account where id in:xyzset limit 50000])
		app.put(a.id, a);
	
	for(work_flow__c c:trigger.new) {
		if(c.address__c == null || c.address__c != null) {
			Account acc = app.get(c.account__c);
			acc.Billingcity = c.address__c;
			accountsToUpdate.add(acc);
		}
	}
	
	if(!accountsToUpdate.isEmpty()) {
		update accountsToUpdate;
	}
}

 

 

All Answers

Eugene PozniakEugene Pozniak

You put String in map instead of Account. Modify your code:

 

trigger updatingaccount on Work_flow__c (after insert,after update) {
	set<id> xyzset=new set<id>();
	for(work_flow__c c:trigger.new) {
		if(c.address__c == null || c.address__c != null) {
			xyzset.add(c.account__c);
		}
	}

	map<id, Account> app=new map<id, Account>();
	List<Account> accountsToUpdate = new List<Account<();
	
	for(account a:[select id,Billingcity from account where id in:xyzset limit 50000])
		app.put(a.id, a);
	
	for(work_flow__c c:trigger.new) {
		if(c.address__c == null || c.address__c != null) {
			Account acc = app.get(c.account__c);
			acc.Billingcity = c.address__c;
			accountsToUpdate.add(acc);
		}
	}
	
	if(!accountsToUpdate.isEmpty()) {
		update accountsToUpdate;
	}
}

 

 

This was selected as the best answer
Varun99Varun99

Thank you Mr. Euqune Pozniak