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
uday kumar yuday kumar y 

I have two object like Contact and Account if Create a Contact it will be automatically Create the Account . But i got some error in my code please let me know that is the solution .

trigger updatepho on Contact (after insert){
    List<Account> acc = new List<Account>();
    for(Contact con:trigger.new){
      if(con.AccountId == null){
        Account a = [select Id,Phone,(select Id,Phone from Contacts) from Account where Phone =: con.Phone];
               a.Name = con.LastName;
               a.Phone = con.Phone;
               a.Amount__c = 25;
        acc.add(a);
        }
      
    }
    insert acc;
}
This is my error descripation :Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updatepho caused an unexpected exception, contact your administrator: updatepho: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updatepho: line 5, column 1
v varaprasadv varaprasad
Hi Uday,

please check below code once : 
 
trigger updatepho on Contact (after insert){
    List<Account> acc = new List<Account>();
    for(Contact con:trigger.new){
      if(con.AccountId == null){
        Account a = new account();		
        a.Name = con.LastName;
        a.Phone = con.Phone;
        a.Amount__c = 25;
        acc.add(a);
        }      
    }
    insert acc;
}

Thanks
Varaprasad​
v varaprasadv varaprasad
Please let me know your complete requirement.based on that i will provide you solution.