• Haroon_jva
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies

We want to display  related lookup objects in a picklist when an opporunity is being created. In the opportinuty we have a lookup to brands. Brands are related to accounts. We want a pick list of brands when an account is selected when a new opportunity is being created.

 

I am using the standard opportunity controller with an extension. The extension can not use the account id on the opportunity untill the opportunity is saved. How can I reference the account on the opportunity?

 

Here is the logic that does not work: 

 public Test1_OppExtension(ApexPages.StandardController stdController) {
    this.o = (Opportunity)stdController.getRecord();

    }
 
    //builds a picklist of account names based on their account id
    public List<SelectOption> getBrands() {
 

        List<SelectOption> options = new List<SelectOption>();
         //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -'));
 
        
        if(o.AccountId != null){
                 id account_id;
                 account_id = Opportunity.AccountId ;
                       
                for (Brand__c brand : [select b.id, b.name From Brand__c b where   b.Advertiser__c =: o.account_id ]){
                options.add(new selectOption(brand.id, brand.Name));
            }
        }
        
        return options;
     }

 

 

 

Hi All

I am a newbie writing apex.I understand why I am getting this but I can not find a work around. I am writing a trigger that creates a custom object every time a new opportunity is created.  The possible workaround I may have,  is to pull all the data of Rep_assignments__c in a list and then use logic to get the values I need. I have about 27,000 record in there and do not find it an effective way to program. Any suggestions are appreciated,

Thanks

 

 

Here is my code:

I am calling this from a trigger.

 

public static void setpipeline (Opportunity [] opps)
 {
Date maxvalue = NULL ;
//build list of records
list<Rep_assignments__c> Records = new list<Rep_assignments__c>() ;
    list <date> dates = new List<date>();

List <print_pipeline__c> pipelinetoinsert = new List<print_pipeline__c>();

//need to put list of opportunities
list<Opportunity> Oppty = new List<Opportunity> ();

//build list of records of rep assignments
for (Opportunity z:opps)
    {
    
    
  if (z.RecordTypeId ==record_type &&  z.integration__c <> 1)
  records = [select r.id, r.Rep1_id__c,  r.Rep1_pct__c, r.Rep2_id__c, r.Rep2_pct__c, r.Rep3_id__c, r.Rep3_pct__c,  r.Rep4_id__c, r.Rep4_pct__c, r.effective_date__c
    from Rep_assignments__c r where r.brand__c = :z.brand__c and r.pub_id__c =:z.pub_id__c and r.effective_date__c <=:z.CloseDate ];
 }


//if more than one record found, then we need to get the max date and the array ref number to get the correct rep assignment
if (records.size() !=0)
{
    integer ref_num = 0;

    date maxdate;
    if (records.size() > 1)
        {
    
        for (integer i = 0; i < records.size(); i++)
        {
            
          dates.add((records.get(i).effective_date__c));  
         }
         //get max date
         maxdate = dates[0];
         
         for (integer i = 0; i < dates.size(); i ++){
           if (dates[i] > maxdate)
           maxdate = dates[i];    
         }
        }
        //get ref number
        
        for (integer i=0; i <Records.size(); i++){
            if (records[i].Effective_Date__c == maxdate)
            ref_num= i;
        }  

//create print pipe
    
    for (Opportunity o:opps){
    
    if (records.get(ref_num).rep1_id__c != null)
    {
            print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id ,
             Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep1_id__c,
            Rep_split__c=records.get(ref_num).Rep1_pct__c );
                pipelinetoinsert.add(pipeline);    
       
    }
    if (records.get(ref_num).rep2_id__c != null){
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep2_id__c,
                Rep_split__c=records.get(ref_num).Rep2_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    if (records.get(ref_num).rep3_id__c != null)
    {
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep3_id__c,
                Rep_split__c=records.get(ref_num).Rep3_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    if (records.get(ref_num).rep4_id__c != null)
    {
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep4_id__c,
                Rep_split__c=records.get(ref_num).Rep4_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    }


if (!pipelinetoinsert.isEmpty())
database.insert(pipelinetoinsert);
}

}

Hi All

I am a newbie writing apex.I understand why I am getting this but I can not find a work around. I am writing a trigger that creates a custom object every time a new opportunity is created.  The possible workaround I may have,  is to pull all the data of Rep_assignments__c in a list and then use logic to get the values I need. I have about 27,000 record in there and do not find it an effective way to program. Any suggestions are appreciated,

Thanks

 

 

Here is my code:

I am calling this from a trigger.

 

public static void setpipeline (Opportunity [] opps)
 {
Date maxvalue = NULL ;
//build list of records
list<Rep_assignments__c> Records = new list<Rep_assignments__c>() ;
    list <date> dates = new List<date>();

List <print_pipeline__c> pipelinetoinsert = new List<print_pipeline__c>();

//need to put list of opportunities
list<Opportunity> Oppty = new List<Opportunity> ();

//build list of records of rep assignments
for (Opportunity z:opps)
    {
    
    
  if (z.RecordTypeId ==record_type &&  z.integration__c <> 1)
  records = [select r.id, r.Rep1_id__c,  r.Rep1_pct__c, r.Rep2_id__c, r.Rep2_pct__c, r.Rep3_id__c, r.Rep3_pct__c,  r.Rep4_id__c, r.Rep4_pct__c, r.effective_date__c
    from Rep_assignments__c r where r.brand__c = :z.brand__c and r.pub_id__c =:z.pub_id__c and r.effective_date__c <=:z.CloseDate ];
 }


//if more than one record found, then we need to get the max date and the array ref number to get the correct rep assignment
if (records.size() !=0)
{
    integer ref_num = 0;

    date maxdate;
    if (records.size() > 1)
        {
    
        for (integer i = 0; i < records.size(); i++)
        {
            
          dates.add((records.get(i).effective_date__c));  
         }
         //get max date
         maxdate = dates[0];
         
         for (integer i = 0; i < dates.size(); i ++){
           if (dates[i] > maxdate)
           maxdate = dates[i];    
         }
        }
        //get ref number
        
        for (integer i=0; i <Records.size(); i++){
            if (records[i].Effective_Date__c == maxdate)
            ref_num= i;
        }  

//create print pipe
    
    for (Opportunity o:opps){
    
    if (records.get(ref_num).rep1_id__c != null)
    {
            print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id ,
             Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep1_id__c,
            Rep_split__c=records.get(ref_num).Rep1_pct__c );
                pipelinetoinsert.add(pipeline);    
       
    }
    if (records.get(ref_num).rep2_id__c != null){
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep2_id__c,
                Rep_split__c=records.get(ref_num).Rep2_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    if (records.get(ref_num).rep3_id__c != null)
    {
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep3_id__c,
                Rep_split__c=records.get(ref_num).Rep3_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    if (records.get(ref_num).rep4_id__c != null)
    {
               print_pipeline__c pipeline = new print_pipeline__c (Opportunity__c=o.id , Rep_assignment__c=records.get(ref_num).id,     Sales_Rep__c=records.get(ref_num).Rep4_id__c,
                Rep_split__c=records.get(ref_num).Rep4_pct__c );
                pipelinetoinsert.add(pipeline);    
    }
    }


if (!pipelinetoinsert.isEmpty())
database.insert(pipelinetoinsert);
}

}