Update Lookup Type Custom Field value Using Javascript object Model(JSOM)

In this post, I will explain to Update Lookup Type Custom Field value Using Javascript object Model(JSOM). In my previous post, I explained you to Update Custom Field Value using Java Script Object Model(JSOM). You can also update lookup type custom field value using Client-Side Object Model(CSOM) and Project Server Interface(PSI) as I explained you in my previous post.

Update Non-Lookup type custom field using below code

draftProject.setCustomFieldValue(customfieldData[0].InternalName, "Custom Field Value");

Update lookup Type custom field using below code.

var LookupEntryInternalName = lookupEntryData[0].InternalName;
           draftProject.setCustomFieldValue(customfieldData[0].InternalName, [LookupEntryInternalName]);

Note: If you are updating lookup type custom field then pass lookup table value Internal Name in square brackets [ ].

You can get lookup table value using below code

 lookupEntriesOBJ.push({
                                    InternalName: ltEntry.get_internalName(),
                                    Value: ltEntry.get_value(),
                                    FullValue: ltEntry.get_fullValue(),
                                    Description: ltEntry.get_description()
                                });

[AdSense-A]

Here is complete code to update lookup and non-lookup value using Javascript Object Model(JSOM)

<script src="../_layouts/15/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../_layouts/15/sp.runtime.js"></script>    
    <script type="text/javascript" src="../_layouts/15/sp.js"></script>
    <script type="text/javascript" src="../_layouts/15/ps.js"></script>

<script>
    var CustomfieldName;
    var lookupEntriesOBJ = [];
    var ProjectContext;
    var projectsOBJ;
    var customFieldsOBJ;
    var customFieldDataOBJ = [];
    function UpdateCus() {
        CustomfieldName = $('#txtGuid').val();
        SP.SOD.executeOrDelayUntilScriptLoaded(GetCustomFields, "PS.js");

    };
    function GetCustomFields() {
        
        ProjectContext = PS.ProjectContext.get_current();

        projectsOBJ = ProjectContext.get_projects();

        customFieldsOBJ = ProjectContext.get_customFields();
        ProjectContext.load(customFieldsOBJ);

        // Run the request on the server.
        ProjectContext.executeQueryAsync(getCFComplete);
    }

    function getCFComplete(response) {
        var cfEnumerator = customFieldsOBJ.getEnumerator();

        while (cfEnumerator.moveNext()) {
            var cf = cfEnumerator.get_current();
            while (cfEnumerator.moveNext()) {
                var cf = cfEnumerator.get_current();


                customFieldDataOBJ.push({
                    Id: cf.get_id(),
                    Name: cf.get_name(),
                    InternalName: cf.get_internalName()
                });
               
                if (cf.get_name() === CustomfieldName) {
                    var lookupTable = cf.get_lookupTable();

                   
                    ProjectContext.load(lookupTable);
                    ProjectContext.executeQueryAsync(function () {
                        var ltEntries = lookupTable.get_entries();

                        
                        ProjectContext.load(ltEntries);
                        ProjectContext.executeQueryAsync(function () {
                            var ltEnum = ltEntries.getEnumerator();

                            while (ltEnum.moveNext()) {
                                var ltEntry = ltEnum.get_current();

                                lookupEntriesOBJ.push({
                                    InternalName: ltEntry.get_internalName(),
                                    Value: ltEntry.get_value(),
                                    FullValue: ltEntry.get_fullValue(),
                                    Description: ltEntry.get_description()
                                });
                                updateCustomField();
                            }
                           
                           
                           
                        });
                    });
                }
                updateCustomField();
            }
           
            
        }
       
    }
    function updateCustomField() {
        var projectId = "9bf4a904-0df4-e611-80fe-00155d018621";
        var project = projectsOBJ.getById(projectId);
        var draftProject = project.checkOut();
        var CustomfieldName = $('#txtGuid').val();//*LookupTypeCustomField: This is your Custom Field name
        // Update custom field
        var customfieldData = $.grep(customFieldDataOBJ, function (val) { //Filter custom field
            return val.Name === CustomfieldName;
        });
        var lookupEntryData = $.grep(lookupEntriesOBJ, function (val) {  //Filter lookup entry value
            return val.Value === "IT"; //*ME: This is your one of lookup entry value of your lookup table
        });

       // If you custom field is lookup type then if condition work
        if (lookupEntryData.length > 0 && customfieldData.length > 0) {
            var LookupEntryInternalName = lookupEntryData[0].InternalName;
           draftProject.setCustomFieldValue(customfieldData[0].InternalName, [LookupEntryInternalName]);
        }
            // If your custom field is NON-lookup type then else condition work
        else if (customfieldData.length > 0) {
            draftProject.setCustomFieldValue(customfieldData[0].InternalName, "Custom Field Value");// *Custom field value: this is custom field value in case your custom field is NON-Lookup Type
        }

        //Publish the change
        var publishJob = draftProject.publish(true);
        //Monitor the job
        ProjectContext.waitForQueueAsync(publishJob, 30, function (response) {
            if (response !== 4) {
                // handle errors
            }
        });
    };
</script>
Enter Custom Field Name: <textarea id="txtGuid"></textarea>
<button id="btnCustom" onclick="UpdateCustom()" value="Update" type="button"></button>

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!