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
Marc De La CruzMarc De La Cruz 

return value from child object into error message

I am trying to create an error message for a parent object that returns a value from the child object.
The objects I'm using are Asset and a custom object Asset_Assignment__c. 
I can't seem to figure out how to return the value for the error message. 
Any help would be appreciated. 

Here's my code 
trigger AssetAssignmentValidation on Asset (before update, before insert) {
    LIST <string> AssetList = NEW LIST <string>();
    for(Asset a :{
        if(a.Status == 'Available' && a.OwnerID != NULL){
                    List <Asset_Assignment__c> assignmentlist = [SELECT Name FROM Asset_Assignment__c WHERE Asset_Name__c in : AssetList];
                     for (Asset_Assignment__c aa : assignmentlist)
                            string assignmentnumber = '';
                            assignmentnumber =;
                            return assignmentnumber; 
            a.adderror('Asset cannot be marked as available because it is currently assigned ' + 'Asset Assignment Number: ' + assignmentnumber);


AnudeepAnudeep (Salesforce Developers) 
Hi Marc, 

My understanding is if we are referencing fields in addError, they should be specified along with the object name ('c.LastName' as opposed to LastName). Please see example below
RestrictContactByName on Contact (before insert, before update) {
    for (Contact c : Trigger.New) {
        if(c.LastName == 'INVALIDNAME') {   //invalidname is invalid
            c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');


I would recommend assigning the value in the field 'assignmentnumber' to a value in a field in Asset. You can then use 'a.yourFieldName'

Marc De La CruzMarc De La Cruz
Hey Anudeep, 
I'm trying to use the field from a custom child object that uses a lookup relationship for a trigger on the parent object. 
What would be the best way to do that? I have no problem using a field on the asset object in the error message. I'm trying to use a field from a child object in an error message on the parent object. 
Hi Marc,

Please verify/clarify these statements:
- You are not trying to add an error on a child object field.
- You are trying to reference the child object field name (not the value) in the error that you add to the parent object.

AnudeepAnudeep (Salesforce Developers) 
Hi Marc, 

It is possible to use a field from a child object in an error message on the parent object. Can you build a string like the example below and use in  
addError method? Thanks
if( < 2){
//field: contact
if(c.Is_Check_Duplicate__c == 1 && c.MobilePhone != null){
List<Contact> lst_result = [SELECT id, LastName FROM Contact WHERE Contact.MobilePhone = :c.MobilePhone];
if(lst_result.size() > 0){
String errmsg = 'Nomor telepon ' + c.MobilePhone + ' sudah terdaftar atas nama:';
for(Contact cresult:lst_result){
errmsg += '<a target="_blank" href=\'' + cresult.Id + '\'>' + cresult.LastName + '</a> ';

//c.addError('Nomor telepon ' + c.MobilePhone + ' sudah terdaftar atas nama: <a target="_blank" href=\'' + lst_result[0].Id + '\'>' + lst_result[0].LastName + '</a>');