0
votes

So i am new to salesforce and i finished my training and now im working on a project. But on my project i have stumbled on a test class that i am not finding a way to write it, so i would appreciate if anyone can help me figure out a way to write it. Here is the code:

    public class AP01_Opportunity 
{
    //Method to create a new service contract when opportunity = Gagné
    public static void CreateContract(List<Opportunity> listOpp, Map<Id, Opportunity> oldMap)
    {
        //Variable Declaration
        ServiceContract sc;
        List<ServiceContract> listSCToAdd = new List<ServiceContract>();
        List<ContractLineItem> listContractItems = new List<ContractLineItem>();
        List<Opportunity> listOppGagne = new list<Opportunity>();

        //Loop in list of opportunities
        for(Opportunity opp : listOpp)
        {
            if(opp.StageName == Label.ClotureGagne && !oldMap.get(opp.Id).isWon)
            {
                listOppGagne.add(opp);
            }
        }
        //check if list has opportunity becoming won
        if(listOppGagne.size()  > 0){

            Map<Id, Opportunity> mapOppGagne = new Map<Id, Opportunity> ([SELECT Id,
                                                                          Name,
                                                                          StageName,
                                                                          Pricebook2Id,
                                                                          Account.Name, 
                                                                          (SELECT Id,
                                                                           PricebookEntryId,
                                                                           PricebookEntry.Name, 
                                                                           Quantity, 
                                                                           UnitPrice 
                                                                           FROM OpportunityLineItems)
                                                                          FROM Opportunity
                                                                          WHERE Id in :listOppGagne]);

            for( Opportunity opp : listOppGagne )
            {
                //Create new service contract
                sc = new ServiceContract();
                sc.Name = opp.Name;
                sc.ApprovalStatus = Label.Activated;
                sc.OpportunityId__c = Id.valueOf(opp.Id);
                sc.Pricebook2Id = opp.Pricebook2Id;
                sc.StartDate = Date.today();
                listSCToAdd.add(sc);
            }
            if(listSCToAdd.size() > 0){
                insert listSCToAdd;
                Opportunity currentOpp;
                ContractLineItem cli;
                Id oppId;
                for(ServiceContract servcont : listSCToAdd)
                {
                    oppId = servcont.OpportunityId__c;
                    if(mapOppGagne.containsKey(oppId))
                    {
                        currentOpp = mapOppGagne.get(oppId);
                        //copy the oppLineItems per opportunity to the respective Service Contract
                        for(OpportunityLineItem items : currentOpp.OpportunityLineItems)
                        {
                            cli = new ContractLineItem();
                            cli.PricebookEntryId = items.PricebookEntryId;
                            cli.Quantity = items.Quantity;
                            cli.UnitPrice = items.UnitPrice;
                            cli.ServiceContractId = servcont.Id;
                            listContractItems.add(cli);
                        }
                    }
                }
                if(listContractItems.size() > 0)
                {
                    insert listContractItems; 
                }
            }


        }
    }
}

this code is a trigger that creates a new service contract record with contract line items copied from the opportunity line items, when the opportunity stage changes to "Cloturé Gagné" which means closed won in french.

Thank you in advance.

1
Hello again , Please if someone can help me i am still stuck and i really need help Thank you in advance.User101
This class is not a trigger. Or do you call it from the trigger class of the Opportunity?utm
I call it from a trigger class of the Opportunity here is my trigger class: trigger OpportunityAfterUpdate on Opportunity (after update) { if(PAD.canTrigger('AP01_Opportunity')){ AP01_Opportunity.CreateContract(Trigger.new, Trigger.oldMap); } }User101
Sorry but I don't get the point. You have an after update trigger-ok. In this after update trigger you call an static method? (canTrigger). What does this method do? And last but not least: You said that you want to make a test class for this trigger. So where is this code? And where execatly are the problems?utm
the canTrigger is a bypass method that if it return true it doesnt go through the trigger it just bypasses it. and concerning my test class, i am not being able to write one. since i am new i am finding difficulties writing one so i dont have a code to base my question on. i am sorry about that. I need someone to tell me how. Thank you in advance.User101

1 Answers

0
votes

In order to write a simple test class I would recommend you to use the following guide: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

The idea is simple: Lets say you create an Opportunity in your Test class and make an insert or update in your case - your trigger class will automatically fire and run the code from your AP01_Opportunity class. You can put some

System.debug('some message');

to check if your logic works as expected and also which code blocks are executed