• Assaf
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hi all,

I wrote a simple test class and I get an Error saying : 'PriceBookEntry is in a different PriceBook than the one assigned to the Opportunity'. (the Error is in the line saying  OpportunityLineItem OppLI_Rec = new Opportunity(...);
                                                                       insert OppLI_Rec;

Code:
public class Test_updateQuarterToOppProd {

static testMethod void UpdateQuarter(){

//insert a new product
Product2 p = new product2(name='x');
insert p;

Product2 p2 = new product2(name='y');
insert p2;

//define the standart price for the product
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
insert new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0, isActive=true);

Pricebook2 pb = new pricebook2(name='test');
insert pb;

PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id,unitprice=1.0, isActive=true);
insert pbe;


Account AcntRec = new Account(name = 'Acc', Type = 'Customer', Industry = 'Automotive');
insert AcntRec;

Opportunity OppRecA = new Opportunity(AccountId = AcntRec.Id, name='Opp', stageName='Open', closeDate=Date.newInstance(2008,10,10), revenue_recognition__c = 'Q1');
insert OppRecA;

OpportunityLineItem OppLI_Rec = new OpportunityLineItem(OpportunityId = OppRecA.Id, pricebookentryid=pbe.id, UnitPrice = 100, Quantity = 3);
insert OppLI_Rec;


OppRecA.revenue_recognition__c = 'Q2';
update OppRecA;

system.assertEquals('Q2',[select Quarter__c from OpportunityLineItem where Id = :OppLI_Rec.Id].Quarter__c);
}

}

 I can't find the problem in here.

I appreciate your help.

Thanks, Assaf



  • September 28, 2008
  • Like
  • 0
Hello all,

I'm trying to retrieve the picklist value of  different quoteLines using a query and then set a field to "Yes" in case there is at list one picklist value equals to "Service & Other".

Product_Family__c is the picklist field.

the problem is that records[i] holds more data than the picklist value itself.


Code:
var soql = "select Product_Family__c  from SFDC_520_QuoteLine__c where Quote__c = '{!SFDC_520_Quote__c.Id}'";
var queryResult = sforceClient.Query(soql);

var records = queryResult.records;
var flag = true;
for(var i=0;i<records.length&&flag;i++)
{
 if(records[i]=="Service & Other")
 {
  upd.set("PSO_Needed__c","Yes");
  flag = false;
 }
}

how can I get the picklist value itself?

Thank you all.

Assaf
 



Message Edited by Assaf on 09-10-2008 11:38 PM
  • September 07, 2008
  • Like
  • 0
Hello all,

my first thread in the community forum...:smileyvery-happy:

I wrote a trigger in the sandbox environment and a test class that invokes that trigger.
the trigger works perfectly, and got 100% coverage.

the problem is that other triggers that were already in which I didn't wrote/change, didn't  pass the tests.

those triggers were already in the environment, I didn't change a thing. I assumed that someone wrote test classes for them in order to deploy them to the production.

anyway, I understood that I need to write a test class that invokes the trigger I just wrote, which I did.

what am I missing here?

Thank a lot , Assaf
  • July 15, 2008
  • Like
  • 0

I have a requirement to code an Apex Trigger that fires with a CaseComment changes and SalesForce Premier Support says this is possible once I learn the tricks so I am here asking about those tricks.

 

Thanks in advance for the help.

I have the following Trigger, which I want to use to update ownership of 2 custom objects (Business_Unit__c & Sub_Unit__c) when a user role changes. The role and it's corresponding BU / Sub BU are fetched and should be updated with this new UserId as the owner.
 
Code:
trigger RoleUpdateBuOwner on User (after update) {
 
 Map<ID,User> trUsersNew = Trigger.newMap;
 Map<ID,User> trUsersOld = Trigger.oldMap;
 
 Set<String> allUpdatedRoles = new Set<String>(); 
 
 Map<Id,Business_Unit__c> allBu = new Map<Id,Business_Unit__c>([SELECT Id,Name,OwnerId FROM Business_Unit__c]);
 Map<Id,Sub_Unit__c> allSubBu = new Map<Id,Sub_Unit__c>([SELECT Id,Name,OwnerId FROM Sub_Unit__c]);
 
 Integer numBuUpdated = 0;
 List<Business_Unit__c> buToUpdate = new List<Business_Unit__c>();
 for(Business_Unit__c aBu : allBu.values()) {
  for(User userWithNewRole : trUsersNew.values()) {
   if(userWithNewRole.Id != aBu.OwnerId) {
    if(userWithNewRole.Role_Name__c.contains(aBu.Name)  && userWithNewRole.Role_Name__c.contains('Plant Manager')) {
     aBu.OwnerId = userWithNewRole.Id; 
     buToUpdate.add(aBu);
     numBuUpdated++;
    }
   }  
  }
 }
  
 Integer numSubBuUpdated = 0;
 List<Sub_Unit__c> subBuToUpdate = new List<Sub_Unit__c>();
 for(Sub_Unit__c aSubBu : allSubBu.values()) {
  for(User userWithNewRole : trUsersNew.values()) {
   if(userWithNewRole.UserRoleId == null) continue;
   if(userWithNewRole.Id != aSubBu.OwnerId) {
    if(userWithNewRole.Role_Name__c.contains(aSubBu.Name) && userWithNewRole.Role_Name__c.contains('Plant Manager')) {
     aSubBu.OwnerId = userWithNewRole.Id; 
     subBuToUpdate.add(aSubBu);
     numSubBuUpdated++;
    }
   }  
  }
 }
 update subBuToUpdate;
 update buToUpdate;
 
 /*
 // For testing
 for(User thisUser : Trigger.new) {
  thisUser.addError('Updating '+numBuUpdated+' BU and '+numSubBuUpdated+' Sub BU');
 }
 */
 
}

 
Now, when I update a user role, the following error appears:
 
Code:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger RoleUpdateBuOwner caused an unexpected exception, contact your administrator: RoleUpdateBuOwner: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0FR00000007OWzMAM; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object: DML operation on non-setup object is not supported after you have updated a setup object: Business_Unit__c: [Id]: Trigger.RoleUpdateBuOwner: line 40, column 5

 
What is wrong with my trigger (that causes this error:))?


Message Edited by Harmpie on 12-12-2008 05:35 AM
Hi all,

I wrote a simple test class and I get an Error saying : 'PriceBookEntry is in a different PriceBook than the one assigned to the Opportunity'. (the Error is in the line saying  OpportunityLineItem OppLI_Rec = new Opportunity(...);
                                                                       insert OppLI_Rec;

Code:
public class Test_updateQuarterToOppProd {

static testMethod void UpdateQuarter(){

//insert a new product
Product2 p = new product2(name='x');
insert p;

Product2 p2 = new product2(name='y');
insert p2;

//define the standart price for the product
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
insert new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0, isActive=true);

Pricebook2 pb = new pricebook2(name='test');
insert pb;

PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id,unitprice=1.0, isActive=true);
insert pbe;


Account AcntRec = new Account(name = 'Acc', Type = 'Customer', Industry = 'Automotive');
insert AcntRec;

Opportunity OppRecA = new Opportunity(AccountId = AcntRec.Id, name='Opp', stageName='Open', closeDate=Date.newInstance(2008,10,10), revenue_recognition__c = 'Q1');
insert OppRecA;

OpportunityLineItem OppLI_Rec = new OpportunityLineItem(OpportunityId = OppRecA.Id, pricebookentryid=pbe.id, UnitPrice = 100, Quantity = 3);
insert OppLI_Rec;


OppRecA.revenue_recognition__c = 'Q2';
update OppRecA;

system.assertEquals('Q2',[select Quarter__c from OpportunityLineItem where Id = :OppLI_Rec.Id].Quarter__c);
}

}

 I can't find the problem in here.

I appreciate your help.

Thanks, Assaf



  • September 28, 2008
  • Like
  • 0
Hello all,

I'm trying to retrieve the picklist value of  different quoteLines using a query and then set a field to "Yes" in case there is at list one picklist value equals to "Service & Other".

Product_Family__c is the picklist field.

the problem is that records[i] holds more data than the picklist value itself.


Code:
var soql = "select Product_Family__c  from SFDC_520_QuoteLine__c where Quote__c = '{!SFDC_520_Quote__c.Id}'";
var queryResult = sforceClient.Query(soql);

var records = queryResult.records;
var flag = true;
for(var i=0;i<records.length&&flag;i++)
{
 if(records[i]=="Service & Other")
 {
  upd.set("PSO_Needed__c","Yes");
  flag = false;
 }
}

how can I get the picklist value itself?

Thank you all.

Assaf
 



Message Edited by Assaf on 09-10-2008 11:38 PM
  • September 07, 2008
  • Like
  • 0
Hello all,

my first thread in the community forum...:smileyvery-happy:

I wrote a trigger in the sandbox environment and a test class that invokes that trigger.
the trigger works perfectly, and got 100% coverage.

the problem is that other triggers that were already in which I didn't wrote/change, didn't  pass the tests.

those triggers were already in the environment, I didn't change a thing. I assumed that someone wrote test classes for them in order to deploy them to the production.

anyway, I understood that I need to write a test class that invokes the trigger I just wrote, which I did.

what am I missing here?

Thank a lot , Assaf
  • July 15, 2008
  • Like
  • 0