locked
Javascript - String Manipulation - Return portion of text RRS feed

  • Question

  • Hi

    We have 2 custom fields on our CRM form and we would like to populate a field with data that is contained in a multiple line of text field.

    The multiple line of text field is called new_description and receives data automatically from a database. We would like to strip out a portion of the text between two words and populate the additional field with this data.

    So if the multiple line of text field contains the following text:

    'This is an example only of text that would be received automatically from the database'

    An additional text field should only include the value - 'example only of text'

    We are trying to find the JavaScript which will check up to the word 'an' and return the words thereafter up to the word 'that'.

    I hope this makes sense and would really appreciate any assistance.

    Thanks

    Regards
    Tony

    Wednesday, November 19, 2014 11:35 AM

All replies

  • Hi Tony,

    I have found a solution for your issue.

    Please create 3 fields on the form as in the image below.

    Please use this javascript to get the desired string part.

    function getStringPart(){
    
    var description = Xrm.Page.getAttribute("description").getValue();
    
    var starttext = Xrm.Page.getAttribute("new_starttext").getValue();
    var endtext = Xrm.Page.getAttribute("new_endtext").getValue();
    
    var start = description.indexOf(starttext);
    var end = description.indexOf(endtext);
    
    diff = end - start;
    
    var res = description.substr(start, diff)
    
    Xrm.Page.getAttribute("new_result").setValue(res);
    
    
    }
    


    Regards, Saad

    Wednesday, November 19, 2014 12:30 PM
  • Hi Saad

    Thank you very much for your reply. This works perfectly except for two issues:

    1). Your suggestion assumes that the Start Text and End Text have been prepopulated. I can prepopulate the fields using an OnLoad function but I was planning to implement something a bit more dynamic.

    2). The Start Text includes the first word of my result. This unfortunately cannot be prepopulated and will change for each description that is prepopulated for the database.

    Any other suggestions for the Start Text?

    Thanks again

    Regards
    Tony

    Wednesday, November 19, 2014 1:33 PM
  • Following on from Saad's suggestion:

    1. To make this a bit more dynamic, instead of the using the two lines to get starttext and endtext from a field on the form, you could assign the 'search for' strings via the JavaScript code.

    2. To make the code search for the last part of the first 'search for' string, again, have the string assigned to the starttext variable but change the code from

    var start = description.indexOf(starttext)

    where starttext is 'example' to

    var start = description.indexOf(starttext)
    start = start + 1 + starttext.length;
    so that it will cope with starttext being 'an'. It just finds the location of the beginning of 'an' (or whatever you want) in the string, adds on 1 (for the space in between the words) and then adds on the length of starttext - this should give you the start of the string you want to extract.

    Wednesday, November 19, 2014 2:54 PM