+ Start a Discussion
Edward Scott 17Edward Scott 17 

Query an opportunity

Hi,

I am trying to query the opportunity object to find all closed lost opportunities. After finding those ops I would like to write test op in the opportunity description field. I am getting an error and can't figure out what is wrong. I posted the code below. Any help would be greatly appreciated.
//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();
  
for (Opportunity obj:oppList)
{
    if obj.StageName = 'Closed Lost'{
        obj.description = 'test opp';
		updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);
Thanks,
Ed
Best Answer chosen by Edward Scott 17
Raj VakatiRaj Vakati
Try this code
//collect all opportunities
List<Opportunity> oppList = [Select Id, Name, StageName,description  from Opportunity];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();
  
for (Opportunity obj:oppList)
{
    if(obj.StageName =='Closed Lost'){
        obj.description = 'test opp';
		updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

 

All Answers

Raj VakatiRaj Vakati
try this code
 
//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();
  
for (Opportunity obj:oppList)
{
    if(obj.StageName =='Closed Lost'){
        obj.description = 'test opp';
		updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

 
Edward Scott 17Edward Scott 17
Thanks Raj I updated the code. One more thing i forgot to add in the first part of the code to create the oppList.
//collect all opportunities
List<Opportunity> oppList = [Select Id, Name, StageName];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();
  
for (Opportunity obj:oppList)
{
    if(obj.StageName =='Closed Lost'){
        obj.description = 'test opp';
		updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

Now for some reason I get an error on the '<'  by the first opportunity.
Raj VakatiRaj Vakati
Try this code
//collect all opportunities
List<Opportunity> oppList = [Select Id, Name, StageName,description  from Opportunity];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();
  
for (Opportunity obj:oppList)
{
    if(obj.StageName =='Closed Lost'){
        obj.description = 'test opp';
		updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

 
This was selected as the best answer
Edward Scott 17Edward Scott 17
Thanks Raj. One more question too if I want to narrow the first list down could I just add a "Where Closedate = 11/21/2018"
Raj VakatiRaj Vakati
Try this
 
//collect all opportunities
Date closeDate = Date.newInstance(2018, 11, 21);
List<Opportunity> oppList = [Select Id, Name, StageName from Opportunity Where Closedate =:closeDate];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();

for (Opportunity obj:oppList)
{
    if(obj.StageName =='Closed Lost'){
        obj.description = 'test opp';
        updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);