0
global class gz implements database.batchable<sobject>{
global database.querylocator start(database.batchablecontext bc)
{
string sn='closed won';
string query='select id,name,stagename from opportunity where stagename=:sn';
return database.getquerylocator(query);
}
global void execute(Database.BatchableContext BC, list<opportunity> opplist){
for(opportunity opp:opplist)
{
opp.description='Your Stage is in Progress';
string val1=opp.name;
string val2='-';
string val3='Update';
string result='';
result=string.valueof(val1)+string.valueof(val2)+string.valueof(val3);
opp.name=string.valueof(result);
}
update opplist;
}
global void finish(Database.BatchableContext BC){}
}

Test Class:
@istest
public class gz_Test{
static testmethod void gz_Test()
{
opportunity opp=new opportunity(name='xxxx',stagename='xxxxx');
opportunity opp1=new opportunity(name='xxxx',stagename=opp.stagename);
update opp1;
}
}
frz.sfdc
  • 13
  • 3
  • simply updating an opportunity in the test class won't cause the batch to execute. See the Apex doc on testing batch classes – cropredy Jun 08 '15 at 04:40

1 Answers1

0

At a minimum, in order to create an opportunity, you first must create an account and associate the opportunity with the account. Check your organization's required fields for a new account and a new opportunity. If you don't satisfy those requirements, your accounts and opportunities won't insert.

Add debug statements to your code and look at your debug logs. See How do I start to debug my own Apex code?.

crmprogdev
  • 40,955
  • 9
  • 58
  • 115
  • How to write this line in test class – frz.sfdc Jun 06 '15 at 14:58
  • string sn='closed won'; string query='select id,name,stagename from opportunity where stagename=:sn'; – frz.sfdc Jun 06 '15 at 14:58
  • Hello crmprogdev sir how to above line in test class – frz.sfdc Jun 06 '15 at 15:13
  • Are you asking me how to write a dynamic query? If so, since it's a test class, do you expect it to change and if not, why do you need it to be dynamic? See the SOCL and SOSL Docs on how to write queries. – crmprogdev Jun 06 '15 at 16:01
  • actually, and I didn't learn this until recently, you can insert Oppos without Accounts, accountId is not a required field - see https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunity.htm#topic-title. The standard UI forces an AccountId but apparently not DML. – cropredy Jun 08 '15 at 04:43
  • Note that he's doing an Update to Opportunity records. Opportunity is on the detail side of a M-D relationship with Account. One can't update unparented records. In most real orgs, validation rules would prevent an Opp from being created without an associated Account. – crmprogdev Jun 08 '15 at 13:03