• anandanand
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 6
    Replies

how to get opportunity products into my related list custom object

 

For adding particular products in opportunity products related list alternatively tat appropriate products added into another related list custom object.

 

Please help:is it possible

In opportunity object having an standard related list "products".And another custom related list am using is "Accomodation ".

In Products related list ,if am adding products using add product button same products will be added into custom related list  "Accomodation" also at the same time.

so my trigger in OpportunityLineItem is

 

trigger accc on OpportunityLineItem (before insert,after insert) {
List<Accomodation__c> lstAccomodation = new List<Accomodation__c>();
    OpportunityLineItem  to=trigger.new[0];

   Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
    list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Product2.Family,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];

    for(OpportunityLineItem qli:ql){
        Accomodation__c lm=new Accomodation__c();
        lm.Price__c=qli.ListPrice;
       
        lm.Name=qli.PriceBookEntry.Name;
        lm.OpportunityLineItem__c=to.id;
        lstAccomodation.add(lm);
      
    }
    if(lstAccomodation != null && !lstAccomodation.isEmpty()){
        insert lstAccomodation;
    }
}

 

In this line lm.OpportunityLineItem__c=to.id; am having error

Error: Compile Error: Invalid field OpportunityLineItem__c for SObject Accomodation__c at line 13 column 9 

because Accomodation object&nbsp;need an lookup relationship to OpportunityLineItem.</script>

but there is no lookup relationship to OpportunityLineItem

 How to solve it

How to write test class for trigger

am new to apex

 

trigger check on Order__c (after insert) {
Order__c to=trigger.new[0];

Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];


for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.Order__c=to.id;
insert lm;
}
}

Field is not writeable:Invoice__c.Id

 

public class SampleAcc {

   static testMethod void testAccTrigger(){
  
 Pricebook2 s = [select id from Pricebook2 where IsStandard = true];  
  
 // create the product
    Product2 p1 = new Product2(
        name='Test Product 1',
        IsActive=true,
        Description='My Product',
        ProductCode='12345'
    );
    insert p1; 
   
    PricebookEntry pbe1 = new PricebookEntry(
        Pricebook2Id=s.id,
        Product2Id=p1.id,
        UnitPrice=0.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe1;  
   
 Opportunity opp1 = new Opportunity(Name='ww',StageName='Qualification',CloseDate=date.today());
 insert opp1;
  
 OpportunityLineItem oli = new OpportunityLineItem();
 oli.Quantity = 1;
    oli.TotalPrice = 1;
    oli.PricebookEntryId = pbe1.id;
    oli.OpportunityId = opp1.id;   
    insert oli; 
 
 Invoice__c inv =new Invoice__c();
 inv.Id=opp1.id;
 inv.name = 'ww';
 insert inv;
  
   Accomodation__C acc = new Accomodation__C();
   acc.name='ww';

   Test.startTest();
   insert acc;
   Test.stopTest();
 }
}

 

shows error in this line

inv.Id=opp1.id;

Field is not writeable:Invoice__c.Id

 

am new to apex .please solve it

how to get opportunity products into my related list custom object

 

For adding particular products in opportunity products related list alternatively tat appropriate products added into another related list custom object.

 

Please help:is it possible

iam new to apex.urgent need of test class for apex class

 

my apex class

 

public class MyController {
public ID accountId {get;set;}
  public String email {get;set;}
public String getName() {
return 'MyController';
}
public Opportunity getOpportunity() {
return [select Account.Id, Account.name, Account.G_English_Name__c, Account.B_English_Name__c, Contact__r.name, Contact__r.MailingStreet,Contact__r.MailingCity,
        Contact__r.MailingState, Contact__r.MailingPostalCode, Contact__r.MailingCountry from Opportunity
where id = :ApexPages.currentPage().getParameters().get('id')];
}


public PageReference sendPdf() {

    PageReference pdf = Page.pdfgeneratortemplate;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',accountId);
 
    // the contents of the attachment from the pdf
    Blob body;
 
    try {
 
      // returns the output of the page as a PDF
      body = pdf.getContent();
 
    // need to pass unit test -- current bug 
    } catch (VisualforceException e) {
      body = Blob.valueOf('Some Text');
    }
 
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('testPdf.pdf');
    attach.setInline(false);
    attach.Body = body;
 
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses(new String[] { 'anand.2861985@gmail.com' });
    mail.setSubject('PDF Email Demo');
    mail.setHtmlBody('Here is the email you requested! Check the attachment!');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
 
    // Send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email));
 
    return null;
 
  }
 
}

"New" button in apex class is not displayed in my enterprise edition How to get "New" button in enterprise edition or else which edition will display "New" button
In salesforce Production/Developer edition test coverage shows only 13%.But same code in trail edition test coverage shows 88% And salesforce Production/Developer edition system.assert shows error "variable doesnot exist But in Trail edition no errror Code Visualforce page DocumentEmailer Apex class DocumentEmailController public with sharing class DocumentEmailController { public ID documentId {get;set;} public String email {get;set;} public List documents { get { if (documents == null) { documents = new List(); documents.add(new SelectOption('01570000001NZDn','Cup of Coffee? - DOC')); documents.add(new SelectOption('01570000001NZDi','Workflow Cheatsheet - PDF')); } return documents; } set; } public PageReference sendDoc() { Document doc = [select id, name, body, contenttype, developername, type from Document where id = :documentId]; Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); attach.setContentType(doc.contentType); attach.setFileName(doc.developerName+'.'+doc.type); attach.setInline(false); attach.Body = doc.Body; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setUseSignature(false); mail.setToAddresses(new String[] { email }); mail.setSubject('Document Email Demo'); mail.setHtmlBody('Here is the email you requested: '+doc.name); mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); // Send the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Document sent to '+email)); return null; } } Test code Test_DocumentEmailer @isTest private class Test_DocumentEmailer { static Document document; static { document = new Document(); document.Body = Blob.valueOf('Some Text'); document.ContentType = 'application/pdf'; document.DeveloperName = 'my_document'; document.IsPublic = true; document.Name = 'My Document'; document.FolderId = [select id from folder where name = 'My Test Docs'].id; insert document; } static testMethod void testDocumentEmailer() { PageReference pref = Page.DocumentEmailer; DocumentEmailController con = new DocumentEmailController(); Test.startTest(); System.assertEquals(2,con.documents.size()); // populate the field with values con.documentId = document.id; con.email = 'test@noemail.com'; // submit the request pref = con.sendDoc(); Test.stopTest(); } } In test code System.assertEquals(2,con.documents.size()); shows error Variable doesnot exist documents
urgent need sample apex class with apex test method

For eg

 

Iam having Goods custom object.it has two fields Goods Name and Goods Category.

In Account object am having lookupfield Goods.In tat lookup it shows all existing Goods Name and Goods Category.

but searching is possible only for Goods Name not for Goods Category.I want to search also with Goods Category.

How it is possible

For example

 

user inputs

First Name:aa

Last Name:bb

 

then the Account name field is filled up automatically  aa+bb if user saves tat account.is it possible or not

how to create a Master-Detail relationship field it shows error

 

You cannot create a new Master-Detail relationship on an existing custom object if records already exist. You must first create a Lookup relationship, populate the lookup field with data in all records, and then change the relationship type to Master-Detail.

urgent needed

How to get Goods__r.Price__c value it show only empty record

am new to apex

 

trigger calculate on Item__c (after insert, after update, after delete) {

// Use a map because it doesn't allow duplicate values 
   

Map<ID, Shipping_Invoice__C> updateMap = new Map<ID, Shipping_Invoice__C>();

// Set this integer to -1 if we are deleting 
   
Integer subtract ;

// Populate the list of items based on trigger type 
   
List<Item__c> itemList;
    if(trigger.isInsert || trigger.isUpdate){
        itemList = trigger.new;
        subtract = 1;
    }
    else if(trigger.isDelete)
    {
        // Note -- there is no trigger.new in delete 
   
        itemList = trigger.old;
        subtract = -1;
    }

// Access all the information we need in a single query  
   
// rather than querying when we need it. 
   
// This is a best practice for bulkifying requests 
   

set<Id> AllItems = new set<id>();

for(item__c i :itemList){
// Assert numbers are not negative.   
   
// None of the fields would make sense with a negative value 
   

System.assert(i.quantity__c > 0, 'Quantity must be positive');
System.assert(i.weight__c >= 0, 'Weight must be non-negative');


// If there is a duplicate Id, it won't get added to a set 
   
AllItems.add(i.Shipping_Invoice__C);
}

// Accessing all shipping invoices associated with the items in the trigger 
   
List<Shipping_Invoice__C> AllShippingInvoices = [SELECT id, ShippingDiscount__c,
                   SubTotal__c, TotalWeight__c, Tax__c, GrandTotal__c
                   FROM Shipping_Invoice__C where id in :AllItems];
                  
// Take the list we just populated and put it into a Map.   
   
// This will make it easier to look up a shipping invoice 
   
// because you must iterate a list, but you can use lookup for a map,  
   
Map<ID, Shipping_Invoice__C> SIMap = new Map<ID, Shipping_Invoice__C>();

for(Shipping_Invoice__C sc : AllShippingInvoices)
{
    SIMap.put(sc.id, sc);
}

// Process the list of items 
   
    if(trigger.isUpdate)
    {
        // Treat updates like a removal of the old item and addition of the          
   
        // revised item rather than figuring out the differences of each field  
   
        // and acting accordingly. 
   
        // Note updates have both trigger.new and trigger.old 
   
        for(integer x = 0; x < trigger.old.size(); x++)
        {
            Shipping_Invoice__C myOrder;
            myOrder = SIMap.get(trigger.old[x].Shipping_Invoice__C);

            // Decrement the previous value from the subtotal and weight. 
   
            myOrder.SubTotal__c -= (trigger.old[x].Goods__r.Price__c *
                                    trigger.old[x].quantity__c);
            myOrder.TotalWeight__c -= (trigger.old[x].weight__c *
                                       trigger.old[x].quantity__c);
               
            // Increment the new subtotal and weight. 
   
            myOrder.SubTotal__c += (trigger.new[x].Goods__r.Price__c *
                                    trigger.new[x].quantity__c);
            myOrder.TotalWeight__c += (trigger.new[x].weight__c *
                                       trigger.new[x].quantity__c);
        }
       
        for(Shipping_Invoice__C myOrder : AllShippingInvoices)
        {
           
            // Set tax rate to 9.25%  Please note, this is a simple example.   
   
            // Generally, you would never hard code values. 
   
            // Leveraging Custom Settings for tax rates is a best practice.   
   
            // See Custom Settings in the Apex Developer's guide  
   
            // for more information. 
   
            myOrder.Tax__c = myOrder.Subtotal__c * .0925;
           
            // Reset the shipping discount 
   
            myOrder.ShippingDiscount__c = 0;
   
            // Set shipping rate to 75 cents per pound.   
   
            // Generally, you would never hard code values. 
   
            // Leveraging Custom Settings for the shipping rate is a best practice. 
   
            // See Custom Settings in the Apex Developer's guide  
   
            // for more information. 
   
            myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
            myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c +
                                    myOrder.Shipping__c;
            updateMap.put(myOrder.id, myOrder);
         }
    }
    else
    {
        for(Item__c itemToProcess : itemList)
        {
            Shipping_Invoice__C myOrder;
   
            // Look up the correct shipping invoice from the ones we got earlier 
   
            myOrder = SIMap.get(itemToProcess.Shipping_Invoice__C);
            if(itemToProcess.Goods__r.Price__c != null){
            myOrder.SubTotal__c += (itemToProcess.Goods__r.Price__c *
                                   itemToProcess.quantity__c * subtract);
            }
            myOrder.TotalWeight__c += (itemToProcess.weight__c *
                                       itemToProcess.quantity__c * subtract);
        }
       
        for(Shipping_Invoice__C myOrder : AllShippingInvoices)
        {
           
             // Set tax rate to 9.25%  Please note, this is a simple example.   
   
             // Generally, you would never hard code values. 
   
             // Leveraging Custom Settings for tax rates is a best practice.   
   
             // See Custom Settings in the Apex Developer's guide  
   
             // for more information. 
   
             myOrder.Tax__c = myOrder.Subtotal__c * .0925;
            
             // Reset shipping discount 
   
             myOrder.ShippingDiscount__c = 0;
   
            // Set shipping rate to 75 cents per pound.   
   
            // Generally, you would never hard code values. 
   
            // Leveraging Custom Settings for the shipping rate is a best practice. 
   
            // See Custom Settings in the Apex Developer's guide  
   
            // for more information. 
   
            myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
            myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c +
                                    myOrder.Shipping__c;
                                      
            updateMap.put(myOrder.id, myOrder);
   
         }
     }   
    
     // Only use one DML update at the end. 
   
     // This minimizes the number of DML requests generated from this trigger. 
   
     update updateMap.values();
}

Birthday: Only current and future year displayed and it does not make sense for birthday using date custom field.how to get past years

In opportunity object having an standard related list "products".And another custom related list am using is "Accomodation ".

In Products related list ,if am adding products using add product button same products will be added into custom related list  "Accomodation" also at the same time.

so my trigger in OpportunityLineItem is

 

trigger accc on OpportunityLineItem (before insert,after insert) {
List<Accomodation__c> lstAccomodation = new List<Accomodation__c>();
    OpportunityLineItem  to=trigger.new[0];

   Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
    list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Product2.Family,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];

    for(OpportunityLineItem qli:ql){
        Accomodation__c lm=new Accomodation__c();
        lm.Price__c=qli.ListPrice;
       
        lm.Name=qli.PriceBookEntry.Name;
        lm.OpportunityLineItem__c=to.id;
        lstAccomodation.add(lm);
      
    }
    if(lstAccomodation != null && !lstAccomodation.isEmpty()){
        insert lstAccomodation;
    }
}

 

In this line lm.OpportunityLineItem__c=to.id; am having error

Error: Compile Error: Invalid field OpportunityLineItem__c for SObject Accomodation__c at line 13 column 9 

because Accomodation object&nbsp;need an lookup relationship to OpportunityLineItem.</script>

but there is no lookup relationship to OpportunityLineItem

 How to solve it

How to write test class for trigger

am new to apex

 

trigger check on Order__c (after insert) {
Order__c to=trigger.new[0];

Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];


for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.Order__c=to.id;
insert lm;
}
}

"New" button in apex class is not displayed in my enterprise edition How to get "New" button in enterprise edition or else which edition will display "New" button
In salesforce Production/Developer edition test coverage shows only 13%.But same code in trail edition test coverage shows 88% And salesforce Production/Developer edition system.assert shows error "variable doesnot exist But in Trail edition no errror Code Visualforce page DocumentEmailer Apex class DocumentEmailController public with sharing class DocumentEmailController { public ID documentId {get;set;} public String email {get;set;} public List documents { get { if (documents == null) { documents = new List(); documents.add(new SelectOption('01570000001NZDn','Cup of Coffee? - DOC')); documents.add(new SelectOption('01570000001NZDi','Workflow Cheatsheet - PDF')); } return documents; } set; } public PageReference sendDoc() { Document doc = [select id, name, body, contenttype, developername, type from Document where id = :documentId]; Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); attach.setContentType(doc.contentType); attach.setFileName(doc.developerName+'.'+doc.type); attach.setInline(false); attach.Body = doc.Body; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setUseSignature(false); mail.setToAddresses(new String[] { email }); mail.setSubject('Document Email Demo'); mail.setHtmlBody('Here is the email you requested: '+doc.name); mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); // Send the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Document sent to '+email)); return null; } } Test code Test_DocumentEmailer @isTest private class Test_DocumentEmailer { static Document document; static { document = new Document(); document.Body = Blob.valueOf('Some Text'); document.ContentType = 'application/pdf'; document.DeveloperName = 'my_document'; document.IsPublic = true; document.Name = 'My Document'; document.FolderId = [select id from folder where name = 'My Test Docs'].id; insert document; } static testMethod void testDocumentEmailer() { PageReference pref = Page.DocumentEmailer; DocumentEmailController con = new DocumentEmailController(); Test.startTest(); System.assertEquals(2,con.documents.size()); // populate the field with values con.documentId = document.id; con.email = 'test@noemail.com'; // submit the request pref = con.sendDoc(); Test.stopTest(); } } In test code System.assertEquals(2,con.documents.size()); shows error Variable doesnot exist documents
urgent need sample apex class with apex test method

For example

 

user inputs

First Name:aa

Last Name:bb

 

then the Account name field is filled up automatically  aa+bb if user saves tat account.is it possible or not