• KB Kim
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
This code auto-creates OLI when a user create a new opportunity and selet a product name from the custome picklist (product__C).

I need to deploy this to our production instance, but I don't know how to write test code for this..

Please, help me.. I need to deploy this as soon as possible.


trigger AutoCreateProduct on Opportunity (after insert) { 
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (Opportunity newOpportunity: Trigger.New) { 
        if(newOpportunity.product__C.equalsIgnoreCase('CRA')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iVAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        } if (newOpportunity.product__C.equalsIgnoreCase('GALV')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iQAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
          if (newOpportunity.product__C.equalsIgnoreCase('2TFS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo7AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('DRTP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoRAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
    } 
    if(!OpportunityLineItems.isEmpty()){
        Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);

        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
                }
            }
        }
    }



Test code that has only 58% coverage.


@IsTest (seealldata=true)
public class AutoCreateProduct{
    public static testmethod void myunittest(){
               
       //create a dummy account
        Account a = new Account ();
        a.Name = 'Test Asset Account';
        insert a;
        
        //create a dummy opportunity
        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Name = 'test Oppty';
        o.StageName = 'opportunity';
        o.CloseDate = system.today();
        o.product__c = 'cra';
        o.volume__c = 10;
                insert o;
  
    }
}
 
  • January 22, 2016
  • Like
  • 0
I have this code to auto-create OLI when create a new opportunity and selet a product name from the custome picklist (product__C).

I don't know how to write test code... plz help me..

trigger AutoCreateProduct on Opportunity (after insert) { 
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (Opportunity newOpportunity: Trigger.New) { 
        if(newOpportunity.product__C.equalsIgnoreCase('CRA')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iVAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        } if (newOpportunity.product__C.equalsIgnoreCase('GALV')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iQAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
          if (newOpportunity.product__C.equalsIgnoreCase('2TFS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo7AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('DRTP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoRAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY TIN')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoqAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('DRBP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoMAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('GLHR')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBobAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('1TFS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo2AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('ETP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoWAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY SHEET')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBolAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('SRTP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp0AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('CRFH')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoCAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('CRHS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoHAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('HRP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBogAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('STS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp5AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('SRBP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBovAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }

        
        
        
    } 
    if(!OpportunityLineItems.isEmpty()){
        Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);

        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
                }
            }
        }
    }






@IsTest (seealldata=true)
public class AutoCreateProduct{
    public static testmethod void myunittest(){
               
       //create a dummy account
        Account a = new Account ();
        a.Name = 'Test Asset Account';
        insert a;
        
        //create a dummy opportunity
        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Name = 'test Oppty';
        o.StageName = 'opportunity';
        o.CloseDate = system.today();
        o.product__c = 'cra';
        o.volume__c = 10;
                insert o;
  
    }
}
  • January 20, 2016
  • Like
  • 0
Can anyone help me to create a test class for this trigger?
---------------------------------------------------------------------------------------

trigger Product on Opportunity (before update) {

list<opportunity> lt = trigger.new;

list<opportunityLineItem> opli = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where opportunityId =: trigger.new[0].id]);

string productName='';

for(opportunityLineItem o : opli){

 productName = o.product2.name;

}

for(Opportunity opp : trigger.new){

 opp.product__c = productName;

}

}
  • January 14, 2016
  • Like
  • 0
Please, share your trigger and test code if you have simmillar one for this requirement.

When a user select a product picklist from a custom field (product__c) on an opportunity, it should create a opportunity product on its related list.
Also, when a user create a opportunity product from the related list, the product__C field on the opportunity should be updated.

We will have only one product for each opportunity.

Truly, thanks for any comments and helps, in advance.
 
  • January 13, 2016
  • Like
  • 0
I have to make a table chart on each case detail view page.
This code works in another custome object but Standard Case object.
No errors, but It does not show colum names even though I already passed the ID.

In addition, if possible... is there any way that I can make a button to add more raws on the table?
We have multiple fields most time when we enter the new cases, so I want to give users option wheather to add more field on the table or not.

I appreciate for your help in advance !



<apex:page standardController="Case">
    <apex:form >
    <apex:pageBlock >
         <apex:pageBlockTable value="{!Case}" var="c">
           
            <apex:column value="{!c.Coil_ID__c}"/>
            <apex:column value="{!c.Product__c}"/>
            <apex:column value="{!c.Specification__c}"/>
            <apex:column value="{!c.Defect__c}"/>
            <apex:column value="{!c.Quantity__c}"/>
            <apex:column value="{!c.Invoiced_Price__c}"/>
            <apex:column value="{!c.Coil_Cost__c}"/>
            <apex:column value="{!c.CSV__c}"/>
            <apex:column value="{!c.Scrap_Value__c}"/>
            <apex:column value="{!c.PCC__c}"/>
            <apex:column value="{!c.ACC__c}"/>
                            
            <apex:inlineEditSupport />       
         </apex:pageBlockTable>
      
   
    </apex:pageBlock>
</apex:form>
</apex:page>
  • September 15, 2015
  • Like
  • 0
I need a table with all the custom fields in case standard object.
This code does not actuall create a table. what am I missing?

<apex:page standardController="case" recordsetVar="coil">
    <apex:pageBlock Title="Coil Information">
         <apex:pageBlockTable value="{!coil}" var="a">
            <apex:column value="{!a.Claim_Case_Number__c}"/>
            <apex:column value="{!a.Coil_Cost__c}"/>
            <apex:column value="{!a.Coil_ID__c}"/>
            <apex:column value="{!a.CSV__c}"/>
            <apex:column value="{!a.ACC__c}"/>
                    
         </apex:pageBlockTable>
       
    </apex:pageBlock>
</apex:page>
  • September 10, 2015
  • Like
  • 0
I have to make a table chart on each case detail view page.
This code works in another custome object but Standard Case object.
No errors, but It does not show colum names even though I already passed the ID.

In addition, if possible... is there any way that I can make a button to add more raws on the table?
We have multiple fields most time when we enter the new cases, so I want to give users option wheather to add more field on the table or not.

I appreciate for your help in advance !



<apex:page standardController="Case">
    <apex:form >
    <apex:pageBlock >
         <apex:pageBlockTable value="{!Case}" var="c">
           
            <apex:column value="{!c.Coil_ID__c}"/>
            <apex:column value="{!c.Product__c}"/>
            <apex:column value="{!c.Specification__c}"/>
            <apex:column value="{!c.Defect__c}"/>
            <apex:column value="{!c.Quantity__c}"/>
            <apex:column value="{!c.Invoiced_Price__c}"/>
            <apex:column value="{!c.Coil_Cost__c}"/>
            <apex:column value="{!c.CSV__c}"/>
            <apex:column value="{!c.Scrap_Value__c}"/>
            <apex:column value="{!c.PCC__c}"/>
            <apex:column value="{!c.ACC__c}"/>
                            
            <apex:inlineEditSupport />       
         </apex:pageBlockTable>
      
   
    </apex:pageBlock>
</apex:form>
</apex:page>
  • September 15, 2015
  • Like
  • 0
I need a table with all the custom fields in case standard object.
This code does not actuall create a table. what am I missing?

<apex:page standardController="case" recordsetVar="coil">
    <apex:pageBlock Title="Coil Information">
         <apex:pageBlockTable value="{!coil}" var="a">
            <apex:column value="{!a.Claim_Case_Number__c}"/>
            <apex:column value="{!a.Coil_Cost__c}"/>
            <apex:column value="{!a.Coil_ID__c}"/>
            <apex:column value="{!a.CSV__c}"/>
            <apex:column value="{!a.ACC__c}"/>
                    
         </apex:pageBlockTable>
       
    </apex:pageBlock>
</apex:page>
  • September 10, 2015
  • Like
  • 0