function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ciprian Stoica 12Ciprian Stoica 12 

Add field to list items in web service

Hi,

I'm creating a REST WebService that allows placing an order for a product.
Basically, I'm receiving an HTTP Post with a list of products and quantity.

When I receive the message my webservice creates a new order, but I can't seem to place the order.id as a parameter in my list json for each of the products.

My webservice is this:
 
global static list<Coffee_Order_Product__c> echoMyType(list<Coffee_Order_Product__c> ic) {
    Coffee_Order__c newOrder = new Coffee_Order__c();
        insert newOrder;
    for(Coffee_Order_Product__c:ic) {
        ic.add(newOrder.Id);
    }
    insert ic;    
    return ic;
    }
	 
}

my JSON should be:
{
    "ic" : [
                   { "Order_Qty__c" : "7", "Cofee__c" : "a010O000029R3b6QAC","Name" : "prod1"},
                   { "Order_Qty__c" : "4", "Cofee__c" : "a010O000029R3baQAC","Name" : "prod2"},
                   { "Order_Qty__c" : "1", "Cofee__c" : "a010O000029R3b6QAC","Name" : "prod3"}
             ]
}
I get an unexpected token ':' at line : for(Coffee_Order_Product__c:ic)

Any help please?

 
Best Answer chosen by Ciprian Stoica 12
Waqar Hussain SFWaqar Hussain SF
You are adding Coffee_Order__c  in list of Coffee_Order_Product__c. which is wrong

See below code snippet
global static list<Coffee_Order_Product__c> echoMyType(list<Coffee_Order_Product__c> ic) {
    Coffee_Order__c newOrder = new Coffee_Order__c();
        insert newOrder;
    for(Coffee_Order_Product__c c:ic) {
        //ic.add(newOrder.Id);
		//here you can assign values to Coffee_Order_Product__c
		c.YOUR_CUSTOM_FIELD__C = newOrder.Id;
    }
    insert ic;    
    return ic;
    }
	 
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
You are adding Coffee_Order__c  in list of Coffee_Order_Product__c. which is wrong

See below code snippet
global static list<Coffee_Order_Product__c> echoMyType(list<Coffee_Order_Product__c> ic) {
    Coffee_Order__c newOrder = new Coffee_Order__c();
        insert newOrder;
    for(Coffee_Order_Product__c c:ic) {
        //ic.add(newOrder.Id);
		//here you can assign values to Coffee_Order_Product__c
		c.YOUR_CUSTOM_FIELD__C = newOrder.Id;
    }
    insert ic;    
    return ic;
    }
	 
}

 
This was selected as the best answer
Ciprian Stoica 12Ciprian Stoica 12
Thanks buddy!