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
ChinnoduChinnodu 

I have an account object, I have a status field which has open and completed checkboxes, when ever I click on completed, I want an opportunity to be created automatically.

Hi All,

I have an account object, I have a status field which has open and completed checkboxes, when ever I click on completed, I want an opportunity to be created automatically.

it will get trigger, how i will get from trigger , any one can help me on this with example functionalty

Thanks,
Chinna
 
Best Answer chosen by Chinnodu
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger createOpportunity on Account (after insert) {

List<Opportunity> oppsToUpdate = new List<Opportunity>();

for(Account ac : Trigger.new) {

    // here is where you check if opportunity that is being inserted
    //meets the criteria
    if (ac.Status__c == 'Completed') {

        Opportunity o = new Opportunity ();

        o.AccountId=ac.Id;
        o.StageName = 'Closed Won'; 
        o.CloseDate = Date.today();   
        o.Name = 'Opp';

        oppsToUpdate.add(o);
    }

}

    if(oppsToUpdate.size()>0){
        insert oppsToUpdate; 
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger createOpportunity on Account (after insert) {

List<Opportunity> oppsToUpdate = new List<Opportunity>();

for(Account ac : Trigger.new) {

    // here is where you check if opportunity that is being inserted
    //meets the criteria
    if (ac.Status__c == 'Completed') {

        Opportunity o = new Opportunity ();

        o.AccountId=ac.Id;
        o.StageName = 'Closed Won'; 
        o.CloseDate = Date.today();   
        o.Name = 'Opp';

        oppsToUpdate.add(o);
    }

}

    if(oppsToUpdate.size()>0){
        insert oppsToUpdate; 
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
ChinnoduChinnodu
Thank you so much Khan, It 's working as expected 
Raj VakatiRaj Vakati
You no need to use the trigger for this one .. You can do it by using the process builder .. 

Refer this 

https://trailhead.salesforce.com/en/content/learn/modules/business_process_automation/process_builder
https://douglascayers.com/2017/04/02/use-process-builder-to-update-related-records-when-activities-logged/