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
prateek khanna 24prateek khanna 24 

Need help in writing this trigger

Milestone and Opportunity Object is there , On Opportunity you have custom field called total Amount, if opportunity is inserted with 10000 as total amount, automatically 10 milesstone should be created.
if the amount is 15000, 15 milesstone should be created and so on.
Best Answer chosen by prateek khanna 24
sachinarorasfsachinarorasf
Hi Prateek

Please go through the below code:

public class OpportunityMilestoneForum {
    public static void createOpportunity(List<Opportunity> oppList)
    {
        try{
            String totalAmount = String.valueOf(oppList[0].Total_Amount__c);
            if(totalAmount != null && totalAmount.length() > 4) {
                String oppNumber = totalAmount.substring(0,2);
                List<MileStone__c> mileStoneList = new List<MileStone__c>();
                for(Integer i =0; i<Integer.valueOf(oppNumber); i++)
                    mileStoneList.add(new MileStone__c(Name='mile stone demo record '+i));
                insert mileStoneList;
            }
        }
        catch(Exception ex) {
            System.debug('Exception:::'+ex.getMessage()+' at line:: '+ex.getLineNumber());
        }
    }
}

Here is the trigger:

trigger OpportunityDemo on Opportunity ( after insert) {
   
    if(Trigger.isAfter && Trigger.isInsert)
        OpportunityMilestoneForum.createOpportunity(Trigger.New);
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com  (http://www.sachinsf.com )

All Answers

sachinarorasfsachinarorasf
Hi Prateek

Please go through the below code:

public class OpportunityMilestoneForum {
    public static void createOpportunity(List<Opportunity> oppList)
    {
        try{
            String totalAmount = String.valueOf(oppList[0].Total_Amount__c);
            if(totalAmount != null && totalAmount.length() > 4) {
                String oppNumber = totalAmount.substring(0,2);
                List<MileStone__c> mileStoneList = new List<MileStone__c>();
                for(Integer i =0; i<Integer.valueOf(oppNumber); i++)
                    mileStoneList.add(new MileStone__c(Name='mile stone demo record '+i));
                insert mileStoneList;
            }
        }
        catch(Exception ex) {
            System.debug('Exception:::'+ex.getMessage()+' at line:: '+ex.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com  (http://www.sachinsf.com )
prateek khanna 24prateek khanna 24
Hi,
    Thanks for your response,How to call this class using trigger, When I am trying to do so, its showing some error.

 
sachinarorasfsachinarorasf
Hi Prateek

Please go through the below code:

public class OpportunityMilestoneForum {
    public static void createOpportunity(List<Opportunity> oppList)
    {
        try{
            String totalAmount = String.valueOf(oppList[0].Total_Amount__c);
            if(totalAmount != null && totalAmount.length() > 4) {
                String oppNumber = totalAmount.substring(0,2);
                List<MileStone__c> mileStoneList = new List<MileStone__c>();
                for(Integer i =0; i<Integer.valueOf(oppNumber); i++)
                    mileStoneList.add(new MileStone__c(Name='mile stone demo record '+i));
                insert mileStoneList;
            }
        }
        catch(Exception ex) {
            System.debug('Exception:::'+ex.getMessage()+' at line:: '+ex.getLineNumber());
        }
    }
}

Here is the trigger:

trigger OpportunityDemo on Opportunity ( after insert) {
   
    if(Trigger.isAfter && Trigger.isInsert)
        OpportunityMilestoneForum.createOpportunity(Trigger.New);
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com  (http://www.sachinsf.com )
This was selected as the best answer
prateek khanna 24prateek khanna 24
Thanks  @sachinarorasf .