• ian b goode
  • NEWBIE
  • 55 Points
  • Member since 2012

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

Hello,

 

I have a trigger that fires on OpportunityLineItem and updates the associated Opportunity with a count of Opportuity Products by grouping and a sum of their TotalPrice by grouping.  I have a custom field on the Opportunity called Coach_Count__c (gives number of products that have Prod_Grp__c = "Coaching") and one called Coach_Dol__c (sums the TotalPrice field for all products that have Prod_Grp__c = "Coaching").  I also created a custom field in the Opportunity Product called Count__c, which is simply a "1" and I sum that to get the count, and I created another field called Total_Price__c which is a formula field that has the value of TotalPrice from the Opportunity Product.  My trigger works as expected when adding or editing Products as both the Coach_Count__c and Coach_Dol__c fields get updated.  The Coach_Count__c field also updates properly when deleting products.  However, the Coach_Dol__c fails to update when there are multiple products where Prod_Grp__c = "Coaching" and will only update when there are none left.  Below is the Trigger.Old section of my trigger.  Can anyone tell me why I cannot get it to update when there are multiple products that have the same Prod_Grp__c value?

 

Map<id,id> M= new map<id,id>();
For(OpportunityLineItem deliv2:trigger.old){

oppIds.add(deliv2.OpportunityId);
m.put(deliv2.opportunityid,deliv2.id);
}
opp = [SELECT Id, Coach_Dol__c,Coach_Count__c
FROM Opportunity
WHERE Id IN:oppIds];

for(Opportunity o: opp){
if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Count__c-=trigger.oldmap.get(M.get(o.id)).Count__c;

else if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Dol__c-=trigger.oldmap.get(M.get(o.id)).Tot_Price__c;

}
Update (Opp);

  • February 27, 2013
  • Like
  • 0

Anyone else having trouble with IDE version 27 not building after save?

 

Tried everything setting I could find but the only way was to "Save to server' after every change.

 

Needless to say that got pretty tedious so I went back to version 26 and all is well. Was it just me?

Anyone else having trouble with IDE version 27 not building after save?

 

Tried everything setting I could find but the only way was to "Save to server' after every change.

 

Needless to say that got pretty tedious so I went back to version 26 and all is well. Was it just me?

Hello,

 

I have a trigger that fires on OpportunityLineItem and updates the associated Opportunity with a count of Opportuity Products by grouping and a sum of their TotalPrice by grouping.  I have a custom field on the Opportunity called Coach_Count__c (gives number of products that have Prod_Grp__c = "Coaching") and one called Coach_Dol__c (sums the TotalPrice field for all products that have Prod_Grp__c = "Coaching").  I also created a custom field in the Opportunity Product called Count__c, which is simply a "1" and I sum that to get the count, and I created another field called Total_Price__c which is a formula field that has the value of TotalPrice from the Opportunity Product.  My trigger works as expected when adding or editing Products as both the Coach_Count__c and Coach_Dol__c fields get updated.  The Coach_Count__c field also updates properly when deleting products.  However, the Coach_Dol__c fails to update when there are multiple products where Prod_Grp__c = "Coaching" and will only update when there are none left.  Below is the Trigger.Old section of my trigger.  Can anyone tell me why I cannot get it to update when there are multiple products that have the same Prod_Grp__c value?

 

Map<id,id> M= new map<id,id>();
For(OpportunityLineItem deliv2:trigger.old){

oppIds.add(deliv2.OpportunityId);
m.put(deliv2.opportunityid,deliv2.id);
}
opp = [SELECT Id, Coach_Dol__c,Coach_Count__c
FROM Opportunity
WHERE Id IN:oppIds];

for(Opportunity o: opp){
if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Count__c-=trigger.oldmap.get(M.get(o.id)).Count__c;

else if(trigger.oldmap.get(M.get(o.id)).Prod_Grp__c=='Coaching')
o.Coach_Dol__c-=trigger.oldmap.get(M.get(o.id)).Tot_Price__c;

}
Update (Opp);

  • February 27, 2013
  • Like
  • 0

Hi,
In visualforce, there's code like

This can be used for string output formatting.

Is there simillar one in Apex, like

Dicimal d = 4000.0;
How can I convert it to a string like '4,000.0'

I tried 2 ways, both failed
1) d.format() , this will be displayed like '4,000'
2) String.format: don't know how to use this API. There's no document from SFDC about this

How to change an existing field's type from Master-Detail relationship type to Look-up relationship type?

 

I am unable to change the datatype of the field.

 

Or do I need to create a new field of look-up relationship type and delete the existing field of master-detail relationship type?

 

 

Can anyone help?

 

 

Abhijeet

public class RelationshipMgr {


    public static void createRelationship(Relationship__c relArgs) {
    
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relArgs.RecordType);
        
        // First ,get corresponding "Relationship Type" from the given "Relationship Category", we
        // need to query from "Relationship Category" obj, "relArgs.Relationship_Category__c"
        // is a Lookup, thus, it contains system ID (Id field) of the "Relationship_Category" obj
        // not the "Name" field...
        List<Relationship_Category__c> relCats = [select Relationship_Type__c, Reverse_Relationship_Category__c, Name, Reversible__c from Relationship_Category__c where Id =: relArgs.Relationship_Category__c];
  
        // TEMP CODE: to be removed or cleaned...      
        if (relCats.IsEmpty()) {
            System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: IsEmpty');    
        }
        
        Relationship_Category__c relCatTemp = relCats.get(0);
 
        
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relCatTemp.Relationship_Type__c);
 
        
        // At the highest level, we group our business rules by "Relationship Type", inside each
        // top-level if/else clause implements BRs specific to each "Relationship Type"...        

        if (relCatTemp.Relationship_Type__c == 'Account to Account') {

            // "Account to Account", all business rules for this case here...
        
            // create second paried (reverse) record...    
            Relationship__c relReverse = new Relationship__c();
            relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
            relReverse.Account_ID__c = relArgs.Rev_Account_ID__c;
            relReverse.Rev_Account_ID__c = relArgs.Account_ID__c;
            relReverse.IsTrigger__c = '1';
            
            //insert relReverse;
            
      
            // since, we only submit one obj, therefore, only one obj returned in SaveResult[] array...
            Relationship__c[] relTemps = new Relationship__c[1];
            relTemps[0] = relReverse;
            Database.SaveResult[] result = Database.Insert(relTemps);
            
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relTemps[0].Id);
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + result[0].Id);
            
            // now, we need to update the parent "Relationship" record (relArgs) with the new "Id" of the
            // reversed record just inserted...
            Relationship__c relArgsCloned = relArgs.clone(true, true);
            relArgsCloned.Rev_Relationship__c =  result[0].Id;
            update relArgsCloned;            
        
        } else if (relCatTemp.Relationship_Type__c == 'Account to Contact') {

            // "Account to Contact", all business rules for this case here...
     
            // First, we need to check of the underlying Contact record has a value in the "Account Name"
            // field, if NOT, then we need to populate it...
            List<Contact> contacts = [select AccountId from Contact where Id =: relArgs.Contact_ID__c];
            
            if (contacts.IsEmpty()) {
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
            }
        
            Contact contactTemp = contacts.get(0);
            
            //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + contactTemp.AccountId);
            
            if (contactTemp.AccountId == null) {
            
                List<Account> accounts = [select Id from Account where Id =: relArgs.Account_ID__c];
            
                if (accounts.IsEmpty()) {
                    System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: accounts IsEmpty');    
                }
            
                Account accountTemp = accounts.get(0);
                contactTemp.AccountId = accountTemp.Id;
                
                update contactTemp;
            }

            Relationship__c relReverse = new Relationship__c();
            
            // Next, we need to check whether the "Relationship" is created from Account or Contact page,
            // the ID assignment rule differs according to the source page...
            if (relArgs.Account_ID__c != null) {
                // "Relationship" is created from "Account" page (Account ID available)...
                relReverse.Contact_ID__c = relArgs.Rev_Contact_ID__c;
                relReverse.Rev_Account_ID__c = relArgs.Account_ID__c; 
                     
            }
            else if (relArgs.Contact_ID__c != null) {
                // "Relationship" is created from "Contact" page (Account ID available)...
                relReverse.Rev_Contact_ID__c = relArgs.Contact_ID__c;
                relReverse.Account_ID__c = relArgs.Rev_Account_ID__c; 

            }
            else {
                // invalid case, not supposed to happen, thus raise the exception...
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: either Account ID or Contact ID must exist...');    
            
            }
            
           
            // Now, create second paired (reverse) record...
            relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
            relReverse.IsTrigger__c = '1';
            insert relReverse;

        } else if (relCatTemp.Relationship_Type__c == 'Contact to Contact') {

            // "Contact to Contact", all business rules for this case here...

            // create second paired (reverse) record...
            Relationship__c relReverse = new Relationship__c();
            
            
            // First, we need to check if the parent Contact has Account or not (AccountId field null),
            // if NO, then we need to auto link with the same Account as the reversed Contact...
            List<Contact> contacts = [select AccountId from Contact where Id =: relArgs.Contact_ID__c];
            
            if (contacts.IsEmpty()) {
                System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
            }
        
            Contact contactParent = contacts.get(0);
         
            if (contactParent.AccountId == null) {
                // the parent Contact does NOT have Account, we must copy the AccountId from the reversed Contact
                // into this parent Contact...            
                contacts = [select AccountId from Contact where Id =: relArgs.Rev_Contact_ID__c];
            
                if (contacts.IsEmpty()) {
                    System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
                }
            
                Contact contactTempReverse = contacts.get(0);
                contactParent.AccountId = contactTempReverse.AccountId;                
 
                update contactParent;
            }


            // Next, we need to check against "Reversible" flag (checkbox), normal cases are NOT reversible, if it's
            // reversible, then it's the case like "Is Married To" (we follow screen here, which a little conflict
            // with our code namving convention)...
            if (!relCatTemp.Reversible__c) {
            
                // Next, we need to check if this is "Has Child" relationship category, if yes,
                // then we need to figure out the parent Contact record whether it's "Male" or
                // "Female" via the "Sex" field, note that, the assumption here is that the "Sex"
                // field is always has value (required field in Contact object)...
                
                // note, we need to use "Relationship Category" obj, not "Relationship", because the "Relationship"
                // obj contains the system Id not actual text...
                if (relCatTemp.Name == 'Has Child') {
                
                    contacts = [select Sex__c from Contact where Id =: relArgs.Contact_ID__c];
                
                    if (contacts.IsEmpty()) {
                        System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: contacts IsEmpty');    
                    }
                
                    Contact contactTemp = contacts.get(0);
    
        
                    //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + contactTemp.Sex__c);
    
                    // we need to get the system "Id", because "Relationship" obj uses "Lookup" for the "Relationship Category"
                    // field...
                    List<Relationship_Category__c> relCatTemps = [select Id, Name from Relationship_Category__c where Name='Has Father' or Name='Has Mother'];
    
                    if (relCatTemps .IsEmpty()) {
                        System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: Relationship Category IsEmpty');    
                    }
                
                    String strRelCatIdFather = '';
                    String strRelCatIdMother = '';
                
                    for (Relationship_Category__c relCatTemp2 : relCatTemps) {
                        
                        if (relCatTemp2.Name == 'Has Father')
                            strRelCatIdFather = relCatTemp2.Id;
                        else if (relCatTemp2.Name == 'Has Mother')
                            strRelCatIdMother = relCatTemp2.Id;    
                    }
       
                    // now, assign corresponding "Relationship Category to the reverse "Relationship" obj...
                    if (contactTemp.Sex__c == 'Male')               
                        relReverse.Relationship_Category__c  = strRelCatIdFather; //'Has Father';
                    else if (contactTemp.Sex__c == 'Femail')
                        relReverse.Relationship_Category__c  = strRelCatIdMother; //'Has Mother';                   
                    else
                        relReverse.Relationship_Category__c  = strRelCatIdMother; //'default to Has Mother';
    
                }
                else {
                
                    relReverse.Relationship_Category__c = relCatTemp.Reverse_Relationship_Category__c;
    
                }
                
            }
            else {
            
                // REVERSIBLE: this is for case like "Is Married To" when the "Reversible" field
                // is checked...
                relReverse.Relationship_Category__c = relArgs.Relationship_Category__c;
            
            }
 
            relReverse.Contact_ID__c = relArgs.Rev_Contact_ID__c;
            relReverse.Rev_Contact_ID__c = relArgs.Contact_ID__c;
            relReverse.IsTrigger__c = '1';            
            insert relReverse;

        }

    
    } // end method...

    
    // This method implements logical delete (Mark Delete) for the Relationship record. It's required
    // "Mark Delete" because of trigger limitation, which does NOT allow delete of the same record type
    // in the "before and after" event via the "delete sObject[]" operation.
    public static void markDeleteRelationship(Relationship__c relArgs) {
        
        //System.assertEquals('TOKEN1', 'TOKEN2', 'CUSTOM_MSG: ' + relArgs.Rev_Relationship__c + ' - ' + relArgs.Id);       

        // NOTE - 140611: WE NO LONGER NEED THIS FLAGMENT BECAUSE WE USE THE STANDARD DELETE FUNCTION TO PHYSICALLY
        // DELETE PARENT RECORD!!!
        // first, update the parent record as deleted...
        //Relationship__c relArgsCloned = relArgs.clone(true, true);
        //relArgsCloned.Mark_for_Delete__c = true;
        //update relArgsCloned;
       
        // now, we need update the paired record as deleted (Mark for Delete = true), the Apex scheduler will
        // perform the physical delete at scheduled interval...
        Relationship__c relTemp = new Relationship__c(Id=relArgs.Rev_Relationship__c);
        relTemp.Mark_for_Delete__c = true;
        update relTemp;             
    
    } // end method...


} // end class...

 

@isTest 
private class TestRelationshipMgr {
    
    static testMethod void myTest() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Account to Account';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      objRe.Account_ID__c = objAcc.id;
      //objRe.Contact_ID__c = objCon.id;
      objRe.Rev_Account_ID__c = objAcc.id;
      //objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
    
    static testMethod void myTest1() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Account to Contact';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      objRe.Account_ID__c = objAcc.id;
      objRe.Contact_ID__c = objCon.id;
     // objRe.Rev_Account_ID__c = objAcc.id;
      //objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
     static testMethod void myTest2() {
  
      Account objAcc = new Account();
      objAcc.Name = 'test';
      insert objAcc;
      
      Contact objcon = new Contact();
      objcon.LastName = 'test';
      objcon.AccountID = objAcc.ID;
      objcon.Sex__c = 'Male';
    
      insert objcon;
      
      Relationship_Category__c RC = new Relationship_Category__c();
      RC.Name = 'Test';
      RC.Relationship_Type__c = 'Contact to Contact';
      RC.Reverse_Relationship_Category__c = RC.id;
      RC.Reversible__c = true;
      insert RC;
      
      Relationship__c objRe = new Relationship__c ();
      //objRe.Account_ID__c = objAcc.id;
      objRe.Contact_ID__c = objCon.id;
     // objRe.Rev_Account_ID__c = objAcc.id;
      objRe.Rev_Contact_ID__c = objCon.id;
      objRe.IsTrigger__c = '1';
      objRe.Relationship_Category__c = RC.id;
      objRe.Mark_for_Delete__c  = true;
      
      test.starttest();
      insert objRe;
      test.stoptest();
     
    }
    
}

 

Code Coverage Low Please Help me!

Thank you so much.

  • June 17, 2011
  • Like
  • 0

I'm getting the attached error when attempting to install the latest Force.com IDE plugin on the latest Eclipse version with the latest JDK installed.  The error comes up after I click next on the "Available Software" selection screen of the "Install New Software" dialog.  How can I fix this problem?  Thank you!

 

 

Cannot complete the install because of a conflicting dependency.
  Software being installed: Force.com IDE 20.0.1.201011121559 (com.salesforce.ide.feature.feature.group 20.0.1.201011121559)
  Software currently installed: Shared profile 1.0.0.1284708747720 (SharedProfile_epp.package.java 1.0.0.1284708747720)
  Only one of the following can be installed at once: 
    Structured Source Editor 1.2.2.v201008232126 (org.eclipse.wst.sse.ui 1.2.2.v201008232126)
    Structured Source Editor 1.1.102.v200910200227 (org.eclipse.wst.sse.ui 1.1.102.v200910200227)
  Cannot satisfy dependency:
    From: Shared profile 1.0.0.1284708747720 (SharedProfile_epp.package.java 1.0.0.1284708747720)
    To: org.eclipse.wst.sse.ui [1.2.2.v201008232126]
  Cannot satisfy dependency:
    From: Force.com IDE 20.0.1.201011121559 (com.salesforce.ide.feature.feature.group 20.0.1.201011121559)
    To: org.eclipse.wst.html.ui [1.0.0,2.0.0)
  Cannot satisfy dependency:
    From: HTML UI Source Editor 1.0.401.v200908111935 (org.eclipse.wst.html.ui 1.0.401.v200908111935)
    To: bundle org.eclipse.wst.sse.ui [1.1.0,1.2.0)

 

 

  • February 03, 2011
  • Like
  • 0