• Traveller Made
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 21
    Replies
I tried to import an Apex trigger and Class I created so that when an opportunity is created a quote is created, with the opportunity's product if it has one. We said me that we have to have 75%code coverage to import it. So I have to create an Apex test. But above my trigger and my class, it is already written 100%, so I don't understand.

Also, my Apex test is only 62%... And I don't know how to increase it. 

/// TRIGGER

trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}

/// CLASS

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }
        //Inserting the new QLIs
        insert lstQLI;
    }
}

/// TEST


@isTest

public class CreateQuoteTest {
    static testMethod void insertNewOpportunity() {
        Test.startTest();

        Product2 testProduct = new Product2(
            Name = 'Happy Funtime Ball'

        );

        insert testProduct;

        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = Test.getStandardPricebookId(),
             Product2Id = testProduct.Id,
             UnitPrice = 100.00,
             IsActive = true

        );

        insert pbEntry;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',
            CloseDate = date.today().addDays(5),        
            StageName = 'Sourcing Demand',
            Type = 'Event',           
            Account_manager_gescom__c = 'Farida Raji',
            Pricebook2Id = pbEntry.Pricebook2Id                      

        );

        insert testOpportunity;

        OpportunityLineItem testOLI = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice        
            //UnitPrice = 1100.00

        );

        insert testOLI;     
        
        OpportunityLineItem testOLI2 = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice         
            //UnitPrice = 1100.00

        );

        insert testOLI2;  
        
        
        Test.stopTest();

    }

}
 
Hi,

After several days of work and a lot of help I managed to create an Apex Class and an Opportunity Trigger to create a quote when an opportunity is created and a quote line item if the opportunity has an opportunity line item. 
I have tried to create a Test Class to manage to import it in my production since then bur the code coverage is only of 61%, I must have 75%.

How can I have a bigger code coverage ? Here's my codes. Hope you could help!

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }

        //Inserting the new QLIs
        insert lstQLI;

    }

}


trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}

// And the Test Apex :

@isTest

public class CreateQuoteTest {
    static testMethod void insertNewOpportunity() {
        Test.startTest();

        Product2 testProduct = new Product2(
            Name = 'Happy Funtime Ball'

        );

        insert testProduct;

        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = Test.getStandardPricebookId(),
             Product2Id = testProduct.Id,
             UnitPrice = 100.00,
             IsActive = true

        );

        insert pbEntry;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',
            CloseDate = date.today().addDays(5),        
            StageName = 'Sourcing Demand',
            Type = 'Event',           
            Account_manager_gescom__c = 'Farida Raji'                      

        );

        insert testOpportunity;

        OpportunityLineItem testOLI = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice,         
            UnitPrice = 1100.00

        );

        insert testOLI;     
        Test.stopTest();

    }

}
Hi,

After several days of work and a lot of help I managed to create an Apex Class and an Opportunity Trigger to create a quote when an opportunity is created and a quote line item if the opportunity has an opportunity line item. 

But now I must create a Test Class to manage to import it in my production and I have no idea how I can do that.

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }

        //Inserting the new QLIs
        insert lstQLI;

    }

}


trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}
I created with a lot of help a trigger with its Apex Class in a sandbox. Now I would like to have it in my production to really use it. 
How can I do that ? 

Please help me, I spend a lot of time creating these, I cannot have done it for nothing. Thank you in advance.
I would like to create a trigger which can insert a quote and a quotelineitem when I clone an opportunity with its product. I tried many Trigger.
If someone can help me understand the problem it would be awesome! I spent already a few days on it...

trigger FacturationCreator on Opportunity (after insert, after update) {
for (Opportunity o : Trigger.new) {
if (o.Type == 'Event') {

//Opportunity oppId=[select id from Opportunity where id=:q.OpportunityId];
List<OpportunityLineItem> opplines=[select id, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId
from OpportunityLineItem where OpportunityId=:o.id];
 for(OpportunityLineItem oppline:opplines){ 
List <Quote> factList = new List<Quote>();

Quote q = new Quote();
q.name = 'Quote-' + o.name;
q.Montant_1_re_ch_ance__c = 1000;
q.Date_de_Facture__c = o.CloseDate;
q.Date_1_re_ch_ance__c = o.CloseDate;
q.opportunityId = o.id;
factList.add(q);
insert q;


List<Quote> qList=[select id from Quote where opportunityId=:o.id];
for(Quote q:qList){ 
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId = q.Id;
qli.UnitPrice = oppline.UnitPrice;
qli.Product2Id = oppline.PriceBookEntry.Product2Id;
qli.Quantity = oppline.Quantity;
qli.PriceBookentryid = oppline.PriceBookentryId;
insert qli;}

}
        }
    }
}
I tried to import an Apex trigger and Class I created so that when an opportunity is created a quote is created, with the opportunity's product if it has one. We said me that we have to have 75%code coverage to import it. So I have to create an Apex test. But above my trigger and my class, it is already written 100%, so I don't understand.

Also, my Apex test is only 62%... And I don't know how to increase it. 

/// TRIGGER

trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}

/// CLASS

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }
        //Inserting the new QLIs
        insert lstQLI;
    }
}

/// TEST


@isTest

public class CreateQuoteTest {
    static testMethod void insertNewOpportunity() {
        Test.startTest();

        Product2 testProduct = new Product2(
            Name = 'Happy Funtime Ball'

        );

        insert testProduct;

        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = Test.getStandardPricebookId(),
             Product2Id = testProduct.Id,
             UnitPrice = 100.00,
             IsActive = true

        );

        insert pbEntry;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',
            CloseDate = date.today().addDays(5),        
            StageName = 'Sourcing Demand',
            Type = 'Event',           
            Account_manager_gescom__c = 'Farida Raji',
            Pricebook2Id = pbEntry.Pricebook2Id                      

        );

        insert testOpportunity;

        OpportunityLineItem testOLI = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice        
            //UnitPrice = 1100.00

        );

        insert testOLI;     
        
        OpportunityLineItem testOLI2 = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice         
            //UnitPrice = 1100.00

        );

        insert testOLI2;  
        
        
        Test.stopTest();

    }

}
 
Hi,

After several days of work and a lot of help I managed to create an Apex Class and an Opportunity Trigger to create a quote when an opportunity is created and a quote line item if the opportunity has an opportunity line item. 

But now I must create a Test Class to manage to import it in my production and I have no idea how I can do that.

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }

        //Inserting the new QLIs
        insert lstQLI;

    }

}


trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}
I created with a lot of help a trigger with its Apex Class in a sandbox. Now I would like to have it in my production to really use it. 
How can I do that ? 

Please help me, I spend a lot of time creating these, I cannot have done it for nothing. Thank you in advance.
I would like to create a trigger which can insert a quote and a quotelineitem when I clone an opportunity with its product. I tried many Trigger.
If someone can help me understand the problem it would be awesome! I spent already a few days on it...

trigger FacturationCreator on Opportunity (after insert, after update) {
for (Opportunity o : Trigger.new) {
if (o.Type == 'Event') {

//Opportunity oppId=[select id from Opportunity where id=:q.OpportunityId];
List<OpportunityLineItem> opplines=[select id, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId
from OpportunityLineItem where OpportunityId=:o.id];
 for(OpportunityLineItem oppline:opplines){ 
List <Quote> factList = new List<Quote>();

Quote q = new Quote();
q.name = 'Quote-' + o.name;
q.Montant_1_re_ch_ance__c = 1000;
q.Date_de_Facture__c = o.CloseDate;
q.Date_1_re_ch_ance__c = o.CloseDate;
q.opportunityId = o.id;
factList.add(q);
insert q;


List<Quote> qList=[select id from Quote where opportunityId=:o.id];
for(Quote q:qList){ 
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId = q.Id;
qli.UnitPrice = oppline.UnitPrice;
qli.Product2Id = oppline.PriceBookEntry.Product2Id;
qli.Quantity = oppline.Quantity;
qli.PriceBookentryid = oppline.PriceBookentryId;
insert qli;}

}
        }
    }
}