• duke1
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 17
    Replies

How can I process a checkbox field in apex code (not with DeveloperForce)?  In my trigger I want to see if the field is checked and after processing it, uncheck the field.  I cannot find examples of this.  I want my user to check a checkbox of accounts to be processed, this will invoke the bulk trigger.  Can anyone help?  I can't find any examples after much searching.

  • August 31, 2010
  • Like
  • 0

I am trying to invoke apex code after a button is clicked.  I want to avoid using s-controls, and  I have looked at the following link.

 

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

 

I follow the idea, but I don't understand how to do what he says at the end:

 

All we need now is a custom Opportunity button. Because we used the Opportunity standard controller in the Visualforce page, we can simply have the button point to a the page we created.

Add that button to your Page Layout and all should work swimmingly.

 

In his example, how would I "point to the page we created"?

 

Thanks.

  • August 27, 2010
  • Like
  • 0

I have a trigger calling an @future method.  The trigger fires correctly, it correctly calls the @future method (processCarsTrans(SET <Id>), see below), but it appears the code inside the @future method never runs.  The first line in processCarsTrans is a System.debug line, which does not appear in the System log.  All I see in the log is:

 

10:12:54.502|METHOD_ENTRY|[35,13]|01pQ00000004lsH|SumCarsTransMethods.processCarsTrans(SET<Id>)
10:12:54.643|METHOD_EXIT|[35,13]|SumCarsTransMethods.processCarsTrans(SET<Id>)

 

It appears no code has run in the processCarsTrans method.  Shouldn't I be able to see the same kind of log with an @future method as I do with one that is not @future?  In the Syetem Log I do not a FUTURE line, indicating the async code is ever getting executed.  How do I get this @future method to run and log its behavior so I can test it?

 

 Any help is greatly appreciated.

  • July 25, 2010
  • Like
  • 0

I am trying to write a select statement like this:

 

[select ID, sum(field1) sum1, sum(field2) sum2, (sum1/sum2) from Account Group by ID]

 

Is it possible to embed a calculation like (sum1/sum2) in a select statement when I'm using aggregate functions?  Or can someone recommend how you could so this in an SOQL statement?  Thanks.

  • July 23, 2010
  • Like
  • 0

I have achieved 100% testing on my classes and trigger, but 0% for the system provided classes XMLDom and StartHere.  I have seen some ambiguous posts about this, but have not found source code I can download to simply test these classes.  Is there a simple solution to this issue.  This problem must come up for everyone who goes to deploy their first application; it seems logical for SalesForce to provide the test code if the classes are automatically included, so everyone doesn't have to solve the same problem over and over again.  Thanks for your help.

  • July 12, 2010
  • Like
  • 0

From what I can tell from error messages, you are not allowed to use aggregate functions in subqueries.  Because of this limitation, I have rearranged my query so the aggregate functions are in the main query, but I am getting errors on the relationships.  Can someone help me sort this out?

 

I have renamed Opportunity as Dealer (plural: Dealers).

I have a Master-Detail relationship between Dealer (master) and custom sObject Car (plural is Cars), where there are many Car sObjects to one Dealer, and many Dealers to one Account.

I am trying to use the aggregate functions to sum up certain fields in the Car sObject for each Dealer.  For example, sum all the Car_Number_Field_1__c fields for all the Cars associated with one Dealer; then elsewhere I will roll up all these Dealer sums for each Account.

 

There are ways to do this without aggregate functions, but I am interested in getting this to work using aggregate functions.  The following select statement in the for statement will not compile.  I get errors in the subquery where the complier does not understand the relationship with Dealers__r and it doesn't understand Dealer__c.  Can someone help me figure out how to get this to compile?  Thanks.

 

I have created a Set of relevant Accounts in accountIDs

 

Map<Id, List<Decimal>> carTotals = new Map<Id, List<Decimal>>();

for (AggregateResult aggregateData : [select Dealer__c,

            (select Id, AccountID from Dealers__r where Id = Dealer__c),
            SUM(Car_Number_Field_1__c) Car1Total, SUM(Car_Number_Field_2__c) Car2Total,
            SUM(Car_Number_Field_3__c) Car3Total from Car__c
            where AccountID in :accountIds Group by Dealer__c])  {

     List<Decimal> carSums = new List<Decimal>{(Decimal)aggregateData.Get('Car1Total'),
          (Decimal)aggregateData.Get('Car2Total'), (Decimal)aggregateData.Get('Car3Total')};
     carTotals.put(AccountID, carSums);

}

  • July 01, 2010
  • Like
  • 0

The essential information is:

 

Obect 1 has a master-detail relationship with Account , but is one-to-one with Account.

Object 1 also has a master-detail relationship with Opportunity that is one-to-many, (many Object1's to one Opportunity).

Each Opportunity has fields that need to be acted on in a for loop and then Object 1 is updated after that loop.

 

The original trigger essentially does the following:

 

create Set accountIDs from trigger.new

List<Object1> objList = [select Account__c ... from sObject1 where Account__c in :accountIDs]

for (Object1 acc : accountIDs) {

   // some code

    List<Opportunity> oppList = [select AccountID ... from sObject1 where AccountID = :accountIDs]

  // some more code

 change fields in objList

}  // end for

upsert objList

 

I need to take the SOQL statement out of the for loop, so the revised trigger does this:

 

create Set accountIDs from trigger.new

List<Object1> objList = [select Account__c ... from sObject1 where Account__c in :accountIDs]

// next line uses "in :accountIDs" instead of "= :accountIDs"

List<Opportunity> oppList = [select AccountID ... from sObject1 where AccountID in :accountIDs]

for (Object1 acc : accountIDs) {

   // some code

   for (Opportunity opp : oppList) {

       if (opp.AccountID == acc.Id) {

       // some code

       } // end if

   } // end inner for

  // some more code

 change fields in objList

}  // end outer for

upsert objList

 

The problem with the structure of the second trigger is that I am iterating over all the Opportunities every time, to find the relevant Opportunities with AccountID = acc.Id, which seems terribly inefficient.  I considered removing the "used" Opportunites after they have been processed, but then I'm still iterating over many irrelevant Opportunities on each loop.

 

There must be a better solution.  Can anyone help?  Thanks.

  • June 29, 2010
  • Like
  • 0

I am showing two triggers below; Trigger 2 at the bottom of the page works, except it performs the operation on all records, so I am trying in Trigger 1 to only access those records that have changed.  I have a lookup relationship between two custom objects Master_Lookup (parent) and Child_Detail (child), where a Master_Lookup could have many Child_Details, but a Child_Detail has only one Master_Lookup.  The first for loop using Trigger.new fills the set with a null value for the ID of the Master_Lookup instead of a valid ID.  I know there is a Master_Lookup associated with this Child_Detail so I can't understand why it has a null ID.  Can someone help me out.  Thanks.

 

Trigger 1:

trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Form a set of affected MasterLookupRecords to avoid duplicates
    Set<id> masterLookupIds = new Set<id>();
    for (Child_Detail__c myChildDetailRecord : Trigger.new){
        masterLookupIds.add(myChildDetailRecord.Master_Lookup__r.Id);
    }
    for (id value : masterLookupIds) {
        System.debug('Set = ' + value);
    }
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c where Id in :masterLookupIds';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup) {
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}

 

Trigger 2:
/*trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Use parent-child relationship to find all quantities for each Master_Lookup
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup){
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}*/

  • June 26, 2010
  • Like
  • 0

I'm trying to implement my first trigger.  I have a lookup relationship between MasterLookup and ChildDetail objects.  Here is the trigger:

 

trigger updateOnQtyCountTotal on Master_Lookup__c (before insert, before update) {
    // Use parent-child relationship to find all quantities for each Master_Lookup
    String queryString = 'SELECT Id, Total_Quantity__c, (SELECT Quantity__c from Child_Detail__r) from Master_Lookup__c';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup){
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningTotal += (Decimal)childRecord.get('Quantity__c');
            }
        }
        masterLookupRecord.Total_Quantity__c = runningTotal;
    }
}

 

The last line is not updating the Total_Quantity__c field when I view the MasterLookup object after making a change to the object.  It is clearly running the code in the trigger when I view the log, but the field is always blank.  This must be an obvious error, but I can't find it and searching the documentation has not helped.  Thanks.

  • June 25, 2010
  • Like
  • 0

How can I process a checkbox field in apex code (not with DeveloperForce)?  In my trigger I want to see if the field is checked and after processing it, uncheck the field.  I cannot find examples of this.  I want my user to check a checkbox of accounts to be processed, this will invoke the bulk trigger.  Can anyone help?  I can't find any examples after much searching.

  • August 31, 2010
  • Like
  • 0

I am trying to invoke apex code after a button is clicked.  I want to avoid using s-controls, and  I have looked at the following link.

 

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

 

I follow the idea, but I don't understand how to do what he says at the end:

 

All we need now is a custom Opportunity button. Because we used the Opportunity standard controller in the Visualforce page, we can simply have the button point to a the page we created.

Add that button to your Page Layout and all should work swimmingly.

 

In his example, how would I "point to the page we created"?

 

Thanks.

  • August 27, 2010
  • Like
  • 0

I have a trigger calling an @future method.  The trigger fires correctly, it correctly calls the @future method (processCarsTrans(SET <Id>), see below), but it appears the code inside the @future method never runs.  The first line in processCarsTrans is a System.debug line, which does not appear in the System log.  All I see in the log is:

 

10:12:54.502|METHOD_ENTRY|[35,13]|01pQ00000004lsH|SumCarsTransMethods.processCarsTrans(SET<Id>)
10:12:54.643|METHOD_EXIT|[35,13]|SumCarsTransMethods.processCarsTrans(SET<Id>)

 

It appears no code has run in the processCarsTrans method.  Shouldn't I be able to see the same kind of log with an @future method as I do with one that is not @future?  In the Syetem Log I do not a FUTURE line, indicating the async code is ever getting executed.  How do I get this @future method to run and log its behavior so I can test it?

 

 Any help is greatly appreciated.

  • July 25, 2010
  • Like
  • 0

I am trying to write a select statement like this:

 

[select ID, sum(field1) sum1, sum(field2) sum2, (sum1/sum2) from Account Group by ID]

 

Is it possible to embed a calculation like (sum1/sum2) in a select statement when I'm using aggregate functions?  Or can someone recommend how you could so this in an SOQL statement?  Thanks.

  • July 23, 2010
  • Like
  • 0

I have achieved 100% testing on my classes and trigger, but 0% for the system provided classes XMLDom and StartHere.  I have seen some ambiguous posts about this, but have not found source code I can download to simply test these classes.  Is there a simple solution to this issue.  This problem must come up for everyone who goes to deploy their first application; it seems logical for SalesForce to provide the test code if the classes are automatically included, so everyone doesn't have to solve the same problem over and over again.  Thanks for your help.

  • July 12, 2010
  • Like
  • 0

From what I can tell from error messages, you are not allowed to use aggregate functions in subqueries.  Because of this limitation, I have rearranged my query so the aggregate functions are in the main query, but I am getting errors on the relationships.  Can someone help me sort this out?

 

I have renamed Opportunity as Dealer (plural: Dealers).

I have a Master-Detail relationship between Dealer (master) and custom sObject Car (plural is Cars), where there are many Car sObjects to one Dealer, and many Dealers to one Account.

I am trying to use the aggregate functions to sum up certain fields in the Car sObject for each Dealer.  For example, sum all the Car_Number_Field_1__c fields for all the Cars associated with one Dealer; then elsewhere I will roll up all these Dealer sums for each Account.

 

There are ways to do this without aggregate functions, but I am interested in getting this to work using aggregate functions.  The following select statement in the for statement will not compile.  I get errors in the subquery where the complier does not understand the relationship with Dealers__r and it doesn't understand Dealer__c.  Can someone help me figure out how to get this to compile?  Thanks.

 

I have created a Set of relevant Accounts in accountIDs

 

Map<Id, List<Decimal>> carTotals = new Map<Id, List<Decimal>>();

for (AggregateResult aggregateData : [select Dealer__c,

            (select Id, AccountID from Dealers__r where Id = Dealer__c),
            SUM(Car_Number_Field_1__c) Car1Total, SUM(Car_Number_Field_2__c) Car2Total,
            SUM(Car_Number_Field_3__c) Car3Total from Car__c
            where AccountID in :accountIds Group by Dealer__c])  {

     List<Decimal> carSums = new List<Decimal>{(Decimal)aggregateData.Get('Car1Total'),
          (Decimal)aggregateData.Get('Car2Total'), (Decimal)aggregateData.Get('Car3Total')};
     carTotals.put(AccountID, carSums);

}

  • July 01, 2010
  • Like
  • 0

The essential information is:

 

Obect 1 has a master-detail relationship with Account , but is one-to-one with Account.

Object 1 also has a master-detail relationship with Opportunity that is one-to-many, (many Object1's to one Opportunity).

Each Opportunity has fields that need to be acted on in a for loop and then Object 1 is updated after that loop.

 

The original trigger essentially does the following:

 

create Set accountIDs from trigger.new

List<Object1> objList = [select Account__c ... from sObject1 where Account__c in :accountIDs]

for (Object1 acc : accountIDs) {

   // some code

    List<Opportunity> oppList = [select AccountID ... from sObject1 where AccountID = :accountIDs]

  // some more code

 change fields in objList

}  // end for

upsert objList

 

I need to take the SOQL statement out of the for loop, so the revised trigger does this:

 

create Set accountIDs from trigger.new

List<Object1> objList = [select Account__c ... from sObject1 where Account__c in :accountIDs]

// next line uses "in :accountIDs" instead of "= :accountIDs"

List<Opportunity> oppList = [select AccountID ... from sObject1 where AccountID in :accountIDs]

for (Object1 acc : accountIDs) {

   // some code

   for (Opportunity opp : oppList) {

       if (opp.AccountID == acc.Id) {

       // some code

       } // end if

   } // end inner for

  // some more code

 change fields in objList

}  // end outer for

upsert objList

 

The problem with the structure of the second trigger is that I am iterating over all the Opportunities every time, to find the relevant Opportunities with AccountID = acc.Id, which seems terribly inefficient.  I considered removing the "used" Opportunites after they have been processed, but then I'm still iterating over many irrelevant Opportunities on each loop.

 

There must be a better solution.  Can anyone help?  Thanks.

  • June 29, 2010
  • Like
  • 0

I am showing two triggers below; Trigger 2 at the bottom of the page works, except it performs the operation on all records, so I am trying in Trigger 1 to only access those records that have changed.  I have a lookup relationship between two custom objects Master_Lookup (parent) and Child_Detail (child), where a Master_Lookup could have many Child_Details, but a Child_Detail has only one Master_Lookup.  The first for loop using Trigger.new fills the set with a null value for the ID of the Master_Lookup instead of a valid ID.  I know there is a Master_Lookup associated with this Child_Detail so I can't understand why it has a null ID.  Can someone help me out.  Thanks.

 

Trigger 1:

trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Form a set of affected MasterLookupRecords to avoid duplicates
    Set<id> masterLookupIds = new Set<id>();
    for (Child_Detail__c myChildDetailRecord : Trigger.new){
        masterLookupIds.add(myChildDetailRecord.Master_Lookup__r.Id);
    }
    for (id value : masterLookupIds) {
        System.debug('Set = ' + value);
    }
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c where Id in :masterLookupIds';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup) {
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}

 

Trigger 2:
/*trigger updateOnQtyCountTotal on Child_Detail__c (after insert, after update) {
    // Use parent-child relationship to find all quantities for each Master_Lookup
    String queryString = 'SELECT Id, Total_Quantity__c, Total_Price__c, ' +
                         'Total_Count__c, (SELECT Quantity__c, Total_Price__c ' +
                         'from Child_Detail__r) from Master_Lookup__c';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup){
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningQuantityTotal = 0;
        Decimal runningPriceTotal = 0;
        Decimal runningItemsTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningQuantityTotal += (Decimal)childRecord.get('Quantity__c');
                runningPriceTotal += (Decimal)childRecord.get('Total_Price__c');
                runningItemsTotal++;
            }
        }
        masterLookupRecord.Total_Quantity__c = runningQuantityTotal;
        masterLookupRecord.Total_Price__c = runningPriceTotal;
        masterLookupRecord.Total_Count__c = runningItemsTotal;
    }
    upsert myMasterLookup;
}*/

  • June 26, 2010
  • Like
  • 0

I'm trying to implement my first trigger.  I have a lookup relationship between MasterLookup and ChildDetail objects.  Here is the trigger:

 

trigger updateOnQtyCountTotal on Master_Lookup__c (before insert, before update) {
    // Use parent-child relationship to find all quantities for each Master_Lookup
    String queryString = 'SELECT Id, Total_Quantity__c, (SELECT Quantity__c from Child_Detail__r) from Master_Lookup__c';
    Master_Lookup__c[] myMasterLookup = Database.query(queryString);
    for (Master_Lookup__c masterLookupRecord : myMasterLookup){
        sObject[] childRecordsFromMaster = masterLookupRecord.getSObjects('Child_Detail__r');
        Decimal runningTotal = 0;
        // Prevent a null relationship from being accessed
        if (childRecordsFromMaster != null) {
            for (sObject childRecord : childRecordsFromMaster){
                runningTotal += (Decimal)childRecord.get('Quantity__c');
            }
        }
        masterLookupRecord.Total_Quantity__c = runningTotal;
    }
}

 

The last line is not updating the Total_Quantity__c field when I view the MasterLookup object after making a change to the object.  It is clearly running the code in the trigger when I view the log, but the field is always blank.  This must be an obvious error, but I can't find it and searching the documentation has not helped.  Thanks.

  • June 25, 2010
  • Like
  • 0