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
Vadim ChelombitkoVadim Chelombitko 

Hello, can u help me? For example i have two custom objects Client and Property.

The Property has field "Available". The Property “Available” field should be set to "True" by default. If the property has
prospective Client the “Available” field must be changed to “False” automatically. Need i do it with Triggers?
Best Answer chosen by Vadim Chelombitko
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Vadim,  If the data model is as Cvuyyuru assumes (Client is the child of Property), then his solution should work.  If Property looks up to the prospective Client (Client is the parent of Property), then you can just make the Available field a Boolean formula field:
ISBLANK(Prospective_Client__c)

 

All Answers

cvuyyurucvuyyuru
Yes, you need to do this through trigger.
Assuming the following
1. Parent object name is Property__c
2. Child object name is Client__c
3. On Client, relationship to Property field api name is Property__c.
4. Relationship name for clients - clients__r
trigger clientTrg on Client__c(after insert, after update, after delete, after undelete){
	
	set<Id> propertyIds = new set<id>();

	if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
		for(Client__c cl: trigger.new){
			propertyIds.add(cl.Property__c);
		}
	}

	if(trigger.isUpdate || trigger.isDelete){
		for(Client__c cl: trigger.old){
			propertyIds.add(cl.Property__c);
		}
	}
	

	if(!propertyIds.isEmpty()){

		list<Property__c> lstProp = new list<Property__c>();
		for(Property__c prop: [Select id, (Select id from Clients__r) from Property__c where Id in: propertyIds]){
			if(prop.Clients__r.size()>0){
				prop.Status__c = 'Available';
				lstProp.add(prop);
			}
		}

		if(!lstProp.isEmpty())
			update lstProp;

	}



}




 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Vadim,  If the data model is as Cvuyyuru assumes (Client is the child of Property), then his solution should work.  If Property looks up to the prospective Client (Client is the parent of Property), then you can just make the Available field a Boolean formula field:
ISBLANK(Prospective_Client__c)

 
This was selected as the best answer