Copy all attachments from Idea to Demand

RakshithaM
Tera Contributor

Hi,

 

I want to copy all the attachments which are present on Idea to Demand. 

I have used After business rule with Insert for below code.

 

But the below code isn't working as per requirement. Can someone guide me where I need to correct.

Have attached screenshot for reference.

Thankyou.

 

(function executeRule(current, previous /*null when async*/ ) {

    var demandTable = "dmn_demand";
    if (GlidePluginManager.isActive('com.snc.project_management_v3')) {
        demandTable = SNC.PPMConfig.getDemandTable(current.getTableName());
    }
    var demand = new GlideRecord(demandTable);
    var dmnId = demand.insert();
    demand.get(dmnId);
    current.demand = dmnId;
    current.stage = 'demand';
    GlideSysAttachment.copy('idea',current.sys_id, 'dmn_demand', demand.sys_id);
   
    var message = gs.getMessage("Demand {0} has been created");
})(current, previous);

 Screenshot 2025-05-15 203333.png

7 REPLIES 7

ursnani
Giga Guru

Try using the below code

GlideSysAttachment.copy('idea',current.sys_id,'dmn_demand',dmId)

Vishal Jaswal
Giga Sage

Hello @RakshithaM 

1. You should not use .get after insert() as it records a new empty GlideRecord where the sys_id of the inserted record is getting lost.

2. You should not set the current.demand and current.stage after demand creation as they won't persist unless you explicitly call current.update() which again is problematic in after business rule as it can become recursive.


Here is the modified code:

(function executeRule(current, previous /* null when async */) {
    var demandTable = "dmn_demand";
    if (GlidePluginManager.isActive('com.snc.project_management_v3')) {
        demandTable = SNC.PPMConfig.getDemandTable(current.getTableName());
    }

    // Create the new demand record
    var demand = new GlideRecord(demandTable);
    demand.initialize();
    demand.short_description = current.short_description; // This is just sample
    demand.description = current.description; // This is just sample

    // Add more field mappings as needed
    var dmnId = demand.insert();
    if (dmnId) {
    
        var idea = new GlideRecord(current.getTableName());
        if (idea.get(current.sys_id)) {
            idea.demand = dmnId;
            idea.stage = 'demand';
            idea.update(); // This avoids using current.update() in After Business Rule
        }

        // Copy attachments
        GlideSysAttachment.copy(current.getTableName(), current.sys_id, demandTable, dmnId);
        gs.info("Demand " + dmnId + " has been created and linked to Idea " + current.sys_id);
    } else {
        gs.error("Failed to create demand from Idea: " + current.sys_id);
    }
})(current, previous);

 


Hope that helps!

Hi @Vishal Jaswal ,

 

I tried the code which you have shared, but it's not copying attachments from Idea to Demand. Below I have shared the Screenshot of what I have been using, could you please correct me where I'm wrong.

Thankyou.

 

Screenshot 2025-05-16 114417.png

 

did you get this to work?