0

I am working with the CPQ Jvascript Calculator. I writing logic on BeforePriceRules. I will need to do a couple of Queries to get what I need. When I write my first Query the order of Operation of how the code execute does not make sense.

When I call the Function GetProductComp, the code fires the Query but then leaves the Function and runs the next line in onBeforePriceRules and then goes back into GetProductComp and Loops through the records to create a map and then the program ends.

What I want it to do is to call the Function GetProductComp do the Query, loop through the records create the map, return the map and continue the onBeforePriceRules function. Any Idea as to why its not doing this??

export function onBeforePriceRules(quote, lineModels, conn) {


if (lineModels.length) {
var varProdIds = [];
//Might want to do a function here. 
lineModels.forEach(function(line) {
  var varProdId= line.record['SBQQ__Product__c'];
  if (varProdId) {
    varProdIds.push(varProdId);
  }  
})
};

const UniqueProdList = [...new Set(varProdIds)];
var ProdIdList = "('" + UniqueProdList.join("', '") + "')"

 console.log('ProdIdList' + ProdIdList);
 debugger;

 //Declare a Map. Call a Function that will produce the map.
var ProductMap = new Map();
ProductMap = GetProductComp(conn,ProdIdList);

//Carry on after the map is created.
var NewVarable = "NewThing";

console.log('NewVarable' + NewVarable);
debugger;
;

 return Promise.resolve();
}


function GetProductComp(conn, ProdIdList){
const ProductMap = new Map();
//Get Products for a Check 
conn.query('SELECT Id, Model__c FROM Product2 WHERE ID IN' + ProdIdList)
.then(function(results) {
//Loop through the Results from the Query, if there is results create a key value pair
if (results.totalSize){
 results.records.forEach(function(record) {
   ProductMap.set(record.Id, record);
   console.log('ProductMap' + ProductMap);
   debugger;

 })
 }
 console.log('ProductMap' + ProductMap);
 debugger;
 });

 return ProductMap;
}

Chaining Promise

export function onBeforePriceRules(quote, lineModels, conn) {


var varQuoteHostid;

if (lineModels.length) {
var varProdIds = [];
//Might want to do a function here. 
lineModels.forEach(function(line) {
  varQuoteHostid = quote.record['Host__c'];
  var varProdId= line.record['SBQQ__Product__c'];
  if (varProdId) {
    varProdIds.push(varProdId);
  }  
})
};
 
const UniqueProdList = [...new Set(varProdIds)];
var ProdIdList = "('" + UniqueProdList.join("', '") + "')"

let myMap;
var ProductMap = new Map();

 conn.query('SELECT Id, Model__c FROM Product2 WHERE ID IN' + ProdIdList).then(
  function(returnedRecords) {
    if (returnedRecords.totalSize){
        returnedRecords.records.forEach(function(record) {
        ProductMap.set(record.Id, record);
        console.log('ProductMap' + ProductMap);
        debugger;
    })
  }
},
function(error){
  //handle error
}

).then(function() {
  myMap = ProductMap;
}
);

console.log('ProductMap' + ProductMap);
debugger;

var NewVarable = "NewThing";
console.log('NewVarable' + NewVarable);
debugger;
    

return Promise.resolve();
}

1 Answer 1

0

I suggest you to have a look here to have a better understanding on how promises works. The function that you have as the first input parameter of your then() method is asynchronous and executed when the server returns the query results. This means that here:

ProductMap = GetProductComp(conn,ProdIdList);

you're not waiting for the function GetProductComp to complete before proceeding with the next instruction.

What I would do in this case, as explained in the link I shared, is chaining multiple function calls, like this:

let myMap;
conn.query('SOQL').then(
    function(returnedRecords) {
        //build productMap with returnedRecords
        myMap = productMap;
    },
    function(error){
        //handle error
    }
).then(
    function() {
        //use myMap to do your logic
    }
    function(error) {
        //handle error
    }
);
2
  • I have read the link you have sent and tried the example that you gave. I added the code that I tried in the Question under Chaining Promise above however it appears that it doesnt wait still and jumps past building a map. Any suggestions?
    – CodeMonger
    Feb 1, 2022 at 14:21
  • Everything you added after the second then() method should be inside of the then() method itself.
    – vila994
    Feb 1, 2022 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .