+ Start a Discussion
Daniel143Daniel143 

Save Error : expression cannot be assigned

I am trying to write a test class and getting a save error in line 0.

 

public with sharing class HelloWorldTestClass {
static testMethod void HelloWorldTest(){
List<Position__c> positions = new List<Position__c>();
for (Integer i=0; i < 20; i++){
Position__c p = new Position__c();
position__c.Name = 'testName' + i;
position__c.Job_Description__c = 'testDescription' + i;
positions.add(p);
}

try{
System.debug('Attempting to insert positions');
insert positions;
System.debug('Insert complete');
// refresh the Position list to include the Hello__c field after the insert
Map<ID,Position__c> positionMap = new Map<ID,Position__c>(positions);
positions = [select id,name,Hello__c from Position__c where id IN :positionMap.keySet()];
for (Position__c position:positions){
System.assertNotEquals(position.id,null);
System.assertEquals(position.Hello__c, 'World');
System.debug('position.id=' + position.id);
System.debug('position.name=' + position.name);
System.debug('position.Hello__c=' + position.Hello__c);
}
} catch (System.DmlException e){
System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
}

}

}

Best Answer chosen by Admin (Salesforce Developers) 
kcpluspluskcplusplus

Then I would imagine that the standard name field of that object is an autonumber, and you can't write to an autonumber field. 

 

--KC

All Answers

kcpluspluskcplusplus
public with sharing class HelloWorldTestClass {
static testMethod void HelloWorldTest(){
List<Position__c> positions = new List<Position__c>();
for (Integer i=0; i < 20; i++){
Position__c p = new Position__c();
p.Name = 'testName' + i;
p.Job_Description__c = 'testDescription' + i;
positions.add(p);
}

try{
System.debug('Attempting to insert positions');
insert positions;
System.debug('Insert complete');
// refresh the Position list to include the Hello__c field after the insert
Map<ID,Position__c> positionMap = new Map<ID,Position__c>(positions);
positions = [select id,name,Hello__c from Position__c where id IN :positionMap.keySet()];
for (Position__c position:positions){
System.assertNotEquals(position.id,null);
System.assertEquals(position.Hello__c, 'World');
System.debug('position.id=' + position.id);
System.debug('position.name=' + position.name);
System.debug('position.Hello__c=' + position.Hello__c);
}
} catch (System.DmlException e){
System.debug('We caught a DML exception: ' + e.getDmlMessage(0));
}

}

}

 I'd start with that, once you declare a new instance of an object, you refer to it throught the variable you assigned it, not the name of the object. 

Daniel143Daniel143

I am getting a different error if i use the assigned variable p.

 

"Save error: Field is not writeable: Position__c.Name "

kcpluspluskcplusplus

Then I would imagine that the standard name field of that object is an autonumber, and you can't write to an autonumber field. 

 

--KC

This was selected as the best answer
Daniel143Daniel143

Thank you. It worked now if i remove the instantiation of the p.Name