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
Alexander Svyatetskyi 6Alexander Svyatetskyi 6 

How to implement a formula

I have a custom Target Customer object with two fields:
Representative - lookup to User object
Customer - lookup to Contacts object
In Contact is a custom field formula isTarget which should be an indicator that the contact and the user entry exists in the object Target Customer.
How to implement a formula in isTarget?
Best Answer chosen by Alexander Svyatetskyi 6
Pankaj_GanwaniPankaj_Ganwani
Hi Alex,

Sorry for wrong suggestion. Please update the events to on after insert and after update and use below mentioned code:
trigger UpdateIsTarget on Target_Customer__c (after insert,after update) {
    List<Contact> lstUpdateContacts = new List<Contact>();
	
	for(Target_Customer__c tarcus: Trigger.New){
            if(tarcus.Customer__c!=NULL && tarcus.User__c!=NULL)
			    lstUpdateContacts.add(new Contact(Id = tarcus.Customer__c, Is_Target__c = true);
    }
	update lstUpdateContacts;
}



 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Alex,

There is no way to traverse from parent to child in formula field. To achieve this functionality, just update your field type to 'Checkbox', create a trigger on Target Customer object on before insert and before update events(since using workflow and process builder we cannot update the fields of parent object).
Alexander Svyatetskyi 6Alexander Svyatetskyi 6
Hi,Pankaj.Thanks for the answer. I did as you said. But when you try to change something in Target Customer that displays an error in the recording of the object :          " Error: Invalid Data. Review all error messages below to correct your data.Apex trigger UpdateIsTarget caused an unexpected exception, contact your administrator: UpdateIsTarget: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateIsTarget: line 3, column 1"

There is triggers code:
trigger UpdateIsTarget on Target_Customer__c (before insert,before update) {
    for(Target_Customer__c tarcus: Trigger.New){
            tarcus.Customer__r.Is_Target__c = true;
    }
}

 
Pankaj_GanwaniPankaj_Ganwani
Hi Alex,

Sorry for wrong suggestion. Please update the events to on after insert and after update and use below mentioned code:
trigger UpdateIsTarget on Target_Customer__c (after insert,after update) {
    List<Contact> lstUpdateContacts = new List<Contact>();
	
	for(Target_Customer__c tarcus: Trigger.New){
            if(tarcus.Customer__c!=NULL && tarcus.User__c!=NULL)
			    lstUpdateContacts.add(new Contact(Id = tarcus.Customer__c, Is_Target__c = true);
    }
	update lstUpdateContacts;
}



 
This was selected as the best answer
Alexander Svyatetskyi 6Alexander Svyatetskyi 6
Pankaj, Thank you a lot. It`s working.