Is there a better way to get the personId of the current contact than using getCgiVariable("personId")? Lately this has become unstable and often returns the personId of a previously viewed user.
The following script is run when a user clicks a button on either a contact card or a ticket/request. So what I need in the end is the x_personid, which is a custom field on person.
#setLanguageLevel 4;
Integer personId = getCgiVariable("personId").toInteger();
if (personId.isNull() == true || personId < 1)
{
setVariable("url", getProgramBlogic() + "&action=doScript&includeId=close");
}
String externalPersonId;
SearchEngine se;
se.addField("person.x_personid");
se.addCriteria("person.person_id", "Equals", personId.toString());
if(se.select() > 0)
externalPersonId = se.getField(0);
if(externalPersonId.toInteger()>0)
setVariable("url", "https://bn.app.local/Customer/Link/" + externalPersonId);
else
setVariable("url", getProgramBlogic() + "&action=doScript&includeId=close");
Alles Antwoorden (6)
Hi Snorre,
Get the active user, then traverse to get the users person id.
// get current user associate ID
User a = getActiveUser();
Integer associateId = a.getValue("associateId").toInteger();
Integer personId;
// get person ID from associate ID
NSAssociateAgent aa;
NSAssociate associate = aa.GetAssociate(associateId);
personId = associate.GetPersonId();
printLine("Person ID from associate: " + personId.toString());
// OR
SearchEngine se;
se.addField("associate.person_id");
se.addCriteria("associate.associate_id", "OperatorEquals", associateId.toString());
se.execute();
se.first();
personId = se.getField(0).toInteger();
printLine("Person ID from SE: " + personId.toString());
Best regards.
Hi Snorre,
I think perhaps you didn't want to the personId of the current associate, but rather the current person in CRM?
You say that getCgiVariable("personId") is unstable. That's not our experience. I think we need to look into your button setup. From your text it seems like you mean that when you click the button the script is called with the wrong personId in the URL.
Can you share how you have configured the button?
Hi Frode
getCgiVariable("personId") is definately unstable on the latest cloud version of crm. After about 30-45 min it starts returning the personid of a previously opened customer. There is some kind of strange caching going on here.
I got help from Simen(SuperOffice) creating a workaround where we use getCgiVariable("ticketId") and from there navigating to the connected customer. We are testing this now and so far it looks better.
String ticketId = getCgiVariable("ticketId");
String personId = getCgiVariable("personId");
String externalPersonId;
String navigateUrl = getProgramBlogic() + "&action=doScript&includeId=close";
String ticketPerson;
if(ticketId.toInteger() > 0) {
SearchEngine seTicket;
seTicket.addField("ticket.cust_id");
seTicket.addCriteria("ticket.id", "Equals", ticketId);
if(seTicket.select() > 0) {
ticketPerson = seTicket.getField("ticket.cust_id");
}
}
Hi Snorre,
You said "getCgiVariable("personId") is definately unstable on the latest cloud version of crm. After about 30-45 min it starts returning the personid of a previously opened customer. There is some kind of strange caching going on here." There could possibly an issue with getCgiVariable, but I'm not convinced. We need to know more details to be able to reproduce it.
The original SearchEngine query seems correct, to get the x_personid value, it's just that you are experiencing an incorrect personId coming out of the getCgiVariable method I'm assuming if we use your script we should be able to do just that after said period of time. Is that correct? You think it's a time issue, and nothing else, e.g. after a certain number of button clicks?
Integer personId = getCgiVariable("personId").toInteger();
if (personId.isNull() == true || personId < 1)
{
setVariable("url", getProgramBlogic() + "&action=doScript&includeId=close");
}
String externalPersonId;
SearchEngine se;
se.addField("person.x_personid");
se.addCriteria("person.person_id", "Equals", personId.toString());
if(se.select() > 0)
externalPersonId = se.getField(0);
if(externalPersonId.toInteger()>0)
setVariable("url", "https://localhost:8080/person/" + externalPersonId);
else
setVariable("url", getProgramBlogic() + "&action=doScript&includeId=close"); //NOT 100% SURE WHAT CLOSE DOES, but seems intuitive enough.
If that is the case my gut feeling is that the problem isn't coming from getCgiVariable("personId"). That method is so extremely widely used, that basically every solution we've ever made would blow up.
It's more likely an issue with the new Request screen, the Screen Designer in CRM and the variable replacement in CRM when building the URL to call. All those three mechanisms are either new or have been having issues lately in SuperOffice 11.3. Could be related to some of the soprotocol issues we've seen lately.
When you click your button, it should open a new tab where you can see the entire URL used to run the script. When you experience the error, is the personId in the URL correct or wrong? My guess is that you'll also see an old personId in the URL, which indicates the problem is in CRM, not with the getCgiVariable method.
Here is the script I tested with:
%EJSCRIPT_START%
<%
#setLanguageLevel 4;
printLine("");
printLine("getCgiVariable(\"personId\"): " + getCgiVariable("personId") + "
");
printLine("getCgiVariables(): " + getCgiVariables().toJson() + "
");
Integer personId = getCgiVariable("personId").toInteger();
NSPersonAgent agent;
NSPersonEntity person = agent.GetPersonEntity(personId);
printLine("Person found: " + person.GetFullName() + "
");
%>
%EJSCRIPT_END%
You could also try to point your button to f.instance https://google.com?q=
Yes I agree. It is the url used to trigger the script that is the source of the problem. In this case the correct personId should be 1560, but 4406 is in the url. The ticketId is correct though