+ Start a Discussion
TilluTillu 

Please Correct this below code ?

I have Market , Business  custom objects. Market is Master  object and Business is child object. So Market has any no of Business records in its related list.

Now i want to update a Market  object field , if  Market has  No Business Records. It is Text field. Can any one correct the below field ? I am getting error on sowl statements.

trigger MarketChange on Market__c (before insert,before update) {

set<id> Pset = new set<id>();

for(Market__c py : Trigger.new){
Pset.add(py.id);
}
List<Market__c> Ltf = [select id,(select id,Market__c  from Business__r ) from Market__c];  // Error Here

if(Ltf.size() > 0){
for(Market__c px : Trigger.new){
px.ResultField__c = 'No Business data';
}
}
}

Error : Compile Error: Didn't understand relationship 'Business__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 17
Best Answer chosen by Tillu
Vatsal KothariVatsal Kothari
Hi,

You can refer below code:

trigger MarketChange on Market__c (before insert,before update) {
	set<id> pSet = new set<id>();
	Map<String,String> businessMap = new Map<String,String>();
	
	for(Market__c py : Trigger.new){
		pSet.add(py.id);
	}
	
	for(Business__c b : [select id,Market__c,Market_lookup__c  from Business__c where Market_lookup__c IN: pSet ]){
		if(b.Market_lookup__c != null){
			businessMap.put(b.Market_lookup__c,b.Market__c);
		}
	}
	
	if(Ltf.size() > 0){
		for(Market__c px : Trigger.new){
			if(!businessMap.containsKey(px.Id)){
				px.ResultField__c = 'No Business data';
			}
		}
	}
}

Replace Market_lookup__c with API name the lookup field.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi,

You can refer below code:

trigger MarketChange on Market__c (before insert,before update) {
	set<id> pSet = new set<id>();
	Map<String,String> businessMap = new Map<String,String>();
	
	for(Market__c py : Trigger.new){
		pSet.add(py.id);
	}
	
	for(Business__c b : [select id,Market__c,Market_lookup__c  from Business__c where Market_lookup__c IN: pSet ]){
		if(b.Market_lookup__c != null){
			businessMap.put(b.Market_lookup__c,b.Market__c);
		}
	}
	
	if(Ltf.size() > 0){
		for(Market__c px : Trigger.new){
			if(!businessMap.containsKey(px.Id)){
				px.ResultField__c = 'No Business data';
			}
		}
	}
}

Replace Market_lookup__c with API name the lookup field.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
TilluTillu
I didn't understand this  b.<strong>Market_lookup__c</strong>  ?
Vatsal KothariVatsal Kothari
Please change Market_lookup__c with API name of your field whic hyou have created on business object.
This is the field which creates the lookup realtion between two objects,
so replace the Market_lookup__c with API name of that field in the above code.
Vatsal KothariVatsal Kothari
trigger MarketChange on Market__c (before insert,before update) {
	set<id> pSet = new set<id>();
	Map<String,String> businessMap = new Map<String,String>();
	
	for(Market__c py : Trigger.new){
		pSet.add(py.id);
	}
	
	for(Business__c b : [select id,Market__c,Market_lookup__c  from Business__c where Market_lookup__c IN: pSet ]){
		if(b.Market_lookup__c != null){
			businessMap.put(b.Market_lookup__c,b.Market__c);
		}
	}
	
	if(Ltf.size() > 0){
		for(Market__c px : Trigger.new){
			if(!businessMap.containsKey(px.Id)){
				px.ResultField__c = 'No Business data';
			}
		}
	}
}