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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

How Can I Add a Account In Case Record Based On Mobile Number

Hi,

1. Case Created When SMS_Transaction__c Creation.
2. I Need To Add Account In Case Record (Based On The Mobile Number).
Add Account IF(Account.PersonMobile == SMS_Transaction__c.Mobile__Number__c).

Kindly Refer The Following Code And Advise me how it's Possible.
 
trigger CasecreationBySMS on SMS_Transaction__c (After Insert, After Update) {
    System.debug('Case Creation By Sms');
    Set<Id> smsTransId  = New Set<Id>();
    Set<String> phone = New Set<String>();
    List<Case> stList = New List<Case>();
    Map<Id,Id> accMap  = New Map<Id,Id>(); 
    
    For(SMS_Transaction__c s : Trigger.New){
        If(s.Event__c == 'TKT' || s.Event__c == 'MsCall1' || s.Event__c == 'MsCall2'){
            smsTransId.add(s.Id);
            System.debug('SMS ID  |' + smsTransId);
        }
        IF(s.Mobile_Number__c !=Null){
            phone.add(s.Mobile_Number__c);
        }
    }
    
    If(smsTransId.size() > 0){
        List<SMS_Transaction__c> st = [Select id,name,Received_Text__c  from SMS_Transaction__c Where ID IN: smsTransId];
        //List<Account> acc = [Select id,name,PersonHomePhone From Account Where PersonHomePhone IN : phone ORDER BY CreatedDate DESC LIMIT 1];
        For(Account acc : [Select id,name,PersonHomePhone From Account Where PersonHomePhone IN : phone ORDER BY CreatedDate DESC LIMIT 1]){
            accMap.put(acc.PersonHomePhone, acc.Id);
        }
        For(SMS_Transaction__c s1 : st){
            Case c = New Case();
            c.Priority = 'Medium';
            c.Status = 'New';
            c.Description = s1.Received_Text__c;
            IF(s1.Mobile_Number__c == accMap.get(acc.PersonHomePhone).Mobile_Number__c){
                c.AccountId = accMap.get(s1.Mobile_Number__c).AccountId;
            }
            stList.add(c);
        }
        System.debug('stList Size | ' + stList.size());
        IF(stList.size() > 0){
            Insert stList;
        }
    }

}

Thanks ,

Soundar.
v varaprasadv varaprasad
HI Raj,

Please let me know why you are using 
Map<Id,Id> accMap = New Map<Id,Id>();
accMap.put(acc.PersonHomePhone, acc.Id);  //personhomephone is string or id field?

Otherwise please try like below any we are using one accout right.
 
string PersonHome = '';
id accid = '';
 For(Account acc : [Select id,name,PersonHomePhone From Account Where PersonHomePhone IN : phone ORDER BY CreatedDate DESC LIMIT 1]){
            PersonHome = acc.PersonHomePhone;
			accid = acc.id;
        }
		
		 IF(s1.Mobile_Number__c == PersonHome){
                c.AccountId = accid;
            }

Hope this helps.
Please let me know in case of any other assistance.

Thanks
Varaprasad