• Bohdan Dosiak 4
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

hi there

have a piece of code

how to create csv file and name it?

 

public static void sheets (List<Opportunity> oppo){

        List<Opportunity > opps = [SELECT Id, Name, Description, CreatedDate, LastModifiedDate FROM Opportunity limit 10];
        string header = '"Record Id","Name","Description","Created Date","LastModifiedDate" \n';
        string finalstr = header ;
        for(Opportunity o: opps){
            
            string recordString = '"'+o.id+'","'+o.Name+'","'+o.Description+'","'+o.CreatedDate +'","'+o.LastModifiedDate+'"\n';
            
            finalstr = finalstr +recordString;
            
        }


        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('callout:Googel_Sheets/upload/drive/v3/files');
        req.setHeader('Content-Type', 'multipart/mixed'); 
        req.setHeader('Content-length', String.valueOf(finalstr.length())); //required!!!!
        req.setTimeout(120000);
        req.setBody(finalstr);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('RESPONSE   --->   '+res.getStatus());  System.debug('RESPONSE   --->   '+res.getBody());
        System.debug('req   --->   '+req);
        
        
    }

when i send it, in google i recive untitled file with unknown type
User-added image

may be any solutions without JSON???
thx
hi there. have a problem and can't reslove it
when i'll try to execute in anonimus window, first opportunity (example "testopp") creating good. when i try to create opportunity with same name("testopp"),  i get my error.
but when i create opportunity with different name ("testopp2"), first time and all next times it creating with same names, without error.
can enybody help newbie?)
 
trigger OpoTrigger on Opportunity (before insert, before update) {
   
    Set<ID> oppoId = new Set<ID>();
    for(Opportunity oppo: Trigger.New){
        oppoId.add(oppo.AccountId);
    }
    System.debug(oppoId+' - id of oopAcc');
    
    Map<Id, Opportunity> opporMap = new Map <Id, Opportunity>([SELECT Name FROM Opportunity WHERE ID in :oppoId]);
    List<String> nameList = new List<String>();
    for (Opportunity o: opporMap.values()){
        nameList.add(o.Name);
    }
    
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Opps_separated_by_coma__c FROM Account WHERE ID in: oppoId]);
	List<String> accList = new List<String>();
    for(Account acc:accMap.values()){
        if(acc.Opps_separated_by_coma__c != null){
            accList.add(acc.Opps_separated_by_coma__c);
        }
    }

    List<Account> insertAccList = new List<Account>();
    for(Opportunity o:Trigger.new){
        
        if(accList.contains(o.Name) == false){
            
             Account a = new Account();
                a.Id = o.AccountId;
  				accList.add(', ');
                accList.add(o.Name);

                String temp = '';
                for(String str : accList){
                    temp += str;
                }
    
                a.Opps_separated_by_coma__c=temp; 
                insertAccList.add(a);
            
        }else if(accList.isEmpty()){
            Account a = new Account();
            a.Id = o.AccountId;
            a.Opps_separated_by_coma__c = o.name;
            insertAccList.add(a);
            
        }else {
            
            o.addError('*****Alredy exist******');
           

        }
        
    }
  update insertAccList;   
  }

 

hi there

have a piece of code

how to create csv file and name it?

 

public static void sheets (List<Opportunity> oppo){

        List<Opportunity > opps = [SELECT Id, Name, Description, CreatedDate, LastModifiedDate FROM Opportunity limit 10];
        string header = '"Record Id","Name","Description","Created Date","LastModifiedDate" \n';
        string finalstr = header ;
        for(Opportunity o: opps){
            
            string recordString = '"'+o.id+'","'+o.Name+'","'+o.Description+'","'+o.CreatedDate +'","'+o.LastModifiedDate+'"\n';
            
            finalstr = finalstr +recordString;
            
        }


        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('callout:Googel_Sheets/upload/drive/v3/files');
        req.setHeader('Content-Type', 'multipart/mixed'); 
        req.setHeader('Content-length', String.valueOf(finalstr.length())); //required!!!!
        req.setTimeout(120000);
        req.setBody(finalstr);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('RESPONSE   --->   '+res.getStatus());  System.debug('RESPONSE   --->   '+res.getBody());
        System.debug('req   --->   '+req);
        
        
    }

when i send it, in google i recive untitled file with unknown type
User-added image

may be any solutions without JSON???
thx
hi there. have a problem and can't reslove it
when i'll try to execute in anonimus window, first opportunity (example "testopp") creating good. when i try to create opportunity with same name("testopp"),  i get my error.
but when i create opportunity with different name ("testopp2"), first time and all next times it creating with same names, without error.
can enybody help newbie?)
 
trigger OpoTrigger on Opportunity (before insert, before update) {
   
    Set<ID> oppoId = new Set<ID>();
    for(Opportunity oppo: Trigger.New){
        oppoId.add(oppo.AccountId);
    }
    System.debug(oppoId+' - id of oopAcc');
    
    Map<Id, Opportunity> opporMap = new Map <Id, Opportunity>([SELECT Name FROM Opportunity WHERE ID in :oppoId]);
    List<String> nameList = new List<String>();
    for (Opportunity o: opporMap.values()){
        nameList.add(o.Name);
    }
    
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Opps_separated_by_coma__c FROM Account WHERE ID in: oppoId]);
	List<String> accList = new List<String>();
    for(Account acc:accMap.values()){
        if(acc.Opps_separated_by_coma__c != null){
            accList.add(acc.Opps_separated_by_coma__c);
        }
    }

    List<Account> insertAccList = new List<Account>();
    for(Opportunity o:Trigger.new){
        
        if(accList.contains(o.Name) == false){
            
             Account a = new Account();
                a.Id = o.AccountId;
  				accList.add(', ');
                accList.add(o.Name);

                String temp = '';
                for(String str : accList){
                    temp += str;
                }
    
                a.Opps_separated_by_coma__c=temp; 
                insertAccList.add(a);
            
        }else if(accList.isEmpty()){
            Account a = new Account();
            a.Id = o.AccountId;
            a.Opps_separated_by_coma__c = o.name;
            insertAccList.add(a);
            
        }else {
            
            o.addError('*****Alredy exist******');
           

        }
        
    }
  update insertAccList;   
  }