Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get last name of a contact and write it in another field with a Script

Posted on by Microsoft Employee

Hello,

I'm new to Scripts in CRM, so I have a basic question: How do I get the last name of a contact and then write this last name into a field (of the entity contact) I created? Let's call this field new_testfield. I know that I can do that with a business rule or with a workflow, but I want to practice using scripts.

I've already researched a bit, but all the websites I found just had some general or really specific advice. I haven't found some useful "Getting started"/"How to" guide with examples for Scripts yet, so I wondered if anyone here could help me with the simple task I described in the first paragraph.

Thanks a lot!

Nooyi

*This post is locked for comments

  • Verified answer
    PS Profile Picture
    PS 23,577 on at
    RE: Get last name of a contact and write it in another field with a Script

    Hi Nooyi,

    Use the following script:

    function CopyLastName() {

    var name = Xrm.Page.getAttribute('lastname').getValue();

    if (lastname != null) {

       Xrm.Page.getAttribute('new_testfield').setValue(name);

    }

    }

  • Suggested answer
    TheMarkChristie Profile Picture
    TheMarkChristie 10,328 on at
    RE: Get last name of a contact and write it in another field with a Script

    Here is a common script for copy all values from one entity to another - I'm sure you can look at it and get some ideas

    if (crmForm.all.new_incomingcustomeraccountid.DataValue != null)

    {

    var id = crmForm.all.new_incomingcustomeraccountid.DataValue[0].id;

    //alert(id);

    var xml = "" +

    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

    "<soap:Envelope xmlns:soap=\"schemas.xmlsoap.org/.../envelope\" xmlns:xsi=\"www.w3.org/.../XMLSchema-instance\" xmlns:xsd=\"www.w3.org/.../XMLSchema\">" +

    GenerateAuthenticationHeader() +

    " <soap:Body>" +

    " <RetrieveMultiple xmlns=\"schemas.microsoft.com/.../WebServices\">" +

    " <query xmlns:q1=\"schemas.microsoft.com/.../Query\" xsi:type=\"q1:QueryExpression\">" +

    " <q1:EntityName>account</q1:EntityName>" +

    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

    " <q1:Attributes>" +

    " <q1:Attribute>accountid</q1:Attribute>" +

    " <q1:Attribute>address1_line1</q1:Attribute>" +

    " <q1:Attribute>address1_line2</q1:Attribute>" +

    " <q1:Attribute>address1_city</q1:Attribute>" +

    " <q1:Attribute>address1_country</q1:Attribute>" +

    " <q1:Attribute>address1_postalcode</q1:Attribute>" +

    " </q1:Attributes>" +

    " </q1:ColumnSet>" +

    " <q1:Distinct>false</q1:Distinct>" +

    " <q1:Criteria>" +

    " <q1:FilterOperator>And</q1:FilterOperator>" +

    " <q1:Conditions>" +

    " <q1:Condition>" +

    " <q1:AttributeName>accountid</q1:AttributeName>" +

    " <q1:Operator>Equal</q1:Operator>" +

    " <q1:Values>"+

    " <q1:Value xsi:type=\"xsd:string\">"+ id +"</q1:Value>"+

    " </q1:Values>"+

    " </q1:Condition>" +

    " </q1:Conditions>" +

    " </q1:Criteria>" +

    " </query>" +

    " </RetrieveMultiple>" +

    " </soap:Body>" +

    "</soap:Envelope>";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../RetrieveMultiple&quot;);

    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML.xml;

    //alert(resultXml );

    if (resultXml != null || resultXml != "")

    {

    var xmlDocument =new ActiveXObject("Microsoft.XMLDOM");

    var bLoaded = xmlDocument.loadXML(resultXml);

    if (bLoaded)

      try

                {

    var Line1 = xmlDocument.selectSingleNode("//BusinessEntity/q1:address1_line1").text;

    crmForm.all.new_address1.DataValue = Line1;

    crmForm.all.new_address1.ForceSubmit = true;

                             }

                catch(err){

    alert ("Line 1 Missing");

    crmForm.all.new_address1.DataValue ='';

    }

      try

                {

    var Line2 = xmlDocument.selectSingleNode("//BusinessEntity/q1:address1_line2").text;

    crmForm.all.new_address2.DataValue = Line2;

    crmForm.all.new_address2.ForceSubmit = true;

                            }

                catch(err){

    alert ("Line 2 Missing");

    crmForm.all.new_address2.DataValue ='';

    }

     try

                {

    var Line3= xmlDocument.selectSingleNode("//BusinessEntity/q1:address1_city").text;

    crmForm.all.new_address3.DataValue = Line3;

    crmForm.all.new_address3.ForceSubmit = true;

                            }

                catch(err){

    alert ("Town Missing");

    crmForm.all.new_address3.DataValue ='';

    }

    try

                {

    var Line4= xmlDocument.selectSingleNode("//BusinessEntity/q1:address1_country").text;

    crmForm.all.new_address4.DataValue = Line4;

    crmForm.all.new_address4.ForceSubmit = true;

                            }

                catch(err){

    alert ("County Missing");

    crmForm.all.new_address4.DataValue ='';

    }

    try

                {

    var Line5= xmlDocument.selectSingleNode("//BusinessEntity/q1:address1_postalcode").text;

    crmForm.all.new_postcode.DataValue = Line5;

    crmForm.all.new_postcode.ForceSubmit = true;

                            }

                catch(err){

    alert ("Post Code Missing");

    crmForm.all.new_postcode.DataValue = '';

    }

    }

     }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans