snippet to change displaytype from 'REFERENCE' to 'PICKLIST' at runtime

  • 1
  • Question
  • Updated 4 years ago
  • Answered
Hello ...

I would like to use custom field renderer on a reference field . I am able to use custom renderer but I was not able to change the displaytype to picklist. Is there any way to change the displaytype from reference to picklist from JS snippet. 

Thank you.
-Jnanendra
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
My current snippet to render owner field is as follows:


var field = arguments[0],    value = skuid.utils.decodeHTML(arguments[1]),
    $ = skuid.$,
    displayType = field.metadata.displaytype,
    targetFieldId;
if (displayType === 'STRING' || displayType === 'TEXT') {
    // We assume that this is the Name field value,
    // otherwise there wouldn't be a link
    targetFieldId = field.row.Id;
} else if (displayType === 'REFERENCE') {
    targetFieldId = value;
}
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
if (field.mode !== 'edit') {
    var link = field.element.find('a');
    if (link.length) {
        link.attr('href','/apex/TeamMemberView?id=' + targetFieldId);
    }
}
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Change this line:

skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);

to this:

skuid.ui.fieldRenderers[PICKLIST][field.mode](field,value);
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Small typo in Moshe's post, you need quotes around 'PICKLIST'.  It should be like this...

skuid.ui.fieldRenderers['PICKLIST'][field.mode](field,value);
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
I'm assuming that by "Picklist" you mean a Reference field rendered as a Picklist, as opposed to an autocomplete.

The way we encourage this to be done is to change field.options.type to 'REFPICK', then run the default renderer:

field.options.type = 'REFPICK';
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
Thank you very much Zach !

However, when I added field.options.type = 'REFPICK'; it is giving me original link but not the intended link ('href','/apex/TeamMemberView?id=' + targetFieldId)). 

-Jnanendra
(Edited)
Photo of Gyan

Gyan

  • 1,400 Points 1k badge 2x thumb
Hi Zach, I tried your approach and tried to solve problem by rewriting the snippet as :


var field = arguments[0],    value = skuid.utils.decodeHTML(arguments[1]),
    $ = skuid.$,
    displayType = field.metadata.displaytype,
    targetFieldId;
if (displayType === 'STRING' || displayType === 'TEXT') {
    // We assume that this is the Name field value,
    // otherwise there wouldn't be a link
    targetFieldId = field.row.Id;
} else if (displayType === 'REFERENCE') {
    targetFieldId = value;
}
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);

// in edit mode render the reference field as pick list       
if(field.mode === 'edit') {
        field.options.type = 'REFPICK';
        skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field, value);

    }

if (field.mode !== 'edit') {
    var link = field.element.find('a');
    if (link.length) {
        link.attr('href','/apex/TeamMemberView?id=' + targetFieldId);
    }
}



This code works fine except  I edit the record ( inline edit) and cancel it , the link takes me to original salesforce link (https://ap1.salesforce.com/00590000002Qaqs) instead of 
href','/apex/TeamMemberView?id=' + targetFieldId

 How could I address this issue  and make it work for the edit and cancel scenario as well?

Thanks!
-Jnanendra 
(Edited)