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
Gateway Bikes Group4Gateway Bikes Group4 

Trigger on Opportunity Object

Question no1: *****No Test Coverage for this below listed Test Class****
Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.Use any values for the fields on the opportunities.Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.

trigger IdenticalOpp on Account(after insert){
list<Opportunity> opplist = new list<Opportunity>();
  for(Account a:Trigger.new)
    {
        Opportunity opp=new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account';
        opp.StageName='Prospecting';
        opp.CloseDate=Date.toDay();
        opplist.add(opp);
                Opportunity opp1=new Opportunity();
        opp1.AccountId=a.id;
        opp1.Name='Created from account';
        opp1.StageName='Prospecting';
        opp1.CloseDate=Date.toDay();
        opplist.add(opp1);
        }     
    if(opplist != null){
        insert opplist ;
    }}

@isTest
private class IdenticalOppTest {
    
@isTest
static void Opportunitycreate() {
         Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=acct.Id);
        insert opp;

Question 2: ***Working on Trigger LeadCompetitor on Opportunity ***
                        What is the datatype field on Leading_Competitor__c ?
                        The Leading Competitor field unable to collect the data of                                    Competitor lowest Price

trigger LeadingCompetitor on Opportunity (before insert,before update) {

for(Opportunity opp:Trigger.new){
    //Add all our prices in a list in order of competitor
    List<Decimal> competitorPrices =new List<Decimal>();
    
        competitorPrices.add(opp.Competitor_1_Price__c);
        competitorPrices.add(opp.Competitor_2_Price__c);
        competitorPrices.add(opp.Competitor_3_Price__c);
       
    //Add all our comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
 
  //Loop through alll competitors to find the position of the lowest price
   Decimal lowestPrice;
   Integer lowestPricePosition;
   
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        
        if(lowestPrice==null || currentPrice<lowestPrice){
           lowestPrice=currentPrice;
           lowestPricePosition=i;
           }
           }
           //Populate the leading competitor field with the competitor matching the lowest price position
           // opp.Leading_Competitor__c=competitors.get(lowestPricePosition);
      } 
Raj VakatiRaj Vakati
This code will give 100 % for Account trigger
 
@isTest
private class IdenticalOppTest {
    
    @isTest
    static void Opportunitycreate() {
        Account acct = new Account(Name='Test Account');
        insert acct;
        
    }
}

Try this for opp trigger
 
@isTest
private class IdenticalOppTest {
    
    @isTest
    static void Opportunitycreate() {
        Account acct = new Account(Name='Test Account');
        insert acct;
        
         Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                           Competitor_1_Price__c =12 ,
                                           Competitor_2_Price__c =13 ,
                                           ven__Competitor_1__c =12 ,
                                           Competitor_3_Price__c=14 ,
                                       AccountId=acct.Id);
        insert opp;
        
    }
}

 
Gateway Bikes Group4Gateway Bikes Group4
Hi Raj,
I was work on the Trigger
Error Message:
User-added imageUser-added image
I was getting error when i saving the record

 
trigger LeadingCompetitor on Opportunity (before insert,before update) {

for(Opportunity opp:Trigger.new){
    //Add all our prices in a list in order of competitor
     List<Decimal> competitorPrices =new List<Decimal>();
    
        competitorPrices.add(opp.Competitor_1_Price__c);
        competitorPrices.add(opp.Competitor_2_Price__c);
        competitorPrices.add(opp.Competitor_3_Price__c);
       
    
      //Add all our comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_2__c);
         competitors.add(opp.ven__Competitor_3__c);
 
  //Loop through alll competitors to find the position of the lowest price
   Decimal Highprice;
   Integer HighPricePosition;
   Integer Hgv;
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        
        if(Highprice==null || currentPrice>Highprice){
           Highprice=currentPrice;
           Highprice=i;
           }
           }
    
           //Populate the leading competitor field with the competitor matching the lowest price position
           
           opp.Leading_Competitor_del_del__c=competitors.get(HighPricePosition);
           //opp.Lead_CompetitorPrice__c=competitorPrices.get(Highprice.intValue());
           //system.assertEquals(1, Lead_CompetitorPrice__c.intValue());
    }  
 
   



    
       
       

}
 
Raj VakatiRaj Vakati
HighPricePosition is not initialized .. please check the value and set before calling them 
Gateway Bikes Group4Gateway Bikes Group4
//We are initilize to the current price?
Decimal Highprice;
   Integer HighPricePosition;
       for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        
        if(Highprice==null || currentPrice>Highprice){
           Highprice=currentPrice;//
           Highprice=i;
           }
           }
    
           //Populate the leading competitor field with the competitor matching the lowest price position
           
           opp.Leading_Competitor_del_del__c=competitors.get(HighPricePosition);
           //opp.Lead_CompetitorPrice__c=competitorPrices.get(Highprice.intValue());