+ Start a Discussion
Marry SteinMarry Stein 

Unable to pass opportunity stagename as parameter

hi guys, 

i have created a data factory class. i am struggeling to pass the stagename of the opportunity as parameter. This is my code:
 
public static Opportunity createOpportunity(String stageName, Id accountId){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.StageName = stageName;
        opp.Type = 'Neukundengeschäft';
        opp.AccountId = accountId;
        opp.CloseDate = System.today();
        opp.z_Vertragsbeginn__c = Date.newInstance(2019, 1, 1);
        opp.z_Enddatum__c = Date.newInstance(2019, 12, 31);

        return opp;
    }
// here i pass the parameter stage1 
Opportunity opp = DataFactory.createOpportunity('stage1', acc.id);
// but this condition fails, it is null
System.assertEquals(opp.StageName, 'stage1')

I appreciate your help guys ! 
 

Greetings marry

Best Answer chosen by Marry Stein
{tushar-sharma}{tushar-sharma}
You can clone the existing object and can use that one instead of creating a new instance.
`sobj.clone(false,true)`

It will keep all values.

All Answers

{tushar-sharma}{tushar-sharma}
Hi Marry,

I just checked this in my org and it worked. The only issue I can see is you are missing semicolon in system.assert. Will you add tht and check.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
Marry SteinMarry Stein
Hi Tushar,

i have found the error in another methode: 
 
public static List<sObject> getObjectList(sObject sobj, String col, Integer numberOfRecords) {
        // Liste zu erstellender objekte
        System.debug(sobj);
        List<sObject> objectsToReturn = new List <sObject>();
        // hol den objektnamen
        String objectName = sobj
        .getSObjectType()
        .getDescribe()
        .getName();
        System.debug('objName: '+ objectName);
        // welches Feld soll verändert werden
        String colValue = (String) sobj.get(col);
        System.debug('ColumnValue: '+ colValue);

        for(Integer i=0; i<numberOfRecords;i++) {
            sObject o = Schema.getGlobalDescribe().get(objectName).newSObject();
            o.put('name', colValue+i);
            objectsToReturn.add(o);
        }
this line of code causes the error:
sObject o = Schema.getGlobalDescribe().get(objectName).newSObject();
I create a completely new sobject, and because of that i delete my predefined values. Do you know how to copy the sobject sobj in my parameter list ? 

Greetings Marry

 
{tushar-sharma}{tushar-sharma}
You can clone the existing object and can use that one instead of creating a new instance.
`sobj.clone(false,true)`

It will keep all values.
This was selected as the best answer