3

we've found some of our records currently have a blank field which we need to populate using the values from a different custom field, because this will be a one time thing im writing a script in the anonymous debug window which will do this, so far i have written :

 List<producto__c> prodOrdersIDs  = [SELECT id, name, prod2__c, project__c,  Quote__c from producto__c where prod2__c = null and Quote__c != null];
for(producto__c po: prodOrdersIDs ){

   List<producto__c> QLIproduct2Id = [SELECT Quote__r.product2id from producto__c];
        if(po.prod2__c = null){
            po.prod2__c = po.Quote.product2id;
    }
    update po.prod2__c;
}

however this bring through the error : condition expression must be of type Boolean.

any idea where this is occurring would be greatly appreciated cheers

UPDATE:

this will now run but now comes through says theres too many future calls: 11

List<productorder__c> prodOrdersIDs  = [SELECT id, name, product2__c, project__c, Quote_line_item__r.product2id,  Quote_Line_Item__c from productorder__c where product2__c = null and Quote_Line_Item__c != null];
List<productorder__c> QLIproduct2Id = [SELECT Quote_line_item__r.product2id from productorder__c];

for(productorder__c po: prodOrdersIDs ){
        if(po.product2__c == null){
            po.product2__c = po.Quote_line_item__r.product2id;
    }
    update po;
}
1
  • Doing a query in a for loop like you do is not a good way !
    – SF_user
    Sep 29, 2014 at 9:01

2 Answers 2

5

You need to use the equality operator (==) in your conditional statement, rather than the assignment operator (=).

Your code also has other issues which will need to be addressed:

  • SELECT from Producto__c into a List<ProductOrder__c>.
  • SOQL query inside a for loop.
  • DML operation (update) within a for loop.
  • DML operation on a field (po.prod2__c) rather than an object (in this case po).

Since this is a one off operation, you may be able to get away with the second two if you have less than 99 ProductOrder__c objects (since you've already used 1 of the 100 SOQL limit in your first query), but I would recommend trying to bulkify anyway by adding the objects that need to be updated to a list and then calling update on that list..

8
  • Hi, the first bullet point was a typo on here, however with the DML update within the loop i get 'DML requires SObject or SObject list type: Id' but when i try to change to update just the 'po' this arrives SObject row was retrieved via SOQL without querying the requested field: ProductOrder__c.Quote_Line_Item__r Sep 29, 2014 at 9:34
  • 1
    @ClevelandDan Can you update the code in your question (preferably my adding it underneath your existing content)? As it stands the code in the question should not give you that error (or in fact compile, as it has other syntax errors). Sep 29, 2014 at 9:39
  • Edited it above Sep 29, 2014 at 9:42
  • 1
    @ClevelandDan Based on the 'too many future calls' error you have a callout in a trigger somewhere. Moving the DML operation out of the loop should fix this (unless your trigger is poorly written). Remove update po and move it below the loop and change it to update prodOrderIds. You can also get rid of your second SOQL query as you never use the results. Sep 29, 2014 at 9:49
  • FYI, update prodOrderIds will make your code call save on records that don't need it, leading to less efficient processing and erroneous LastUpdate information. Best practice would be to set the modified po into a new list and save that list only.
    – pchittum
    Sep 29, 2014 at 10:07
1

Apart from the problem with the equality operator, here is a suggestion for your code, as you are doing some potentially dangerous things.

Your code:

List<producto__c> prodOrdersIDs  = [SELECT id, name, prod2__c, project__c,  Quote__c 
                                   from producto__c 
                                   where prod2__c = null and Quote__c != null];

for(producto__c po: prodOrdersIDs ){

  List<producto__c> QLIproduct2Id = [SELECT Quote__r.product2id 
                                     from producto__c]; <--same sObject queried twice
                                                <-- no where clause is a bad idea
    if(po.prod2__c = null){
        po.prod2__c = po.Quote.product2id;
    }                                   ^---probably this is quote__r ???
    update po.prod2__c; <--this is almost guaranteed to cause problems when done inside a loop
                ^--also, youdon't call update on a field, rather on an sobject or list of sobjects
}

Try this:

List<producto__c> prodOrdersForUpdate = new List<producto__c>();
List<producto__c> prodOrdersIDs  = [SELECT id, name, quote__r.product2Id, prod2__c, project__c
                                   from producto__c 
                                   where prod2__c = null and Quote__c != null];

for(producto__c po: prodOrdersIDs ){

    if(po.prod2__c = null){
        po.prod2__c = po.quote__r.product2id;
    }
    prodOrdersForUpdate.add(po);
}
update prodOrdersForUpdate; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.