Answered by:
activity to opportunity: Is there a way to block Contact related opportunities?

Question
-
Hello,
In a current Dynamics CRM 2011 implementation, I added a jScript in the "Potential Customer" field of the opportunity entity as to only allow users to pick up Accounts, and not Contacts. I also removed the Opportunity link from the Contact navigation bar. The idea is to avoid users creating opportunities against Contact records (we only do B2B sales in this implementation).
However I have noticed that when converting an activity (e.g.: phone call or email) to an opportunity, is the activity was related to a contact, the Opportunity "Potential Client" will be set to the Contact which the activity was related to.
What I would like to do is to avoid this situation, either by:
- When the form load in such circumstance, clear the value of the Potential Customer lookup field; or even better
- When the form load in such circumstance, set the value of the Potential Customer lookup field as the parent Account for the Contact.
I found this article here which might seem to be a good starting point: http://social.technet.microsoft.com/wiki/contents/articles/4122.dynamics-crm-2011-dynamics-crm-2011-perform-jscript-validations-on-entity-form-before-execution-of-special-events.aspx
However I am not sure of how a script in the Activity form could perform such change in the Opportunity form.
I wonder if this is at all possible through jscript in this special circumstance. Any thoughts or suggestions on this issue?
Thanks for the help!
P.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Friday, April 27, 2012 8:20 AM
Answers
-
Just answered here http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/f55aff09-39ef-4370-bbd6-f7a5d0b14a04
make sure to close thread if you got your answer.
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Edited by HIMBAPModerator Monday, April 30, 2012 3:07 PM
- Marked as answer by pmdci Tuesday, May 15, 2012 10:27 AM
Monday, April 30, 2012 2:49 PMModerator
All replies
-
Hi,
I think you need to apply your validation on opportunity instead of activity, for example you in opportunity create you can check parent form it is related to activity, then you can check regardingobjectid if it is contact type fetch it's parent account and set it there.
you can try to access parent entity on opportunity on load using : window.parent.opener.Xrm.Page.data.entity
or may be simply you can check in opportunity create directly if parent customer is contact, fetch it's account and set it.
HTH
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Friday, April 27, 2012 8:53 AMModerator -
Mahender,
Thank you for the heads up. I reckon this would be oData. Am I correct? :)
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Friday, April 27, 2012 9:03 AM -
Hi,
yes if you want to get parent account for selected contact you can use oData.
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Friday, April 27, 2012 9:15 AMModerator -
Hi Mahender,
Thanks for the head's up.
How could I, however, make sure that this only happens if the value in the Potential Customer field is a Contact?
I don't want this script to load if the Potential Customer field is already an Account.
any ideas?
Cheers,
P.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Friday, April 27, 2012 9:52 AM -
Hi,
you need to check for typename , which will tell you if Potential customer is a account or contact, so just run your code only when contact
so it should be something like
if(Xrm.Page.getAttribute("customerid").getValue()!=null)
{
if(Xrm.Page.getAttribute("customerid").getValue()[0].typename=="contact")
{
//your code
}
}
HTH
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Friday, April 27, 2012 10:01 AMModerator -
Hi Mahender,
So far do good. I am just having a little bit of difficulty with the oData. Perhaps you could give me a hand?
I need to get the GUID of the contact currently in the customerid field, then I must pull the GUID of the parent customer for it, then push this GUID into the customerid field.
Is that correct? Any ideas how would that query be?
Sorry for the "noob" question but I am totallynew to oData :)
Cheers,
P.
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Friday, April 27, 2012 4:04 PM -
you need to follow below steps
1. First read contact guid from customer field like below
if(Xrm.Page.getAttribute("customerid").getValue()!=null)
var ID=Xrm.Page.getAttribute("customerid").getValue()[0].id;
2. use need to do retrieve call to get parentid, just like you did in your last script
var filter = "/ContactSet?$filter=ParentCustomerId/Id eq guid'" + ID + "'"; //Edit : we need to use schema name
3. once you got parentcustomerid you can set lookup like belowvar lookup = new Object();
var lookupValue = new Array();lookup.id=<<GUID>>; guid of the parentcustomerid
lookup.entityType ="account";
lookup.name =<<name of account>>;
lookupValue[0] = lookup;
Xrm.Page.getAttribute("customerid").setValue(lookupValue);I hope you should be able to do it now.
let us know if you need any other infor on this.
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Friday, April 27, 2012 4:32 PM
- Edited by HIMBAPModerator Monday, April 30, 2012 3:07 PM
Friday, April 27, 2012 4:32 PMModerator -
Mahender,
Thanks for the help. will try this one on Monday and let you know :)
MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Friday, April 27, 2012 6:10 PM -
Hi mahender,
I am having a look on the example you sent me, but I am not sure on how to get the GUID from the parent account. Here is the code I have so far. Perhaps you could give me a hand when you get the chance?
function setOpAccountPerContact() { var CRM_FORM_TYPE = Xrm.Page.ui.getFormType(); if (CRM_FORM_TYPE != 2) { return; } if(Xrm.Page.getAttribute("customerid").getValue()!=null) { if(Xrm.Page.getAttribute("customerid").getValue()[0].typename=="contact") { alert ("this op is bound to a contact"); var idCustomer = Xrm.Page.getAttribute("customerid").getValue()[0].id; var retrieveRecordsReq = new XMLHttpRequest(); var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc"; var filter = "/ContactSet?$filter=parentcustomerid/Id eq guid'" + idCustomer + "'"; // Not sure how to proceed from here. How csn I get the GUID for the parent account? retrieveRecordsReq.open('GET', ODataPath+filter, false); retrieveRecordsReq.setRequestHeader("Accept", "application/json"); retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveRecordsReq.send(null); var records = this.parent.JSON.parse(retrieveRecordsReq.responseText).d; } } }
Thanks for the help!
Cheers,
P.MCC, MCT, MCP, MCTS
If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".Monday, April 30, 2012 12:59 PM -
Just answered here http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/f55aff09-39ef-4370-bbd6-f7a5d0b14a04
make sure to close thread if you got your answer.
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Edited by HIMBAPModerator Monday, April 30, 2012 3:07 PM
- Marked as answer by pmdci Tuesday, May 15, 2012 10:27 AM
Monday, April 30, 2012 2:49 PMModerator