Skip to main content

Search

Update lookup column after adding item

Comments

2 comments

  • Henning Eiben

    AFAIK this is not possible in "classic" forms, however in modern forms this is OOTB possible as there is a built-in option to add new items to lookup lists.

    0
  • Felix Müller

    Unfortunately we can't use modern forms yet because some features are not yet implemented.

    I found a solution even if it is a "dirty hack":

    After opening the New-Form, I run following script:

    var addedID = [[@Web.GetFirstValueForQuery('Vertragspartner', '<OrderBy><FieldRef Name="Created" Ascending="FALSE" /></OrderBy><Where><And><Eq><FieldRef Name="Author" /><Value Type="Integer"><UserID /></Value></Eq><Geq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue="TRUE">'+[[=new Date((new Date()).setMinutes((new Date()).getMinutes() - 1)).toISOString()]]+'</Value></Geq></And></Where>', 'ID')]];
    var addedCompany = [[@Web.GetFirstValueForQuery('Vertragspartner', '<OrderBy><FieldRef Name="Created" Ascending="FALSE" /></OrderBy><Where><And><Eq><FieldRef Name="Author" /><Value Type="Integer"><UserID /></Value></Eq><Geq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue="TRUE">'+[[=new Date((new Date()).setMinutes((new Date()).getMinutes() - 1)).toISOString()]]+'</Value></Geq></And></Where>', 'Company')]];
    if (addedID && $("select[id^='icVertragspartner'] option[value='"+addedID+"']").length === 0 ) {
      $("select[id^='icVertragspartner']" ).append(new Option(addedCompany, addedID));
      $("select[id^='icVertragspartner']" ).val(addedID);
      $("select[id^='icAnsprechpartner']" ).find('option').not(':first').remove();
    }

    First I try to get the last created item from my Sublist. After that I use jQuery to check if this item is already in the dropdown (this can happen when I abort the creation of a new item in the sublist). If it is not already there, I manually add the new item and select it afterwards.

    It is not an actual update but it does the job.

    0

Please sign in to leave a comment.