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
vinay kumar 1170vinay kumar 1170 

i have a custom object appointments__c it has a field called status__c .when status__c is changed from confirmed to visited then account record should be created.these two objects has a master detail relationship where account is parent

trigger capture on Appointment__c (after update) 
{
   list<account> acclist = new list<account> ();
    
    for(Appointment__c app: trigger.new)
    {
        if(app.Status__c=='Confirmed' &&  trigger.oldMap.get(app.Id).Status__c=='Visited')
        {
            account acc = new account();
            acc.Patient_Name__c = app.Id;
            acc.Phone_Number__c = app.Contact_Number__c;
            acc.Email__c = app.Email__c;
            
            acclist.add(acc);
        }
    }
    
    if(acclist.size()>0)
    {
        
        try{
            insert acclist;
            }
        catch(System.DmlException e)      
        {
            System.debug(e);
        }
            
    }
    
}
Best Answer chosen by vinay kumar 1170
Narender Singh(Nads)Narender Singh(Nads)
Use this code:

trigger capture on Appointment__c (after update) 
{
   list<account> acclist = new list<account> ();
    
    for(Appointment__c app: trigger.new)
    {
        if(app.Status__c=='Visited' &&  trigger.oldMap.get(app.Id).Status__c=='Confirmed')
        {
            account acc = new account();
            acc.Name='Test Account';
            acc.Patient_Name__c = app.Id;
            acc.Phone_Number__c = app.Contact_Number__c;
            acc.Email__c = app.Email__c;
            
            acclist.add(acc);
        }
    }
    
    if(acclist.size()>0)
    {
        
        try{
            insert acclist;
            }
        catch(System.DmlException e)      
        {
            System.debug(e);
        }
            
    }
    
}

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Can you tell me where are you facing the difficulty? Your trigger code looks fine.
I guess you are having trouble associating the account record with the custom object record. 
Please confirm
vinay kumar 1170vinay kumar 1170
When i changed the status to visited then account record should be created.But account record was not creating.Could you please rectify this problem.
Ajay K DubediAjay K Dubedi
Hi Vinay,

If you are using standard Account Object then Account Name is the mandatory field to insert new Account.

Account acc = new Account();
acc.Name ='Test';

Let me know if any problem occurs again. 
Please mark it as best answer if you find it helpful.

Thank You
Ajay Dubedi
Narender Singh(Nads)Narender Singh(Nads)
Use this code:

trigger capture on Appointment__c (after update) 
{
   list<account> acclist = new list<account> ();
    
    for(Appointment__c app: trigger.new)
    {
        if(app.Status__c=='Visited' &&  trigger.oldMap.get(app.Id).Status__c=='Confirmed')
        {
            account acc = new account();
            acc.Name='Test Account';
            acc.Patient_Name__c = app.Id;
            acc.Phone_Number__c = app.Contact_Number__c;
            acc.Email__c = app.Email__c;
            
            acclist.add(acc);
        }
    }
    
    if(acclist.size()>0)
    {
        
        try{
            insert acclist;
            }
        catch(System.DmlException e)      
        {
            System.debug(e);
        }
            
    }
    
}
This was selected as the best answer
vinay kumar 1170vinay kumar 1170
hey thanks a lot buddy.Its working
Narender Singh(Nads)Narender Singh(Nads)
Great!