function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ranadheer chRanadheer ch 

I had written a test class for the following class....but am getting this error

I wrote a test class for the following class ...but while saving am getting this error Whats wrong in my code...thanks



Error Error: Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects at line 8 column 32


my test class


@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
    AccountContactRole  accs = new AccountContactRole(Account.Name = 'TestAccountName',Role='Newrole',IsPrimary='True' );

    //Insert the object virtually
    insert accs;

    //Create a new instance of standard controller
    ApexPages.StandardController sc = new ApexPages.standardController(accs);

    AccountContactRoles controller = new AccountContactRoles(sc);
    }
}



My class


public with sharing class AccountContactRoles {
    private final Contact acr;
   
    public AccountContactRoles(ApexPages.StandardController controller){
        this.acr = (Contact)controller.getRecord();
    }
   
    public List<AccountContactRole> acrs {
        get {
            if (acrs == null) {
                acrs = [Select Id, Account.Name,
                     Role, IsPrimary
                    From AccountContactRole
                    Where ContactId=:acr.Id
                    ];               
            }
            return acrs;
        }
        private set;
    }
}




I need help ...thanks in advance
Best Answer chosen by Ranadheer ch
Anoop yadavAnoop yadav
Hi,

One more thing.
IsPrimary is a boolean, you should use only true.
Also pass the Contact in ApexPages.StandardController.

Check the below one.
@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Contact con = new Contact(LastName = 'Test Last Name', AccountId = acc.Id);
    insert con;
    
    AccountContactRole  accs = new AccountContactRole(AccountId = acc.Id, ContactId = con.Id, Role='Newrole',IsPrimary=True );

    //Insert the object virtually
    insert accs;

    //Create a new instance of standard controller
    ApexPages.StandardController sc = new ApexPages.standardController(con);

    AccountContactRoles controller = new AccountContactRoles(sc);    
    }
}


All Answers

Anoop yadavAnoop yadav
Hi,

Try Like below.
@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
	Account acc = new Account(Name = 'Test Account');
	insert acc;
	Contact con = new Contact(LastName = 'Test Last Name', AccountId = acc.Id);
	insert con;
	
    AccountContactRole  accs = new AccountContactRole(AccountId = acc.Id, ContactId = con.Id, Role='Newrole',IsPrimary='True' );

    //Insert the object virtually
    insert accs;

    //Create a new instance of standard controller
    ApexPages.StandardController sc = new ApexPages.standardController(accs);

    AccountContactRoles controller = new AccountContactRoles(sc);
    }
}

ShashankShashank (Salesforce Developers) 
Please see this: http://unid-machine.blogspot.sg/2014/08/invalid-constructor-syntax-namevalue.html

You cannot instantiate an inner class like sObjects.
Anoop yadavAnoop yadav
Hi,

One more thing.
IsPrimary is a boolean, you should use only true.
Also pass the Contact in ApexPages.StandardController.

Check the below one.
@isTest
private class TestAccountcontactroleClass{
    @isTest
    private static void testClass()
    {
    //Standard controller of Accountcontactrole
    //Create a new instance of Accountcontactrole
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Contact con = new Contact(LastName = 'Test Last Name', AccountId = acc.Id);
    insert con;
    
    AccountContactRole  accs = new AccountContactRole(AccountId = acc.Id, ContactId = con.Id, Role='Newrole',IsPrimary=True );

    //Insert the object virtually
    insert accs;

    //Create a new instance of standard controller
    ApexPages.StandardController sc = new ApexPages.standardController(con);

    AccountContactRoles controller = new AccountContactRoles(sc);    
    }
}


This was selected as the best answer
Ranadheer chRanadheer ch
Thanks anoop yadhav...i tried the above code but am still getting the same error like...
Error: Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects at line 13 column 32.

Any way thanks for ur help
unidhaunidha
Hope this link will help http://www.shelovestocode.com/2014/08/invalid-constructor-syntax-namevalue.html . 

When instantiate inner class for defined constructor, directly set the parameter such as

new WrapperObject(param1,param2,...) instead of new WrapperObject(property1=param1,property2=param2,...)