Question

CRM onSave Script to Populate another field based on the value of two other fields

Asked by: jgould763

I need to populate another field on a CRM Form based on the value selected in another field.
I know I need to use the onChange script of the primary field but I'm struggling to figure this out.

Example:
If the value in field MFG = 1  
AND
the value in field Price > 0
THEN make the value of field RSA = Option1

If the value in field MFG = 2
AND
the value in field Price >0
THEN make the value of field RSA = Option2

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-06-10 at 09:01:40ID24479874
Topics

CRM Software

,

Microsoft Dynamics

Participating Experts
2
Points
500
Comments
8

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. CRM
    I don't know a thing about CRM so my question may look stupid: Can I deliver a CRM solution without delivering a data warehouse? How does it work?
  2. Pop-Up Dialog Box via OnChange event in CRM 4.0
    I need to pop a dialog box using the OnChange event in CRM. This dialog box would simply be a reminder telling the user that they need to do something if the check this bit field. What script would I need to use in order to do this?
  3. CRM onChange Script to Populate another field
    I need to populate another field on a CRM Form based on the value selected in another field. I know I need to use the onChange script of the primary field but I'm struggling to figure this out. Example: If the value in field MFG = 1 Then make the value of field RSA = Option1...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: feridunPosted on 2009-06-10 at 09:46:46ID: 24593619

The solution depends on the logic neeed for the two fields to test. If Price always has to be greater than zero then I would enclose the switch statement inside an if statement something like this:


if (crmForm.all.PRICE.DataValue > 0) {

switch (crmForm.all.MFG.DataValue)
  {
  case "One":
    crmForm.all.RSA.DataValue="One";
    break
     
  case "Two":
    crmForm.all.RSA.DataValue="Two";
    break

  default:
      crmForm.all.RSA.DataValue="Default";
    break
  }
}

This script would do nothing if price was not > 0.

If on the other hand, the price test might vary you could do something like this:

switch (crmForm.all.MFG.DataValue)
  {
  case "One":
    // test for PRICE> 5 as an example
    if (crmForm.all.PRICE.DataValue > 5) {
    crmForm.all.RSA.DataValue="One";
    }
    break
     
  case "Two":
    // add in the appropriate test for price as per case one
    crmForm.all.RSA.DataValue="Two";
    break

  default:
      crmForm.all.RSA.DataValue="Default";
    break
  }

Hope this helps.

 

by: jgould763Posted on 2009-06-10 at 10:16:21ID: 24593848

feridun, I think your 1st suggestion is close to what I need.  Here is the exact code that I placed in the "onSave" event.

It appears to work when I Save the form, however the value is not populated in the table because when I open the record, the field is blank again.

Any ideas?

Here is the code:


if (crmForm.all.new_royalsupport.DataValue > 0) {
switch (crmForm.all.new_manufacturer.DataValue)
 {
 case "1":
   crmForm.all.new_supportaccount.DataValue="9";
   break
  case "2":
   crmForm.all.new_supportaccount.DataValue="5";
   break
  case "3":
   crmForm.all.new_supportaccount.DataValue="6";
   break
  case "4":
   crmForm.all.new_supportaccount.DataValue="7";
   break
  case "5":
   crmForm.all.new_supportaccount.DataValue="10";
   break
  case "6":
   crmForm.all.new_supportaccount.DataValue="8";
   break
  default:
     crmForm.all.new_supportaccount.DataValue="None";
   break
 }
}

 

by: feridunPosted on 2009-06-10 at 10:23:44ID: 24593913

Do you have an OnLoad script that might be blanking the field?

 

by: jgould763Posted on 2009-06-10 at 10:56:19ID: 24594226

feridun,  This code does work when creating a new record; however, when I "save" an existing record, the field is not populated.

Any ideas?  Is this a possible bug in CRM?

 

by: jgould763Posted on 2009-06-10 at 11:23:20ID: 24594496

Here is the onLoad script.  I don't see anything that would be blanking that field out.


// Initailly Hide Specific Sections
{
crmForm.all.new_royalsupportaccount.parentElement.parentElement.parentElement.style.display = 'none';
crmForm.all.transactioncurrencyid.parentElement.parentElement.parentElement.style.display = 'none';
}
{
crmForm.all.new_vendorsupportacct.parentElement.parentElement.parentElement.style.display = 'none';
}
// Display various sections depending on value of this field

if (crmForm.all.new_supporttype.DataValue =='1')
{
crmForm.all.new_royalsupportaccount.parentElement.parentElement.parentElement.style.display = 'block';
crmForm.all.new_vendorsupportacct.parentElement.parentElement.parentElement.style.display = 'none';
}
else if (crmForm.all.new_supporttype.DataValue =='2')
{
crmForm.all.new_royalsupportaccount.parentElement.parentElement.parentElement.style.display = 'none';
crmForm.all.new_vendorsupportacct.parentElement.parentElement.parentElement.style.display = 'block';
}
 
 
//check if the current user has the 'System Administrator' role  
//alert(UserHasRole("System Administrator"));  
function UserHasRole(roleName)  
{  
//get Current User Roles, oXml is an object  
var oXml = GetCurrentUserRoles();  
if(oXml != null)  
{  
 //select the node text  
 var roles = oXml.selectNodes("//BusinessEntity/q1:name");  
 if(roles != null)  
 {  
  for( i = 0; i < roles.length; i++)  
  {  
   if(roles[i].text == roleName)  
   {  
    //return true if user has this role  
    return true;  
   }  
  }  
 }  
}  
//otherwise return false  
return false;  
}  
 
function GetCurrentUserRoles()  
{  
var xml = "" +  
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +  
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +  
GenerateAuthenticationHeader() +  
" <soap:Body>" +  
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +  
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +  
" <q1:EntityName>role</q1:EntityName>" +  
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +  
" <q1:Attributes>" +  
" <q1:Attribute>name</q1:Attribute>" +  
" </q1:Attributes>" +  
" </q1:ColumnSet>" +  
" <q1:Distinct>false</q1:Distinct>" +  
" <q1:LinkEntities>" +  
" <q1:LinkEntity>" +  
" <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" +  
" <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" +  
" <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" +  
" <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" +  
" <q1:JoinOperator>Inner</q1:JoinOperator>" +  
" <q1:LinkEntities>" +  
" <q1:LinkEntity>" +  
" <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +  
" <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" +  
" <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +  
" <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +  
" <q1:JoinOperator>Inner</q1:JoinOperator>" +  
" <q1:LinkCriteria>" +  
" <q1:FilterOperator>And</q1:FilterOperator>" +  
" <q1:Conditions>" +  
" <q1:Condition>" +  
" <q1:AttributeName>systemuserid</q1:AttributeName>" +  
" <q1:Operator>EqualUserId</q1:Operator>" +  
" </q1:Condition>" +  
" </q1:Conditions>" +  
" </q1:LinkCriteria>" +  
" </q1:LinkEntity>" +  
" </q1:LinkEntities>" +  
" </q1:LinkEntity>" +  
" </q1:LinkEntities>" +  
" </query>" +  
" </RetrieveMultiple>" +  
" </soap:Body>" +  
"</soap:Envelope>" +  
"";  
 
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");  
 
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");  
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);  
xmlHttpRequest.send(xml);  
 
var resultXml = xmlHttpRequest.responseXML;  
return(resultXml);  
}
function GetCurrentTeams()  
{  
var xml = "" +  
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +  
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +  
GenerateAuthenticationHeader() +  
" <soap:Body>" +  
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +  
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +  
" <q1:EntityName>team</q1:EntityName>" +  
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +  
" <q1:Attributes>" +  
" <q1:Attribute>name</q1:Attribute>" +  
" </q1:Attributes>" +  
" </q1:ColumnSet>" +  
" <q1:Distinct>false</q1:Distinct>" +  
" <q1:LinkEntities>" +  
" <q1:LinkEntity>" +  
" <q1:LinkFromAttributeName>teamid</q1:LinkFromAttributeName>" +  
" <q1:LinkFromEntityName>team</q1:LinkFromEntityName>" +  
" <q1:LinkToEntityName>teammembership</q1:LinkToEntityName>" +  
" <q1:LinkToAttributeName>teamid</q1:LinkToAttributeName>" +  
" <q1:JoinOperator>Inner</q1:JoinOperator>" +  
" <q1:LinkEntities>" +  
" <q1:LinkEntity>" +  
" <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +  
" <q1:LinkFromEntityName>teammembership</q1:LinkFromEntityName>" +  
" <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +  
" <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +  
" <q1:JoinOperator>Inner</q1:JoinOperator>" +  
" <q1:LinkCriteria>" +  
" <q1:FilterOperator>And</q1:FilterOperator>" +  
" <q1:Conditions>" +  
" <q1:Condition>" +  
" <q1:AttributeName>systemuserid</q1:AttributeName>" +  
" <q1:Operator>EqualUserId</q1:Operator>" +  
" </q1:Condition>" +  
" </q1:Conditions>" +  
" </q1:LinkCriteria>" +  
" </q1:LinkEntity>" +  
" </q1:LinkEntities>" +  
" </q1:LinkEntity>" +  
" </q1:LinkEntities>" +  
" </query>" +  
" </RetrieveMultiple>" +  
" </soap:Body>" +  
"</soap:Envelope>" +  
"";  
 
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");  
 
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");  
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);  
xmlHttpRequest.send(xml);  
 
var resultXml = xmlHttpRequest.responseXML;  
return(resultXml);  
}  

function UserHasTeam(teamName)
{
               //get Current User Teams, oXml is an object  

                var oXml = GetCurrentTeams();  
               if(oXml != null)  
               {  
               //select the node text  
               var teams = oXml.selectNodes("//BusinessEntity/q1:name");  
               if(teams != null)  
               {  
                               for( i = 0; i < teams.length; i++)  
                               {  
                                               if(teams[i].text == teamName)
                                                                return true;

                                }  
               }  
               }
               return false;
}
function HideSection( tabIndex , sectionIndex , displayType )
{
  var tab2Hide = document.getElementById( "tab" + tabIndex );
  tab2Hide.childNodes[0].rows[ sectionIndex ].style.display = displayType;
}

if(!UserHasTeam("PriceSupportApprover"))
{
  HideSection(0, 1, "none");
}
/************************************************
function to calculate the total support value
Last Edited: Pratima Surapaneni 4/7/09
************************************************/
CalculateTotal = function()
{
  /* var currency = crmForm.all.transactioncurrencyid;
   if (currency.DataValue == null)
 {
  // Create an array to set as the DataValue for the lookup control.
  var lookupData = new Array();
  // Create an Object add to the array.
  var lookupItem= new Object();
  // Set the id, typename, and name properties to the object.
  lookupItem.id = '{F84F576A-3EC9-DC11-A8D2-0003FF9EE217}';
  lookupItem.typename = 'transactioncurrency';
  lookupItem.name = 'US Dollar';
  // Add the object to the array.
  lookupData[0] = lookupItem;
  // Set the value of the lookup field to the value of the array.
  currency.DataValue = lookupData;
 }*/
 
   if(crmForm.all.new_royalsupport.DataValue == null && crmForm.all.new_vendorsupport.DataValue == null)
  {
     crmForm.all.new_value.DataValue = 0;
  }
 else if(crmForm.all.new_royalsupport.DataValue == null)
  {
     crmForm.all.new_value.DataValue = crmForm.all.new_vendorsupport.DataValue;
  }
  else if(crmForm.all.new_vendorsupport.DataValue == null)
  {
     crmForm.all.new_value.DataValue = crmForm.all.new_royalsupport.DataValue;
  }
  else
  {
     crmForm.all.new_value.DataValue = crmForm.all.new_vendorsupport.DataValue + crmForm.all.new_royalsupport.DataValue;
  }
crmForm.all.new_value.ForceSubmit = true;
}
CalculateTotal();

 

by: jgould763Posted on 2009-06-10 at 11:43:12ID: 24594716

After thinking about it a little more, it can't be anything in the onLoad script.  If I query the DB after a "Save & Close" the field is the sames as it was prior to the Save & Close.

I hope this helps.

 

by: rakeshAgarwalPosted on 2009-06-10 at 23:37:39ID: 24599286

Can you please check this steps?

1. have you enabled on save event
2. Is this field read only , Then you have to use forcesubmit in onsave
3.  can you put an lert check wether this goes in on save vent
4. May be some code before this faulting

 This  are my thoughts
hope this helps
Rakesh

 

by: feridunPosted on 2009-06-10 at 23:46:51ID: 24599337

It is unlikely to a bug, there will be something that we have overlooked. May I check my understanding of what is happening?

 - If you create a new record and then save it, the OnSave event populates the field and if you close the form and then open it again, the update to the field remains.

- However, for an existing record when you click Save in the form, the field gets updated but if you close the form and then open it again, the update has gone.

Please let me know if any of that is not right.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...