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
Siva SakthiSiva Sakthi 

How to display the sub query value in apex trigger

Hi,
            How to display the sub query value in apex trigger. Please find the code and correct me where i made mistake. How to aolve this issue.
I have try to conver Lead to Account and that time i want to send a text message. Your Lead has successfully converted with Account Name as well as Contact Name. I can get Account Name but not able to get the contact name to display.Please suggest me.  
trigger ConvertLeadtoAccount on Account (after insert) {
    List<smsbox__c> sms = new List<smsbox__c>();
    String Txttemp = null;
    for (Account acct : Trigger.New){
                          
            Account acc = [SELECT Phone, Name, (SELECT Name, MobilePhone, Phone FROM Contacts) FROM Account WHERE Id =:acct.Id limit 1];
           
             
            Txttemp = 'The Lead  '+ acc.Name +' has been successfully converted into '+ acc.Name +' Account';
            
            system.debug(Txttemp);
            smsbox__c obj = new smsbox__c();
            obj.PhoneNumber__c = acc.Phone;
            obj.SMSText__c = Txttemp;  
            obj.external_field__c = acct.Id + 'instratestmessage';
            obj.Name__c = acc.Name;
            sms.add(obj);
        }
    insert sms;
}

I have tried acc.contacts[0].Name also but not yet fixed.

Thanks
Sivasakthi
sfdcMonkey.comsfdcMonkey.com
Hi Sivasakthi,
your trigger is on 'after Insert' right.... so this code will be execute after new account record create, so at this time there is no contacts available to this account. because contacts are child record for account so before create parent how we can get child records. that is why you can't get contact name. hopes you understand.

Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks 
sfdcmonkey.com 
Amit Chaudhary 8Amit Chaudhary 8
You will not able to get Contact record because your Trigger is on After Account Insert.

In Lead Convertion Record will created in below order
1) Account
2) Contact
3) Opportunity

So please try to create trigger on Contact.

 
Siva SakthiSiva Sakthi
Thanks Piyush Soni & Amit Chaudhary. I understand the lead convertion recoed flow.