+ Start a Discussion
Arjun SrivastavaArjun Srivastava 

ApiName and value pair in String form how to update a record if i have id of that record?

Hi,

I have an ApiName and value pair in String form like given below. 

 

String fieldvalpair='Name=Aig Sun america,Lead_Status__c=Contacted,Lead_Source__c=Inhouse';

 

 

Prospect pros=new Prospect(id='a0YQ0000003YLph');

 


I want to update record pros,how can i do that?

 

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman

Please try this Apex code script, I hope it will help you!

String fieldValPair='Name=Aig Sun america,Lead_Status__c=Contacted,Lead_Source__c=Inhouse';

/*To make list for all pairvalues string*/

list<String> valuePairList = fieldValPair.split(',');

/*

                Key: Field Name

                Value: Field Value

*/

map<String,String> valuePairMap = new map<String,String>();

/*To create list for Field and value Every time it will return two elements in this list First element will be Field Name second Field Value*/

list<String> splitValuePair = null;

for(String vpl:valuePairList){

                splitValuePair = vpl.split('=');

                if(splitValuePair != null){

                                valuePairMap.put(splitValuePair.get(0),splitValuePair.get(1));

                }

}

/*To updae value put fields in this manner.*/

Prospect objProspect = new Prospect (id='',name = valuePairMap.get('Name'),Lead_Source__c = valuePairMap.get('Lead_Source__c'),Lead_Status__c = valuePairMap.get('Lead_Status__c'));upadte objProspect;