0

I am trying to replace opportunity field value using apex class,

public class update_system {
    public static String saveData(){
        for (Opportunity opp : [SELECT Id,system__c FROM Opportunity where system__c = 'Test (A)' and StageName Not in ('Closed','Closed Won','Closed Lost') limit 10
]){
               if (opp.system__c == 'Test (A)' ){ 
                   opp.update_system__c = 'Test';
                   update opp;

               }    
        }
     }
}

but i am getting below error

Error Error: Compile Error: Invalid field update_system__c for SObject Opportunity at line 6 column 20

1

It looks like the field you are trying to set is called System__c, not Update_System__c. The API Name doesn't change due to context. It is constant. Also not you have DML in a loop, which violates Trigger Bulkification 101. Also note that your if condition is redundant, since you have already filtered System__c = 'Test (A)' in your query.

Here is what your code should look like:

public static String saveData()
{
    List<Opportunity> records = [
        SELECT Id FROM Opportunity WHERE System__c = 'Test (A)'
        AND StageName NOT IN ('Closed', 'Closed Won', 'Closed Lost')
    ];
    for (Opportunity record : records)
    {
        record.System__c = 'Test';
    }
    update records;
}
| improve this answer | |
1

Make sure you have the update_system__c field in your SOQL

Are you firing this from a trigger a button? Either way, you should realize that you are just targeting one record at the time and giving a LIMIT 10?

You probably want to do something like this:

public class update_system {

    public static String saveData()
    {
     for (List<Opportunity> oppList : [SELECT Id,system__c FROM Opportunity where system__c = 'Test (A)' and StageName Not in ('Closed','Closed Won','Closed Lost') limit 10])
     {
         for(Opportunity opp: oppList)
         {
           if (opp.system__c == 'Test (A)') 
               opp.system__c = 'Test';
         } 
          update oppList;
     }

}

Assuming the field you want to update is System__c and not Update_system__c.

You can just run this code in you developer console as anonymous block:

for (List<Opportunity> oppList : [SELECT Id,system__c FROM Opportunity where system__c = 'Test (A)' and StageName Not in ('Closed','Closed Won','Closed Lost') limit 10])
{
 for(Opportunity opp: oppList)
  {
   if (opp.system__c == 'Test (A)') 
       opp.system__c = 'Test';
  } 
  update oppList;
}

But remember that all executions on anonymous code from your developer console will actually run in your org and make the changes, so please, make sure you are not executing this on production.

You can check this structure on this code coming from SFDC's documentation:

public void massUpdate() {
    for (List<Contact> contacts:
      [SELECT FirstName, LastName FROM Contact]) {
        for(Contact c : contacts) {
            if (c.FirstName == 'Barbara' &&
              c.LastName == 'Gordon') {
                c.LastName = 'Wayne';
            }
        }
        update contacts;
    }
}

Here the URL on 'Working with Very Large SOQL Queries'.

| improve this answer | |
  • How would that fix this compile error, which indicates update_system__c is not an existing field? – Adrian Larson May 5 '17 at 13:53
  • Yeah, probably the field is System__c and not update_system__c – Carlos Naranjo May 5 '17 at 13:56
  • the field is system__C – user3585019 May 5 '17 at 14:07
  • after using Carlos Naranjo code im getting below error Error: Compile Error: Variable does not exist: opp at line 15 column 18 – user3585019 May 5 '17 at 14:07
  • Yes, sorry, change update opp to update oppList. That should do it. – Carlos Naranjo May 5 '17 at 14:17

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.