I want to add or remove a AgentLogin to a person but do get an error when running this code:
var login = func.agentlogin_query(“1234”).SingleOrDefault();
var person = func.person_query(login.DBID).SingleOrDefault();
CfgAgentLoginInfo info = new CfgAgentLoginInfo(service,person);
info.SetAgentLoginDBID(login.DBID);
Console.WriteLine(info.ToString());
person.AgentInfo.AgentLogins.Add(info);
person.Save();
I get this error:
{“Object reference not set to an instance of an object.”}
The extension exists and the console write is giving me this result:
Below is the modified slightly to retrieve the CfgAgentLogin and use that when creating the CfgAgentLoginInfo object, but even using your way of using .SetAgentLoginDBID worked.
CfgPersonQuery qPerson = new CfgPersonQuery();
qPerson.UserName = "phoyle";
CfgPerson myPerson = confService.RetrieveObject<CfgPerson>(qPerson);
if (myPerson == null)
{
log.Info("Person Not Valid");
return;
}
CfgAgentLoginQuery qLogin = new CfgAgentLoginQuery();
qLogin.Dbid = 422;
CfgAgentLogin myLogin = confService.RetrieveObject<CfgAgentLogin>(qLogin);
if (myLogin == null)
{
log.Info("AgentLogin Not Valid");
return;
}
CfgAgentLoginInfo agentInfo = new CfgAgentLoginInfo(confService, myPerson);
//agentInfo.SetAgentLoginDBID(422);
agentInfo.AgentLogin = myLogin;
myPerson.AgentInfo.AgentLogins.Add(agentInfo);
myPerson.Save();
I saw my mistake when adding the number, I have searched for the agent who got the agentlogin assigned.
so adding is now woking aswell.
Just removing is still not working:
var login = func.agentlogin_query(“1324”).SingleOrDefault();
var person = func.person_query(login.DBID).SingleOrDefault();
CfgAgentLoginInfo info = new CfgAgentLoginInfo(func.app_service, person);
info.SetAgentLoginDBID(login.DBID);
Console.WriteLine(info.ToString());
person.AgentInfo.AgentLogins.Remove(info);
person.Save();