Asked by:
Concatenate two fields into a third field when your leave focus of either of the two originating fields

Question
-
I'm working with CRM Online. I would like to concatenate the first field to the second field with a little text string in between. I would like the third field to update upon moving focus from either the first field or second field, so it updates even prior to the save event. Is that possible using the new PBL for input forms, or should I use some javascript? If javascript is needed, what would the code be and where would it go specifically? Thanks!
Don
Thursday, June 5, 2014 6:10 AM
All replies
-
Hi,
You would have to use JavaScript to achieve that. Use the following piece of code:
var firstField = Xrm.Page.getAttribute("first_field_name").getValue(); var secondField = Xrm.Page.getAttribute("second_field_value").getValue(); var mergedValue = firstField + "your_small_string" + secondField; Xrm.Page.getAttribute("merged_field_name").setValue(mergedValue);
Wire up this piece of JavaScript on change of First field and Second field.Thursday, June 5, 2014 6:19 AM -
Thank you so much for your reply. Apparently I forgot to mention that the first field was a lookup field to the parent record. So I'm getting this object name showing, with the little text string and second field showing correctly (the second field is a simple text box). I thought about relationship mapping but that apparently won't work if the first field is modified. So what is the javascript needed to work with a lookup field? Thanks again!
Don
Don
Thursday, June 5, 2014 6:58 AM -
Hi,
In that case, use this code:
var firstField = Xrm.Page.getAttribute("first_field_name").getValue(); var firstFieldVal = ""; if (firstField != null) { firstFieldVal = firstField[0].name; } var secondFieldVal = Xrm.Page.getAttribute("second_field_value").getValue(); var mergedVal = firstFieldVal + "your_small_string" + secondFieldVal; Xrm.Page.getAttribute("merged_field_name").setValue(mergedValue);
Thursday, June 5, 2014 7:06 AM -
Well, I'm getting an error. Here is the exact code I am using:
var firstField = Xrm.Page.getAttribute("hbw3_subdivisionid").getValue();
var firstFieldVal = "";if (firstField != null)
{
firstFieldVal = firstField[0].name;
}var secondFieldVal = Xrm.Page.getAttribute("hbw3_lotnumber").getValue();
var mergedVal = firstFieldVal + ", Lot " + secondFieldVal;Xrm.Page.getAttribute("hbw3_name").setValue(mergedValue);
I have closely verified the field names (the Lot Number "second field' carried over in the first example you provided. Any ideas why the complete solution with lookup addressed is not working? Thanks very much!
Don
Don
Thursday, June 5, 2014 7:31 AM -
Hi,
You used mergedValue instead of mergedVal
var firstField = Xrm.Page.getAttribute("hbw3_subdivisionid").getValue(); var firstFieldVal = ""; if (firstField != null) { firstFieldVal = firstField[0].name; } var secondFieldVal = Xrm.Page.getAttribute("hbw3_lotnumber").getValue(); var mergedVal = firstFieldVal + ", Lot " + secondFieldVal; Xrm.Page.getAttribute("hbw3_name").setValue(mergedVal);
- Proposed as answer by Mayank PujaraEditor Thursday, June 5, 2014 9:52 AM
Thursday, June 5, 2014 9:12 AM -
Still getting an error. I even tried deleting the jscript library record and the onChange event record for both fields, and created them with the latest version of code. Then I deleted the jscript library record and the onChange event record and no more error. Please advise. Going offline for a few hours and will follow up then. This is something I must get working. Thanks!
Don
Don
Thursday, June 5, 2014 9:57 AM -
A line of code I've used previously for getting text from a Option Set, not sure if it'll make any difference for a lookup, try the following
Var firstField = Xrm.Page.data.entity.attributes.get("hbw3_subdivisionid").getValue(); //For the value
or possibly
Var firstField = Xrm.Page.data.entity.attributes.get("hbw3_subdivisionid").getText(); //For the text
Shaun
Thursday, June 5, 2014 10:10 AM -
Hi,
What is the exact error that you're getting? I tried the exact same thing in CRM 2013, and it works perfectly fine.
Thursday, June 5, 2014 10:12 AM -
Hi,
This is from a error dialog displayed when trying to exit the record. It does work when opening an already created recorded, but not on create new record. Thanks!
Don
Microsoft Dynamics CRM Error Report Contents
<CrmScriptErrorReport>
<ReportVersion>1.0</ReportVersion>
<ScriptErrorDetails>
<Message>Syntax error</Message>
<Line>1</Line>
<URL>/main.aspx?etc=10008&extraqs=%3f_gridType%3d10008%26etc%3d10008%26id%3d%257b9F330479-48ED-E311-AA18-6C3BE5A8DD60%257d%26rskey%3d284577376&pagemode=iframe&pagetype=entityrecord&rskey=284577376</URL>
<PageURL>/main.aspx?etc=10008&extraqs=%3f_gridType%3d10008%26etc%3d10008%26id%3d%257b9F330479-48ED-E311-AA18-6C3BE5A8DD60%257d%26rskey%3d284577376&pagemode=iframe&pagetype=entityrecord&rskey=284577376</PageURL>
<Function></Function>
<CallStack>
</CallStack>
</ScriptErrorDetails>
<ClientInformation>
<BrowserUserAgent>Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3; WOW64; Trident/7.0; Touch; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0; InfoPath.3)</BrowserUserAgent>
<BrowserLanguage>en-US</BrowserLanguage>
<SystemLanguage>en-US</SystemLanguage>
<UserLanguage>en-US</UserLanguage>
<ScreenResolution>1463x823</ScreenResolution>
<ClientName>Web</ClientName>
<ClientTime>2014-06-06T02:02:24</ClientTime>
</ClientInformation>
<ServerInformation>
<OrgLanguage>1033</OrgLanguage>
<OrgCulture>1033</OrgCulture>
<UserLanguage>1033</UserLanguage>
<UserCulture>1033</UserCulture>
<OrgID>{05BD3A83-F78B-4BCD-80A8-F76B2B9CF81E}</OrgID>
<UserID>{42FD84CA-7455-4961-84F0-392E16EC3693}</UserID>
<CRMVersion>6.1.0.575</CRMVersion>
</ServerInformation>
</CrmScriptErrorReport>Don
Friday, June 6, 2014 7:10 AM -
Hi,
I tried the same script, it works fine with both new and existing records. Have you wired the script with Form on Load?
Friday, June 6, 2014 9:35 AM