• Surekhasfdc
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 6
    Replies
Hi All,
Problem:
When an Opportunity is getting inserted with amount equal or more than 50000,then create one more new opportunity with half of initial
opportunity and initial opportunity must also be updated with half of the amount.All other fields should be copied as it is to the new opportunity.
My Attempts to solve:
 I am trying to write Trigger on Opportunity object and I am able to insert the new opportunity and initial opportunity and also made 
Amount to Half in both records but the initial record is also inserted one more time.That makes the total records inserted to 3.I tried several methods like after delete,after update but not successful.My code is below the screenshot.Where am I doing mistake?please let me know.
User-added image

Trigger:
trigger OpportunityTrigger on Opportunity(before insert) {
if(Trigger.IsInsert ==True || Trigger.IsAfter==True)
{
    OppClass.secondOpp(Trigger.new);
}
 
}

Apex Class:

public class OppClass {
    public static void secondOpp(List<Opportunity> listOpp){
        if(CheckRecursive.runOnce()) {
try{
        Opportunity originalOpp=new Opportunity();
        Opportunity newOpp=new Opportunity();
        list<Opportunity> record=new list<Opportunity>();
        //System.debug('Before for loop');
       //for(Opportunity oppquery:listOpp){
          //  record=[SELECT Id,Amount from Opportunity where Amount=:newOpp.Amount];
    //    }
     //       System.debug('record from query '+record);
     //  delete record;
       
        for(Opportunity opp:listOpp){
            originalOpp.Amount=(opp.Amount)/2;
            originalOpp.Name=opp.Name;
            originalOpp.CloseDate=opp.CloseDate;
            originalOpp.StageName=opp.StageName;
            System.debug('First Opportunity '+originalOpp);
           
           
        if(opp.Amount>=50000){
           
            newOpp.Amount=(opp.Amount)/2;
            newOpp.Name=opp.Name;
            newOpp.CloseDate=opp.CloseDate;
            newOpp.StageName=opp.StageName;
           // originalOpp.Amount=25000;
        }//end of if
        }//end of for loop
        System.debug('Second Opportunity ' +newOpp);
        insert newOpp;
        insert originalOpp;
           
       
        }catch(Exception e){
            System.debug('Exception caught'+e);
        }
        }//end of recursive
    }//end of secondOpp method
}//end of class


Thanks
Surekha
Hi All,
Problem:
When an Opportunity is getting inserted with amount equal or more than 50000,then create one more new opportunity with half of initial
opportunity and initial opportunity must also be updated with half of the amount.All other fields should be copied as it is to the new opportunity.
My Attempts to solve:
 I am trying to write Trigger on Opportunity object and I am able to insert the new opportunity and initial opportunity and also made 
Amount to Half in both records but the initial record is also inserted one more time.That makes the total records inserted to 3.I tried several methods like after delete,after update but not successful.My code is below the screenshot.Where am I doing mistake?please let me know.
User-added image

Trigger:
trigger OpportunityTrigger on Opportunity(before insert) {
if(Trigger.IsInsert ==True || Trigger.IsAfter==True)
{
    OppClass.secondOpp(Trigger.new);
}
 
}

Apex Class:

public class OppClass {
    public static void secondOpp(List<Opportunity> listOpp){
        if(CheckRecursive.runOnce()) {
try{
        Opportunity originalOpp=new Opportunity();
        Opportunity newOpp=new Opportunity();
        list<Opportunity> record=new list<Opportunity>();
        //System.debug('Before for loop');
       //for(Opportunity oppquery:listOpp){
          //  record=[SELECT Id,Amount from Opportunity where Amount=:newOpp.Amount];
    //    }
     //       System.debug('record from query '+record);
     //  delete record;
       
        for(Opportunity opp:listOpp){
            originalOpp.Amount=(opp.Amount)/2;
            originalOpp.Name=opp.Name;
            originalOpp.CloseDate=opp.CloseDate;
            originalOpp.StageName=opp.StageName;
            System.debug('First Opportunity '+originalOpp);
           
           
        if(opp.Amount>=50000){
           
            newOpp.Amount=(opp.Amount)/2;
            newOpp.Name=opp.Name;
            newOpp.CloseDate=opp.CloseDate;
            newOpp.StageName=opp.StageName;
           // originalOpp.Amount=25000;
        }//end of if
        }//end of for loop
        System.debug('Second Opportunity ' +newOpp);
        insert newOpp;
        insert originalOpp;
           
       
        }catch(Exception e){
            System.debug('Exception caught'+e);
        }
        }//end of recursive
    }//end of secondOpp method
}//end of class


Thanks
Surekha
Dear one,

I have tried for all best answer given by any of the member here.But unable to pass challenge #3.
Facing the error-

Challenge Not yet complete... here's what's wrong: 
A Volunteer Shift Worker record created for the logged in user is not automatically being assigned the correct status.

Any kind of help will be appreciated.

Thanks 
Hello,

I'm facing an issue with the SuperBadge "App Customization Specialist"

I'm trying to check my Report challenge but I get this error message : User-added image

And this is how i've created it : 

User-added image

Do you have any idea ? 

Thanks,
Guillaume